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 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.
- Inside the function definition, the “for” condition will iterate until the “i” value is less than “num” value.
- 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.
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;
}
Output:
6 found at index 6
Want to see more full solutions like this?
Chapter 17 Solutions
Problem Solving with C++ plus MyProgrammingLab with Pearson eText-- Access Card Package (9th Edition)
Additional Engineering Textbook Solutions
Starting Out with Programming Logic and Design (5th Edition) (What's New in Computer Science)
Thinking Like an Engineer: An Active Learning Approach (4th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
Electric Circuits. (11th Edition)
Mechanics of Materials (10th Edition)
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
- 4. def modify_data(x, my_list): X = X + 1 my_list.append(x) print(f"Inside the function: x = {x}, my_list = {my_list}") num = 5 numbers = [1, 2, 3] modify_data(num, numbers) print(f"Outside the function: num = {num}, my_list = {numbers}") Classe Classe that lin Thus, A pro is ref inter Ever dict The The output: Inside the function:? Outside the function:?arrow_forwardpython Tasks 5 • Task 1: Building a Library Management system. Write a Book class and a function to filter books by publication year. • Task 2: Create a Person class with name and age attributes, and calculate the average age of a list of people Task 3: Building a Movie Collection system. Each movie has a title, a genre, and a rating. Write a function to filter movies based on a minimum rating. ⚫ Task 4: Find Young Animals. Create an Animal class with name, species, and age attributes, and track the animals' ages to know which ones are still young. • Task 5(homework): In a store's inventory system, you want to apply discounts to products and filter those with prices above a specified amount. 27/04/1446arrow_forwardOf the five primary components of an information system (hardware, software, data, people, process), which do you think is the most important to the success of a business organization? Part A - Define each primary component of the information system. Part B - Include your perspective on why your selection is most important. Part C - Provide an example from your personal experience to support your answer.arrow_forward
- Management Information Systemsarrow_forwardQ2/find the transfer function C/R for the system shown in the figure Re དarrow_forwardPlease original work select a topic related to architectures or infrastructures (Data Lakehouse Architecture). Discussing how you would implement your chosen topic in a data warehouse project Please cite in text references and add weblinksarrow_forward
- Please original work What topic would be related to architectures or infrastructures. How you would implement your chosen topic in a data warehouse project. Please cite in text references and add weblinksarrow_forwardWhat is cloud computing and why do we use it? Give one of your friends with your answer.arrow_forwardWhat are triggers and how do you invoke them on demand? Give one reference with your answer.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,