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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
void getGrades ( int grades[], int& size )
{
int counter;
size = 10;
cout << " \n\n ";
cout << setw(40 + ( 52 / 2 )) << " Please enter 10 grades as integers to be averaged. " << endl;
cout << " \n\n\n ";
for ( counter = 0; counter <10; counter++ )
{
cout << setw(40) << " Enter grade # " << (counter +1) << " --> ";
cin >> grades[counter];
}
}
double computeAverage ( int randomArray[], int grades[], int size, float& average )
{
int counter;
float sum = 0.0;
if ( grades[size] )
for ( counter = 0; counter <size; counter++ )
{
sum+= grades[counter] ;
average = ( sum / size );
}
return grades[size];
if ( randomArray[size] )
for ( counter = 0; counter <size; counter++ )
{
sum+= randomArray[counter];
average = ( sum / size );
}
return randomArray[size];
}
double printInts ( int randomArray[], int grades[], float average, int size )
{
int counter;
if ( randomArray[size] )
for ( counter = 0; counter <size; counter++ )
{
cout << setw(35) << " The numbers " << ( counter + 1 ) << " generated " << randomArray[counter] << endl;
}
return randomArray[size];
if ( grades[size] )
for ( counter = 0; counter <size; counter++ )
{
cout << setw(35) << " The numbers " << ( counter + 1 ) << " generated " << grades[counter] << endl;
}
return grades[size];
}
void getRandom ( int randomArray[], int& size )
{
int counter;
int raNum;
int rand_max = 50;
int rand_min = 1;
size = 20;
cout << setw(40 + ( 35 / 2 )) << " This random generator will generate 20 random numbers between 1 and 50.\n " << endl;
srand(static_cast<unsigned int>(time(0)));
for ( counter = 0; counter <20; counter++ )
{
raNum = (rand() % (rand_max - rand_min + 1)) + rand_min;
randomArray[counter] = raNum;
}
}
int main ()
{
// local constants
int grades[10];
int randomArray[20];
int size;
float average;
system ("title Average Grades Array ");
system ("color 1f ");
cout << " \n\n\n ";
getGrades ( grades, size );
system ("cls");
cout << " \n\n\n ";
computeAverage ( randomArray, grades, size, average );
printInts ( randomArray, grades, average, size );
cout << " \n\n\n ";
cout << setw(40) << " The average of these 10 grades is: " << average << endl;
cout << " \n\n\n ";
system("pause");
system("cls");
cout << " \n\n\n ";
getRandom ( randomArray, size );
printInts ( randomArray, grades, average, size );
computeAverage ( randomArray,grades, size, average );
cout << " \n\n\n ";
cout << setw(40) << " The average of these 20 numbers is: " << average << endl;
system ("pause");
cout << " \n\n\n\n ";
return 0;
}
|