Create an array of 20 doubles Assign values to your array us

Create an array of 20 doubles. Assign values to your array using a for loop, where myDoubles is the name of the array created. Print out the values using another for loop. Now, using your array and another loop, calculate the average of the values in the myDoubles array and print out the average using the statement below: \"The average value of myDoubles is: X\"

Solution

Please find the required program along with its output. Please see the comments against each line to understand the step.

#include <iostream>
#include <string>

using namespace std;

int main()
{
double myDoubles[20]; //declare the double array
  
for(int i = 0; i < 20; i++){ //assign values to the double array
myDoubles[i] = i;
}
  

cout << \"Array values are: \" << endl;
for(int i = 0; i < 20; i++){ //print the values of array
cout << myDoubles[i] << \" \" << flush;
}
cout <<endl;
  
  
double avg = 0;
for(int i = 0; i < 20; i++){ //iterate through the loop to determine the average
avg = avg + myDoubles[i];
}
  
avg = avg/20;
  
cout << \"The average value of myDoubles is: \" << avg <<endl; //print the average

}

-------------------------------------------------------------------------------------------

OUTPUT:

Array values are:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
The average value of myDoubles is: 9.5

 Create an array of 20 doubles. Assign values to your array using a for loop, where myDoubles is the name of the array created. Print out the values using anoth

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site