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.
Write a C++ function definition named containsNumber. The function should take three parameters: a one-dimensional integer array, the number of entries in the array, and an integer value. The function should search the array for the integer value and return a boolean value of true if the value is found and false otherwise. Below is a example call to the function: valueFound = containsNumber(table, nbrOfEntries, value); You may assume that all parameters passed to the function are valid, that the nbrOfEntries amount is always greater than or equal to one, and that a specific integer value occurs at most once in the table. Remember to only write the function definition for the containsNumber function. Do not write a complete C++ program.
In C++ Write the definition of a void function that has one double parameter and changes the sign of the parameter (if it is negative it becomes positive, and if it is positive it becomes negative)

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