Problem Solving with C++ (9th Edition)
Problem Solving with C++ (9th Edition)
9th Edition
ISBN: 9780133591743
Author: Walter Savitch
Publisher: PEARSON
bartleby

Videos

Textbook Question
Book Icon
Chapter 17, Problem 1P

Write a function template for a function that has parameters for a partially filled array and for a value of the base type of the array. If the value is in the partially filled array, then the function returns the index of the first indexed variable that contains the value. If the value is not in the array, the function returns −1. The base type of the array is a type parameter. Notice that you need two parameters to give the partially filled array: one for the array and one for the number of indexed variables used. Also, write a suitable test program to test this function template.

Expert Solution & Answer
Check Mark
Program Plan Intro

Program plan:

  • The function template “search” is defined with the three parameters.
    • Inside the function definition, the “for” condition will iterate until the “i” value is less than “num” value.
      • If “a[i]” is equal to “v” then return “i”, otherwise return -1.
  • Define the main function.
    • Declare the required variables and assign the value for those variables.
    • Call the “search” function with the arguments.
    • Check “i” is equal to “-1” or not.
      • If the condition is true, display the search value is not found. Otherwise display the search element position.
Program Description Answer

The program is used to find the search element in the given array is as follows:

Explanation of Solution

Program:

//include the necessary header file

#include<iostream>

using namespace std;

//definition of template

template<typename T>

//definition of "search" function

int search(T a[], int num, T v)

{

  //check the condition

  for(int i = 0; i < num; i++)

        //check the condition

        if(a[i] == v)

            //return "i" value

            return i;

    //return "-1" value

    return -1;

}

//definition of main function

int main()

{

    //declare and assign the array value

    int a[20] = {1, 2, 4, 9, 3, 7, 6, 5, 10, 15};

    //declare and assign the value

    int num = 10;

    int v = 6;

    //declare and call the function

    int i = search(a, num, v);

    //check the condition

    if(i == -1)

        //display the result

        cout<<v<<" is not found in the array\n";

    else

        //display the result

        cout<<v<<" found at index "<<i<<"\n";

    //return statement

    return 0;

}

Sample Output

Output:

6 found at index 6

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Suppose that an array is passed as a parameter. How does this differ from the usual use of a value parameter? When an array is passed as a parameter, it is like passed by reference. A new array will be created in the called function and any changes to the new array will pass back to the original array. When an array is passed as a parameter, it is passed by value. So any changes to the parameter do not affect the actual argument. When an array is passed as a parameter, changes to the array affect the actual argument. This is because the parameter is treated as a pointer that points to the first component of the array. This is different from a value parameter (where changes to the parameter do not affect the actual argument). When an array is passed as a value, changes to the array affect the actual argument. This is because the parameter is treated as a pointer that points to the first component of the array. This is different from a parameter (where changes to the parameter do not…
Write a function which accepts an int array and the number of elements in the array as two parameters.  The function should dynamically create a new int array that is one element larger than the argument array.  Element 0 of the new array should contain size, i.e. the number of elements in the argument array.  Element 0 of the argument array should be copied to Element 1 of the new array, Element 2 of the argument array should be copied to Element 1 of the new array, etc.  The function should return a pointer to the new array and should not modify the argument array.  You may assume that the argument array contains at least one valid element and size is the number of elements in the array.  You do not need to demonstrate calling this function.   Example: if a[] is {2, 7, 4}, and size is 3, the function should return a pointer to a new array with 4 elements containing {3, 2, 7, 4}. int *counter(int a[], int size)
create a function template code that can support different datatypes based on the following:- int doubleValue (int x) { int result; result= 2 * x; return result; }

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Definition of Array; Author: Neso Academy;https://www.youtube.com/watch?v=55l-aZ7_F24;License: Standard Youtube License