I have a problem with my programs
here is what should be done
Make a programme to find average, min and max grade from a serial ( unlimited ) grade of student ..
Input ended if the grade inputted is -1
Here is what the output should be like ..
I have to use ‘while’ or ‘for’ command
i have done the output/input for student grade ..
but cant find a way the way to find min/max input (grade) .. because the number of student is unlimited ...
and I dont have a clue to print out the grade distribution .. (based from the student grade)
just give me the algorithms .. and I'll try to make the programs .. then i'll post it here again after I have the algo/pseudo
In this case, finding the minimum and maximum is easy.
Keep two variables -- minimum_score and maximum_score. Initialize minimum_score to some
really big number and maximum_score to some really small number. Every time you read a
grade from input, see if the grade you just read is greater than the maximum or less than the
minimum and update your variables accordingly.
For the histogram, you need to keep an array of 5 elements. The zero-th element counts the
number of grades that were in the range 0-19, and so forth. Each time you read a grade,
increment the appropriate element in the array.
Printing out the correct number of stars is then easy.
histo: have you studied arrays? If so, use them, otherwise you can do histo_1, histo_2, etc.; it's just it will be more typing that way. Your if is wrong --- you want if( grade >= 0 && grade <= 19 ). Otherwise you got it there too.
If the value of histo_1 is 10 after you're done reading input, then print 10 stars. If it's 9, print 9 stars, etc. Use a
for() loop for that. for( int num_stars = 0; num_stars < histo_1; num_stars++ ), for example
hello well i hope you have learned vectors and so I am using vectors.they are the dynamic arrays and hence memory can be allocated at the run time. If you have not learned vectors then this can be done using pointers and using new and delete.
here is the code using vectors
#include <iostream>
#include <vector> //for using vector and its related functions
#include <conio.h>
using namespace std;
int main()
{
int i=1,grade,sum=0,avg;
vector<int> v;
do
{
cout<<"\nenter the grade of student "<<i;
cin>>grade;
v.push_back(grade);
i++;
}while(grade!=-1);
v.pop_back(); //to delete the element storing value -1
int max=v[0];
int min=v[0];
//avg calculation begins
for(int i=0;i<v.size();i++)
{
sum=sum+v[i];
if(v[i]>max)
{
max=v[i];
}
else if(v[i]<min)
{
min=v[i];
}
}//end of for
I hope this will work somwhere u might get some simple errors because I have not tried it in a compiler. but do remember that the compiler must include all the new features added i.e it should have standard template library. thats a must because then only vectors will work