while,for causes

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

GRADE STUDENT
=======================

Student grade :
Student 1: 87
Student 2: 68
Student 3: 94
Student 4: 100
Student 5: 83

average 86.40
smallest Grade is 68
greatest Grade is 100

grade distribution:
0-19:
20-49:
50-79: *
80-99: ***
100: *

Anyone have a suggestion ??
Or even make me the whole programme ??
Last edited on
The suggestion is to post your attempt at this program, and we'll help from there.

We won't do your homework for you, since it benefits neither you nor us at all to do so.
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.
assume me as a very newbie to c++ so i hope you'll be very patient to answer my question

first about the min/max
this is what i concluded from your suggestion

int min;
int max;

min = 100
max = 0 \\because the grade is between them

cout << "input grade";
cin >> grade;

if (grade <= min)
min = grade
if (grade >= max)
max = grade

cout << "max is" << max;
cout << "min is" << min;

is that correct ?


for the histogram ( i dont really understand )
this is my conclusion

int histo_1;
int histo_2;
and so on

if ( grade <= 0 <= 19 ) \\this supposed to be 0-19 if i'm correct
histo_1 + 1

is it ?

still dont understand how to print stars based on how much the histo is ..

thx for your help but i still need a lot of it ..
oh ,, i forgot one thing .. I'm expected to use while/for command
Last edited on
min/max: you got it.

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

avg=sum/v.size();
cout<<"\n\naverage = "<<avg;
cout<<"\nmax= "<<max<<"\nmin= "<<min;

//now for displaying the number of stars for students

cout<<"\ngrade distribution"<<"\n0-19\n";
for(i=0;i<v.size();i++)
{

if(v[i]<=19)
cout<<"*";
}

cout<<"\n20-49\n";
for(i=0;i<v.size();i++)
{

if(v[i]>19&&v[i]<=49)
cout<<"*";
}


cout<<"\n50-79";
for(i=0;i<v.size();i++)
{

if(v[i]>49&&v[i]<=79)
cout<<"*";
}

cout<<"\n79-99";
for(i=0;i<v.size();i++)
{

if(v[i]>79&&v[i]<=99)
cout<<"*";
}

for(i=0;i<v.size();i++)
{

if(v[i]=100)
cout<<"*";
}


getch();
return 0;
}




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
could anyone post an array version of this prob ??
also an algo to delete the ( -1 ) input so it wont considered as a grade
Topic archived. No new replies allowed.