Problem Solving with C++ (10th Edition)
Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
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
In C++ Write the definition of a void function that has two parameters: an array, and an integer parameter that specifies the number of elements in the array.  The functions swaps the first and last elements of the array.
Using C++ Programming language: Assume you want a function which expects as parameters an array of doubles and the size of the array. Write the function header that accepts these parameters but is defined in such a way that the array cannot be modified in the function.  You can use your own variable names for the parameters.
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)

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
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
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