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
Refer to page 80 for problems on white-box testing. Instructions: • Perform control flow testing for the given program, drawing the control flow graph (CFG). • Design test cases to achieve statement, branch, and path coverage. • Justify the adequacy of your test cases using the CFG. Link: [https://drive.google.com/file/d/1wKSrun-GlxirS3IZ9qo Hazb9tC440 AZF/view?usp=sharing]
Refer to page 10 for problems on parsing. Instructions: • Design a top-down parser for the given grammar (e.g., recursive descent or LL(1)). • Compute the FIRST and FOLLOW sets and construct the parsing table if applicable. • Parse a sample input string and explain the derivation step-by-step. Link: [https://drive.google.com/file/d/1wKSrun-GlxirS31Z9qoHazb9tC440 AZF/view?usp=sharing]
Refer to page 20 for problems related to finite automata. Instructions: • Design a deterministic finite automaton (DFA) or nondeterministic finite automaton (NFA) for the given language. • Minimize the DFA and show all steps, including state merging. • Verify that the automaton accepts the correct language by testing with sample strings. Link: [https://drive.google.com/file/d/1wKSrun-GlxirS31Z9qo Hazb9tC440AZF/view?usp=sharing]

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++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Definition of Array; Author: Neso Academy;https://www.youtube.com/watch?v=55l-aZ7_F24;License: Standard Youtube License