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

C++ Write the function sum() that receives two parameters; a double type array a

ID: 3834832 • Letter: C

Question

C++

Write the function sum() that receives two parameters; a double type array and an integer type parameter representing the size of the array. The function returns the sum of the elements in the array

Write the function average() that receives two parameters; a double type array and an integer type parameter representing the size of the array. This function calculates the average of the numbers in the array and returns this average. The function average() must make a call to the function sum() in part (a) to obtain the sum of the elements used in calculating the average.

Write a main function that initializes and array of double type using random integers between 1 and 100. You can decide on the size of the array, perhaps a size between 10 to 20 elements. Display the numbers generated then call the function average() to receive the value of the average, which is then printed.

Rewrite the function average in part (b) average2() but make the return type of the function as“void” and use a reference parameter to communicate/return the calculated average to the man() function. ( hint: instead of two parameters the function average2() must have three parameters with some minor adjustments) [no need to make a call to this function, just write it to demonstrate the use of reference parameters.]

Explanation / Answer

#include <iostream>
using namespace std;

double sum(double size,double list[])

               { int i;
                       double s=0.0;

                       for (i=0;i<size;i++)
                               s+=list[i];
                       return(s);
               }

double average(double size,double list[])

               {
               int i;
                       double s=0.0;
                   s=sum(size,list);
                        return(s/size);
               }
              
void average2(double &a,double &b,double &c)

               {
                 
                       double d=c+a+b;
                      
                        cout<<d/3;
               }
      
int main() {
double list[5] = {2, 4, 6, 8, 10};
cout<<"element of array : ";
for (int i=0;i<5;i++)
{
cout<<list[i]<<" ";
}                          
cout<<" sum is : "<< sum( 5,list);  
cout<<" Average is : "<<average(5,list)<<" ";
double a=5,b=3,c=5;
average2(a,b,c);
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote