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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
#include <iostream>
#include <iomanip>
#include <ctime>
#include <fstream>
using namespace std;
void clearGrades(int g[] int nG)
{
for(int i = 0; i < nG; i++)
{
g[i] = 0;
}
}
void setNumberOfGrades(int g[] &nG)
{
do
{
cout << "Number of Grades (0-5): ";
cin >> nG;
} while(nG < 0 || nG > 5);
}
void enterGrades(int g[], int nG)
{
for(int i = 0; i < nG; i++)
{
do
{
cout << "Grade " << i + 1 << " : ";
cin >> g[i];
} while(g[i] < 0 || g[i] > 100);
}
}
void randomGrades(int g[], int nG)
{
for(int i, i < nG; i++)
{
g[i] = rand()%101;
}
}
void readGrades(int g[], int nG)
{
ifstream inFile;
inFile.open("Grades.txt");
for(int i = 0; 1 <nG; i++)
{
inFile >> g[i];
}
inFile.close();
}
void writeGrades(int g[], int nG)
{
ofstream outFile;
outFile.open("Grades.txt");
for(int i = 0; 1 <nG; i++)
{
outFile << g[i] << " ";;
}
outFile.close();
}
float calcAverage(int g[], int nG)
{
if(nG == 0)
{
return 0;
}
else
{
int total = 0;
for(int = 0; i < nG; i++)
{
total = total + g[i];
}
return (total/float(nG));
}
}
const int gradeSize = 5;
int main()
{
int grades[gradeSize] = {};
int numberOfGrades = 0;
int menuSelection = 0;
srand(int(time(0)));
do
{
system("cls");
cout << "Number of Grades: " << numberOfGrades << endl;
cout << "Loaded Grades: ";
for(int i=0, i < numberOfGrades; i++)
{
cout << grades[i] << " ";
]
cout << fixed << setprecision(1) << endl;
cout << "Grade Average :" << calcAverage(grades, numberOfGrades) << endl << endl << endl;
cout << "(1) Set the number of grades" << endl;
cout << "(2) Enter the grades manually" << endl;
cout << "(3) Generate random grades" << endl;
cout << "(4) Read grades from Grades.txt file" << endl;
cout << "(5) Write grades to Grades.txt file" << endl;
do
{
cout << "Menu selection (1-5) : ";
cin >> menuSelection;
} while(menuSelection < 1 || menuSelection > 5)
cout << endl << endl;
switch(menuSelection)
{
case 1: clearGrades(grades, gradeSize);
setNumberOfGrades(numberOfGrades);
break;
case 2: clearGrades(grades, gradeSize);
enterGrades(grades, numberOfGrades);
break;
case 3: clearGrades(grades, gradeSize);
randomGrades(grades, numberOfGrades);
break;
case 4: clearGrades(grades, gradeSize);
readGrades(grades, numberOfGrades);
break;
case 5: writeGrades(grades, numberOfGrades);
break;
}
}while(true)
system("pause")
return 0;
}
|