Consider the following C++ program. Assume that all the function calls are Indep
ID: 3692721 • Letter: C
Question
Consider the following C++ program. Assume that all the function calls are Independent that la no function call affects other functions rsquo calls for rach of the parameter passing method. What are the values of the variables value and list after each of the three calls to swap for each of the following parameter passing methods? Consider the following C program. Show the slack contents for the points labeled 1, 2, and 3 using the sequence of function calls: main calls f1; f1 calls f2; and f2 calls f3. Use the space beside and below Ihe program. Write a generic C++ function that takes an array of generic elements and a scalar of the same type as the array elements. The purpose of the function is to search the given arras for a given scalar and to return the subscript of the scalar-in the array. If the scalar is not in the array, the function must return -1. Test the function for int, double, char, and siring types. Provide below the complete code.Explanation / Answer
Answer for Question 1:
In Pass by case : The values after swapping doesn't reflect in main function
in main
swap(value,list[0]); ----- 2 1
swap(list[0],list[1]); ----1 1
swap(value,list[value]);-----2 5
In Pass By Reference:
in main
swap(value,list[0]); ----- 1 2
swap(list[0],list[1]); ----3 3
swap(value,list[value]);-----2 5
in Value by Reference :
in main
swap(value,list[0]); ----- 1 2
swap(list[0],list[1]); ----3 3
swap(value,list[value]);-----2 5
Answer for Question 2:
After excuting this below given c++ program output is K K C
#include<iostream>
using namespace std;
void f3(char c, double k)
{
cout<<"C ";
}
void f2(double r)
{
char k;
cout<<"K ";
f3(k,r);
}
void f1(double p, char c)
{
int k;
cout<<"K ";
f2(p);
}
int main()
{
double s;
char z;
f1(s,z);
return 1;
}
Answer for Question 3:
This below template program will work for all data types except string type for string using the stl algorhim find function.
Refer the below code
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
template<typename T>
bool arraySearch(T array[], int size, T thing)
{
for(int i = 0; i < size; i++)
{
if(array[i] == thing)
return true;
}
return false;
}
int main()
{
const int SIZE = 12;
int intArray[] = {14, 3, 6, 76, 34, 22, 21, 54, 33, 23, 76, 234};
cout << "The element was found: " << arraySearch(intArray, SIZE, 23) << endl;
double doubleArray[] = {34.5, 65.56, 11.1, 45.4, 87.5, 98.3, 23.6, 15.5, 3.3, 5.44, 54.3, 99.9};
cout << "The element was found: " << arraySearch(doubleArray, SIZE, 23.6) << endl;
string stringArray[] = {"cool", "bug", "master", "katze", "republic", "randolph", "watermelon", "igloo", "sardine", "cream", "yellow", "rubber"};
std::string* res = std::find(stringArray, stringArray + sizeof(stringArray) / sizeof(std::string), "cool");
cout << "The element was found: " << *res<< endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.