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++ (10th Edition)
Additional Engineering Textbook Solutions
Introduction to Programming Using Visual Basic (10th Edition)
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Starting Out with Python (4th Edition)
Starting Out with C++ from Control Structures to Objects (8th Edition)
Modern Database Management
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
- In C++, write a function (named "templated_array_copy") that takes two parameters (two arrays of the same type). It copies the first array into the second. Note, if the arrays are different sizes, only copy what fits. Case 1: double source[] ={1, 2, 5, 6}; double dest[]={9.4, 4, 5}; Answer: double expected[]={1, 2, 5}arrow_forwardWrite a function called “CompareTwo” that accepts four parameters: A1 array of type integer, A2 array of type integer, S1 number of elements in A1 array, and S2 number of elements in A2 array. The function must return true if the sum of A1 equals to the sum of A2, otherwise it must return false.arrow_forwardWrite a function intersect() that takes five arguments: an array of integers, number of elements in this array, the second array of integers, number of elements in the second array, the third array as parameters; finds the numbers in the first array that also exist in the second array and places these numbers into the third array; returns the number of matching values stored in array-3. Prototype: int intersect(int ar1[], int n1, int ar2[], int n2, int ar3[])arrow_forward
- 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.arrow_forwardCreate a C++ Function Template named swap so that it has two parameters of the same type. A Template Function created from swap will exchange the values of these two parameters. Create a C++ Function Template named multiples so that it has three parameters sum, x, and n. The first two parameters will have the type represented by the function template type parameter WhatKind. n will always be int. The return type is void. All parameters are passed by value except for sum which is passed by reference. A Template Function created from multiples will compute... sum = 1 + x + 2x + 3x + ... + nx Create a C++ Class called Mathy that also contains these two functions and that is properly templatized. Write code in your main function to demonstrate that each function works well individually and when used from within the class using multiple types. Demonstrate that you achieve the same results using both the templatized class and functions.arrow_forwardWrite a function template that gets an array of generic type T as well as the number of items within the array, calculates the standard deviation of the items within the array and returns it. The user will first give an integerItemCount value (int) and that many integer values (all ints). Then, the user will give a doubleItemCount value (int) and that many double values (all doubles). Then, the program will use the function template to calculate the standard deviations of both arrays and print them out as first the standard deviation of the int array, then the standard deviation of the double array. Standard deviation of an array with size N, values are given as , and arithmetic mean as μ can be calculated as: Requirements: You MUST define and use a function template that takes an array of generic type T and an integer which defines the size of the array Getting all the user input, calling the appropriate version of the function via the template and printing the output should be done…arrow_forward
- Write a function in C++ which (normally) allocates new space for a double array. It should alter its pointer argument to point to the newly allocated space. The size of the array should be a second argument. If this argument is not a valid array size, allocate a single double instead of an array. An example call might be: double * p; allocate(p, 25); // p now points to an array of 25 doubles -- if successfularrow_forwardWrite a function checkConditions that takes a two-dimensional array of integers, array, and a set of integers, conditions, as input. The function should return 0 if all elements in the array are contained in the conditions set. If any element in the array is not in the conditions set, the function should return -1. Function signature: int checkConditions(int array[][], int rowSize, int colSize, std::set<int> conditions) Write the implementation of the function checkConditions without using any built-in functions for searching elements in sets. You should implement your own algorithm for searching elements in the conditions set. Example: int arr[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}}; std::set<int> conditions = {1,2,3,6,7,8}; int result = checkConditions(arr, 3, 4, conditions); //result will be 0 since all elements in the array are in the conditions set.arrow_forwardusing online compiler, create a function template code that can support different datatypes based on the following:- int doubleValue (int x) { int result; result= 2 * x; return result; }arrow_forward
- Write the definition of a function named isSorted that receives three arguments: an array of int, an int that indicates the number of elements of interest in the array, and a bool.If the bool argument is true then the function returns true if and only if the array is sorted in ascending order. If the bool argument is false then the function returns true if and only if the array is sorted in descending order. In all other cases the function returns false.You may assume that the array has at least two elements.arrow_forwardWrite a C function arrDivN() that takes one array of integers and three integer numbers (say ar, n1, n2, n3) as parameters, it stores into the array the numbers divisible by n3 between n1 and n2 (both inclusive) and returns the count of the numbers stored in the array. Prototype: int arrDivN(int ar[], int n1, int n2, int n3);arrow_forwardAnswer the given question with a proper explanation and step-by-step solution. JavaScript DO NOT submit with any console.log statements in your file. DO NOT edit the function definitions. You should simply write your code inside each function body. The functions are provided for you as a starting point in the submission-template.js file. DO NOT create your own values inside the functions. We have a test suite which will pass in its own values. For example, in findSum, there is an array of numbers (called array) passed into the function as a parameter. You should work with array to write your solution. It's fine to pass your own values as you're working, but write the function body with the assumption that it should work against any appropriate values that are passed in. Problem 4 - Write a function called largestPair that takes an array of integers and finds the pair of adjacent elements that has the largest product and return that product. For example: largestPair([5, 1, 2, 3, 1,…arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning