Write two functions, both of which take two parameters and that, when called, ch
ID: 3547888 • 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
main()
{
double first,second;
cin >> first;
cin >> second;
avgPtr(&first,& second);
cout>>"first number is:">>first;
cout>>"second number is:">>second;
cin >> first;
cin >> Second;
avgRef(first,second);
cout>>"first number is:">>first;
cout>>"second number is:">>second;
}
void avgPtr(double *Val1, double *Val2)
{
double avg=(*Val1 + *Val2)/2;
*Val1 = avg;
*Val2 = avg;
}
void avgRef(double &Val1, double &Val2)
{
double avg= (Val1 + Val2)/2;
Val1= avg;
Val2= avg;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.