Can you rewrite function double cal_average() differently (use different coding to write it, but the result remain the s

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Can you rewrite function double cal_average() differently (use different coding to write it, but the result remain the s

Post by answerhappygod »

Can you rewrite function double cal_average()differently (use different coding to write it, but the resultremain the same) C++
#include <iostream>#include <iomanip>using namespace std;
void format_fun(){ cout<<setw(30)<<setfill('-')<<""<<endl;}
void populate_arr(int narr [], int asz){format_fun();cout << "CS 162 Assignment One" << endl <<endl;format_fun(); for (int i = 0; i < asz; i++){ cout << "Enter number: "; cin >> narr; format_fun(); }}
double cal_average(int narr [], int asz){ double sum = 0; for (int i = 0; i < asz; i++){ sum += narr; } return sum / asz;}
void print(int narr [], int asz, double average){cout << "Array value = ";for (int i = 0; i < asz; i++){ cout << narr << " ";}cout << endl;format_fun();cout << fixed << setprecision(2);cout << "Average = " << average << endl;}
int main(){ const int arr_sz = 3; int arr_values [arr_sz];
populate_arr(arr_values,arr_sz); double average = cal_average(arr_values, arr_sz); print(arr_values,arr_sz, average);
format_fun(); return 0;}
Sample output
--------------------------------
CS 162 Assignment One
--------------------------------
Enter number: 23
--------------------------------
Enter number: 44
--------------------------------
Enter number: 55
--------------------------------
Array values = 23 44 55
--------------------------------
Average = 40.67
--------------------------------
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply