530,822 views
45 votes
45 votes
1-(50 points) The function sum_n_avgcomputes the sum and the average of three input arguments and relays its results through two output parameters.a)Write a prototype for a function sum_n_avgthat accepts three double-type inputparameters and returns two output parameters through reference.b)Write the function definition for function sum_n_avg. The function definition is where the actual computations are performed.c)Write a function call in main ()forsum_n_avg. The function call can look like below free

User Jon Rosen
by
2.4k points

1 Answer

21 votes
21 votes

Answer:

The program in C++ is as follows:

#include <iostream>

using namespace std;

double *sum_n_avg(double n1, double n2, double n3);

int main () {

double n1, n2, n3;

cout<<"Enter 3 inputs: ";

cin>>n1>>n2>>n3;

double *p;

p = sum_n_avg(n1,n2,n3);

cout<<"Sum: "<<*(p + 0) << endl;

cout<<"Average: "<<*(p + 1) << endl;

return 0;

}

double *sum_n_avg(double n1, double n2, double n3) {

static double arr[2];

arr[0] = n1 + n2 + n3;

arr[1] = arr[0]/3;

return arr;

}

Step-by-step explanation:

This defines the function prototype

double *sum_n_avg(double n1, double n2, double n3);

The main begins here

int main () {

This declares variables for input

double n1, n2, n3;

This prompts the user for 3 inputs

cout<<"Enter 3 inputs: ";

This gets user inputs

cin>>n1>>n2>>n3;

This declares a pointer to get the returned values from the function

double *p;

This passes n1, n2 and n3 to the function and gets the sum and average, in return.

p = sum_n_avg(n1,n2,n3);

Print sum and average

cout<<"Sum: "<<*(p + 0) << endl;

cout<<"Average: "<<*(p + 1) << endl;

return 0;

}

The function begins here

double *sum_n_avg(double n1, double n2, double n3) {

Declare a static array

static double arr[2];

Calculate sum

arr[0] = n1 + n2 + n3;

Calculate average

arr[1] = arr[0]/3;

Return the array to main

return arr;

}

User Lsteinme
by
3.2k points