Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write two functions, both of which take two parameters and that, when called, ch

ID: 3547889 • Letter: W

Question

Write two functions, both of which take two parameters and that, when called, change the values of two doubles that are local to the calling function, to their average. The first function, avgPtr(), uses pointers to accomplish the task. The second function avgRef() uses reference variables to accomplish the task. Also write a main() function that:

a) Reads two doubles(issue no prompt)

b) Calls avgPtr() to change the two double to their average, then print the average.

c) Reads two more doubles(issue no prompt)

d) Calls avgRef() to change the two doubles to their average, then prints the average.

Explanation / Answer

#include<iostream>
using namespace std;
//uses pointers to change the values of two doubles that are local to the calling function, to their average
void avgPtr(double *a,double *b)
{
double avg = (*a+*b)/2;
*a = avg;
*b = avg;
}
//uses reference to change the values of two doubles that are local to the calling function, to their average
void avgRef(double& a,double& b)
{
double avg = (a+b)/2;
a = avg;
b = avg;
}
int main()
{
double value1,value2;
double value3,value4;
cout <<"Enter two double values :";
cin >> value1 >> value2;
cout << endl;
// call pass by pointer
avgPtr(&value1,&value2);
cout << "Average is given by " << value1 << endl;
cout <<"Enter two double values :";
cin >> value3 >> value4;
cout << endl;
// call pass by reference
avgRef(value3,value4);
cout << "Average is given by " << value3 << endl;
return 0;
}