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
This is a question that I have and would like someone who has experiences with scene graphs and entity component systems to answer.For context, I am currently implementing a game engine and currently I am debating on our current design.Our current design is we have a singular game component class that every component inherits from. Where we have components like SpriteRendererComponent, Mehs Component, etc. They inherit from this GameComponent class. The point of this is being able to have O(1) access to the scene to being able to modify components to attach more components with the idea of accessing those components to specific scene objects in a scene.Now, my question is what kinds of caveauts can this cause in terms of cache coherence? I am well aware that yes its O(1) and that is great but cache coherence is going to be really bad, but would like to know more explicit details and real-life examples such as write in RAM examples on how this is bad. A follow-up question that is part…
Q4: Consider the following MAILORDER relational schema describing the data for a mail order company. (Choose five only). PARTS(Pno, Pname, Qoh, Price, Olevel) CUSTOMERS(Cno, Cname, Street, Zip, Phone) EMPLOYEES(Eno, Ename, Zip, Hdate) ZIP CODES(Zip, City) ORDERS(Ono, Cno, Eno, Received, Shipped) ODETAILS(Ono, Pno, Qty) (10 Marks) I want a detailed explanation to understand the mechanism how it is Qoh stands for quantity on hand: the other attribute names are self-explanatory. Specify and execute the following queries using the RA interpreter on the MAILORDER database schema. a. Retrieve the names of parts that cost less than $20.00. b. Retrieve the names and cities of employees who have taken orders for parts costing more than $50.00. c. Retrieve the pairs of customer number values of customers who live in the same ZIP Code. d. Retrieve the names of customers who have ordered parts from employees living in Wichita. e. Retrieve the names of customers who have ordered parts costing less…
Q4: Consider the following MAILORDER relational schema describing the data for a mail order company. (Choose five only). (10 Marks) PARTS(Pno, Pname, Qoh, Price, Olevel) CUSTOMERS(Cno, Cname, Street, Zip, Phone) EMPLOYEES(Eno, Ename, Zip, Hdate) ZIP CODES(Zip, City) ORDERS(Ono, Cno, Eno, Received, Shipped) ODETAILS(Ono, Pno, Qty) Qoh stands for quantity on hand: the other attribute names are self-explanatory. Specify and execute the following queries using the RA interpreter on the MAILORDER database schema. a. Retrieve the names of parts that cost less than $20.00. b. Retrieve the names and cities of employees who have taken orders for parts costing more than $50.00. c. Retrieve the pairs of customer number values of customers who live in the same ZIP Code. d. Retrieve the names of customers who have ordered parts from employees living in Wichita. e. Retrieve the names of customers who have ordered parts costing less than$20.00. f. Retrieve the names of customers who have not placed…

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