hello guys, right now i try to write a problem in order to keep track student's grade using vector in c++. it should include one procedure(determin_grade), and three functions(average; find_highest; and find_lowest). however when i write the code, i can not the similar output as below (because i can not find out which part i need to fix):
Enter result “1” (or -1 if no more result): 55
Enter result “2” (or -1 if no more result): 90.4
Enter result “3” (or -1 if no more result): 110 (when calculate the average, and finding both lowest and highest, this number will not be counted. because it is out of the grade range that from 0 to 100)
Invalid Input!
Enter result “3” (or -1 if no more result): 74.60
Enter result “4” (or -1 if no more result): 34.6
Enter result “5” (or -1 if no more result): -1
Summary of the results:
Result 1: 55.00 Grade P
Result 2: 90.40 Grade A
Result 3: 74.60 Grade B
Result 4: 34.60 Grade U
The average of the results = 63.65
The lowest of the results = 34.60
The highest of the results = 90.40
please help figure out which part i need to change in order to get the similar output. thanks.
there is my code:
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
void determin_grade();
double averagevector(vector<double>);
double find_highest(vector<double>);
double find_lowest(vector<double>);
int main()
{
int num_result=5;
double input;
int index=1;
vector <double> grades;
double average, highest,lowest;
while (index <= 5)
{
cout<<"Enter result "<<index<< "(or -1 if no more result): ";
cin>>input;
if (input >=0 && input<50)
{
cout <<"Grade U will be assigned to this result" <<endl;
index++;
}
else if (input>=50 && input<60)
{
cout <<"Grade P will be assigned to this result" <<endl;
index++;
}
else if (input>=60 && input<70)
{
cout <<"Grade C will be assigned to this result" <<endl;
index++;
}
else if (input>=70 && input<90)
{
cout <<"Grade B will be assigned to this result" <<endl;
index++;
}
else if (input>=90 && input<=100)
{
cout <<"Grade A will be assigned to this result" <<endl;
index++;
}
else if (input> 100)
{
cout <<"Invalid Input" <<endl;
index;
}
else
break;
grades.push_back(input);
}
determin_grade();
average = averagevector(grades);
cout<<"average is "<<average<<endl;
highest = find_highest(grades);
cout<<"highest is "<<highest<<endl;
lowest = find_lowest(grades);
cout<<"lowest is "<<lowest<<endl;
return 0;
}
void determin_grade(vector<double> vectinput)
{
int index;
int num_result=5;
cout<<"summary of the result: \n";
cout<<fixed<<showpoint<<setprecision(2);
for (index=0; index < num_result; index++)
{
cout<<"result "<<(index+1);
cout<<": "<<vectinput.size()<< endl;
}
}
double averagevector(vector<double> vectgrade)
{
double total = 0.0;
double average;
for(int index=0; index < vectgrade.size(); index++)
{
total += vectgrade[index];
average = total/vectgrade.size();
}
return average;
}
double find_highest(vector<double> vectgrade)
{
int index;
double max = 0.0;
for (index=0; index < vectgrade.size(); index++)
{
if (max<vectgrade[index])
max = vectgrade[index];
}
return max;
}
double find_lowest(vector<double> vectgrade)
{
int index;
double lowest=0.0;
for (index=0; index < vectgrade.size(); index++)
{
if (vectgrade[index]<=lowest)
lowest = vectgrade[index];
}
return lowest;
}
|