getting problems by using vector in c++
Nov 11, 2014 at 1:34am UTC
hello guys, right now i get some problem in the vector. when i write the code below and run it, i can not get what i need.
the problem: 1)when i call the function of determin_grade, the number which is bigger than 100, always be included in the function. however, i do not want to include that number which is out the range(0 - 100).
2) when i calculate the average and try to find the highest, the number, which is out of range, also always be included in the function.
please help me fix these problems. thank you guys!!
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
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
void determin_grade(vector<double >);
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(grades);
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 > vect)
{
int index;
char grade;
int num_result=5;
cout<<"summary of the result: \n" ;
cout<<fixed<<showpoint<<setprecision(2);
for (index=0; index < vect.size(); index++)
{
cout<<"result " <<(index+1);
cout<<": " <<vect[index]<< " grade: " ;
if (vect[index] >=0 && vect[index]<50)
cout<< "grade = 'U'\n" ;
else if (vect[index]>=50 && vect[index]<60)
cout<< "grade = 'P'\n" ;
else if (vect[index]>=60 && vect[index]<70)
cout<< "grade = 'C'\n" ;
else if (vect[index]>=70 && vect[index]<90)
cout<< "grade = 'B'\n" ;
else if (vect[index]>=90 && vect[index]<=100)
cout<< "grade = 'A'\n" ;
else if (vect[index]<0 || vect[index]>100)
break ;
}
}
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=100.0;
for (index=0; index < vectgrade.size(); index++)
{
if (vectgrade[index]<lowest)
lowest = vectgrade[index];
}
return lowest;
}
Nov 11, 2014 at 3:13am UTC
branch (input> 100)
has no exit to prevent it from calling grades.push_back(input);
index
is also obsolete. Code can also be far more efficient if you'd make use of an array of pre-stored messages instead of a giant if else
construct
Topic archived. No new replies allowed.