I need to display the average pounds of coffee ordered stored in the pounds arra
ID: 3802732 • Letter: I
Question
I need to display the average pounds of coffee ordered stored in the pounds array using a while loop. Needs to be in C++ obviously
#include
#include
using namespace std;
//function prototype
double getAvg();
int main()
{
double pounds[6] = {10, 15.5, 17, 18.5, 11, 12.5};
double average = 0.0;
average = getAvg();
cout << fixed << setprecision(2);
cout << "Average number of pounds ordered: " << average << endl;
return 0;
} //end of main function
//*****function definitions*****
double getAvg()
{
} //end of getAvg function
Explanation / Answer
#include <iostream>
#include <iomanip>
using namespace std;
double getAvg(double pounds[], int n);
int main()
{
double pounds[6] = {10, 15.5, 17, 18.5, 11, 12.5};
double average = 0.0;
average = getAvg(pounds, 6);
cout << fixed << setprecision(2);
cout << "Average number of pounds ordered: " << average << endl;
return 0;
} //end of main function
//*****function definitions*****
double getAvg(double pounds[], int n)
{
double sum = 0;
int i=0;
while(i<n){
sum = sum +pounds[i];
i++;
}
return sum/n;
} //end of getAvg function
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Average number of pounds ordered: 14.08
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.