1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int ComputeAve (int a[], int n);
int ComputeAve (float d[], int n, float&D_Average);
template <class T>
void ReadData (T x[], int n)
{
for (int i = 0; i < n; ++i)
cin >> x[i];
cout << endl;
}
template <class T>
void DisplayData (T x[], int n)
{
for (int i = 0; i < n; ++i)
cout << x[i] << " ";
cout << endl;
}
template <class T1, class T2>
void ShowAve (T1 p,T2 q)
{
cout << p << q;
cout << endl;
}
template <class T1, class T2>
void ShowAve (T1 p, T2 q)
{
cout << p << q;
cout << endl;
}
int main()
{
int a[5]; char c[6]; float d[4]; string s[4];
///Read data into each array
cout << "Enter 5 integer numbers:"; ReadData(a,5);
cout << "Enter 6 characters:"; ReadData(c,6);
cout << "Enter 4 decimal numbers:"; ReadData(d,4);
cout << "Enter 4 names:"; ReadData(s,4);
///Display all arrays
cout << fixed << showpoint << setprecision(2);
cout << "This is array a:"; DisplayData(a,5);
cout << "This is array c:"; DisplayData(c,6);
cout << "This is array d:"; DisplayData(d,4);
cout << "This is array s:"; DisplayData(s,4);
///Compute the average of data in array a and d
float A_Average=ComputeAve(a,5);
ShowAve("The average of numbers in array a is:", A_Average);
ComputeAve(d,4,D_Average);
ShowAve(D_Average, " is the average of numbers in array d");
///Terminate program
system ("pause");
return 0;
}
int ComputeAve (int a[], int n)
{
float A_Average = 0;
float total = 0;
for (int i = 0; i < n; ++i)
{
total+=a[i];
}
A_Average = total/5.;
return A_Average;
}
int ComputeAve (float d[], int n, float&D_Average)
{
D_Average = 0;
float total = 0;
for (int i = 0; i < n; ++i)
{
total+=d[i];
}
D_Average = total/4.;
return D_Average;
}
|