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

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

ID: 3834778 • Letter: C

Question

C++

1. 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.

2. 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.

3. 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.

4 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;
//defining the sum function
double sum(double arr[100],int size){   //sum function with 2 argument as array and size of array
   double sum=0;
   for(int i=0;i<size;i++){   //applying the loop upto size of array and summing the array each element
       sum+= arr[i];       // adding each element of array
   }
   return sum;           //return the sum calculated
}

double average(double arr[100],int size){       //average function with 2 argument as array and size of array return avg of array
   double sumCal=sum(arr,size);       // getting the sum of array by calling sum() function
   double avgCal = sumCal/size;       // calculating average of array
   return avgCal;                       //return the average of array calculated
}

int main(){
   int size;
   double arr[100];
   cout<<"enter size of array btw 10-20: ";       //entering the size of array btw 10-20
   cin>>size;
   cout<<"enter elements of array: "<<endl;       //entering elements of array
   for(int i=0;i<size;i++){
       cin>>arr[i];
   }
  
   cout<<"average is: "<<average(arr,size)<<endl;       //calling the average function that returns the average of array and printing it.
  
  
   return 0;
}

this is the first part of the program

last part

#include<iostream>
using namespace std;
//defining the sum function
double sum(double arr[100],int size){   //sum function with 2 argument as array and size of array
   double sum=0;
   for(int i=0;i<size;i++){   //applying the loop upto size of array and summing the array each element
       sum+= arr[i];       // adding each element of array
   }
   return sum;           //return the sum calculated
}

double average(double arr[100],int size){       //average function with 2 argument as array and size of array return avg of array
   double sumCal=sum(arr,size);       // getting the sum of array by calling sum() function
   double avgCal = sumCal/size;       // calculating average of array
   return avgCal;                       //return the average of array calculated
}

void average2(double arr[100],int size){       //average function with 2 argument as array and size of array return void
   double sumCal=sum(arr,size);           // getting the sum of array by calling sum() function
   double avgCal = sumCal/size;       // calculating average of array
   cout<<"average new is: "<<avgCal;       //print avg of array
}
int main(){
   int size;
   double arr[100];
   cout<<"enter size of array btw 10-20: ";       //entering the size of array btw 10-20
   cin>>size;
   cout<<"enter elements of array: "<<endl;       //entering elements of array
   for(int i=0;i<size;i++){
       cin>>arr[i];
   }
  
   cout<<"average is: "<<average(arr,size)<<endl;       //calling the average function that returns the average of array and printing it.
   average2(arr,size);                                   //calling the average2 function that prints the avergae in function itself.
  
   return 0;
}

in this i have included comments on each and every line, hope you will understand.

thank you!

have a great day!