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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
/*
You are to create a file called "Grades.txt". You should put 20 random grades of your choosing in there (manually, not the computer generated ones).
Your program should then inFile all the grades from the file in a function called getGrades(). //the return type and arguements are up to you to
figure out.
Once you are done with that function, your program should go to another function called Calculations(). This function will receive the array of
grades and calculate the smallest, largest, and average of the numbers. **do not sort the numbers**.
After that function is done, your program should go to the final function called calcGradeDistrib(). This function will receive the array and
calculate how many A's, B's, etc. there are.
Be sure to display the results of the 2nd and 3rd funcion to the screen in a nice fashion.
*/
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
int getGrades(int[], int);
void Calculations(int[],int);
void calcGradeDistrib(int[],int);
using namespace std;
int main()
{
const int SIZE = 20;
int grades[SIZE];
int size = getGrades(grades, SIZE);
if (size < 0) {
cout << "Error reading grades\n";
return 1;
} else if (size == 0) {
cout << "No grades read\n";
return 1;
} else {
cout << "Read " << size << " grades\n";
}
Calculations(grades, size); // pass actual number of grades
calcGradeDistrib(grades, size); // pass actual number of grades
return 0;
}
// Read up to "size" grades from "Grades.txt" into the grades array.
// Return the number of grades actually read, or -1 to indicate an error
int getGrades(int grades[], int size)
{
ifstream infile;
infile.open("Grades.txt");
int x=0;
while (x<20 && (infile>>grades[x])) {
++x;
}
if (infile || infile.eof()) {
return x;
} else {
return -1;
}
}
//this function calculates the lowest, highest, and average grade
void Calculations(int grades[],int SIZE)
{
int lowest=grades[0];
for(int count=1; count<SIZE; count++)
{
if(grades[count]<lowest)
lowest=grades[count];
}
cout<<lowest<<" is the lowest grade. "<<endl;
}
{
int highest=grades[0];
for(int count=1; count<SIZE; count++)
{
if(grades[count]>highest)
highest=grades[count];
}
cout<<highest<<" is the highest grade. "<<endl;
}
{
double average=0.0;
int total=0;
for(int count=0;count<SIZE; count++)
{
total+=grades[count];
}
average=total/SIZE;
cout<<average<<" is the average grade. "<<endl;
}
//this function calculates the letter grades
void calcGradeDistrib(int grades[],int SIZE)
{
int distribution[5]={0,0,0,0,0};
int total=0;
for(int x=0; x<20; x++)
{
if(grades[x]>=50&&grades[x]<=59)
{
total+=total;
total++;
distribution[0]=total;
}
else if(grades[x]>=60&&grades[x]<=69)
{
total+=total;
total++;
distribution[1]=total;
}
else if(grades[x]>=70&&grades[x]<=79)
{
total+=total;
total++;
distribution[2]=total;
}
else if(grades[x]>=80&&grades[x]<=89)
{
total+=total;
total++;
distribution[3]=total;
}
else
{
total+=total;
total++;
distribution[4]=total;
}
}
cout<<distribution[0]<< " of the grades are Fs"<<endl;
cout<<distribution[1]<<" of the grades are Ds"<<endl;
cout<<distribution[2]<<" of the grades are Cs"<<endl;
cout<<distribution[3]<<" of the grades are Bs"<<endl;
cout<<distribution[4]<<" of the grades are As"<<endl;
}
|