4. Write the definition of a void function that takes as input two parameters of
ID: 3838948 • Letter: 4
Question
4. Write the definition of a void function that takes as input two parameters of type int, say
sum and testScore. The function updates the value of sum by adding the value of
testScore. The new value of sum is reflected in the calling environment. (10)
5. Consider the following program: (20)
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
void traceMe(double x, double y);
int main()
{
double one, two;
cout << "Enter two numbers: ";
cin >> one >> two;
cout << endl;
traceMe(one, two);
traceMe(two, one);
return 0;
}
void traceMe(double x, double y)
{
double z;
if (x != 0)
z = sqrt(y) / x;
else
{
cout << "Enter a nonzero number: ";
cin >> x;
cout << endl;
z = floor(pow(y, x));
}
cout << fixed << showpoint << setprecision(2);
cout << x << ", " << y << ", " << z << endl;
}
The function traceMe in this program outputs the values of x, y, and z. Modify the definition of
this function so that rather than print these values, it sends the values back to the calling environment
and the calling environment prints these values.
Explanation / Answer
4) void function (int& sum, const int& testscore)
{
sum += testscore;
}
5)
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
double traceMe(double& x, double& y);
int main()
{
double one, two;
cout << "Enter two numbers: ";
cin >> one >> two;
cout << endl;
double z = traceMe(one, two);
cout << fixed << showpoint << setprecision(2);
cout << one << ", " << two << ", " << z << endl;
z = traceMe(two, one);
cout << one << ", " << two << ", " << z << endl;
return 0;
}
double traceMe(double& x, const double& y)
{
double z;
if (x != 0)
z = sqrt(y) / x;
else
{
cout << "Enter a nonzero number: ";
cin >> x;
cout << endl;
z = floor(pow(y, x));
}
return z;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.