### Print highest and lowest+sort

So my problem here is I am trying to print each the highest and the lowest grades after the user inputs a grade for each. Then ill print the entire list unsorted and then sorted followed by the average and highest grade and lowest grade. Its been awhile but I think a nested loop here is needed but im not sure where to implement it or even how.
Here's what's actually done:
 ``1234567891011121314151617181920212223`` `````` #include using namespace std; int main() {int a[100]; int grades; int runtotal=0; cout<<"How many grades do you have?"<>grades; for (int x=1; x<=grades;x++) { cout<<"\nGrade on test "<>a[x]; runtotal= runtotal + a[x]; } cout<<"your average is: "<
Alright Ive made some progress but it gives me the wrong numbers, anyone could clarify this for me? What I put in comments was giving me problems and when I took it out the error was gone and it ran but the numbers come out wrong.

 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````include using namespace std; int main() {int a[100]; int grades; int runtotal=0; int y; cout<<"How many grades do you have?"<>grades; for (int x=1; x<=grades;x++) { cout<<"\nGrade on test "<>a[x]; //for(int y=1; ya[y+1]); } {int temp = a[y+1]; a[y+1] = a[y]; a[y] = temp; cout<<"The lowest grade is: "<
closed account (Dy7SLyTq)
a) `for(int y=1; y<y+1; y++); //line 17 ` there are two things wrong here. the first being y<y+1. thats an infinite loop. y will always be one less than y+1. secondly there is a semicolon at the end, cause for another infinite loop.

b) line 19) why? its pointless to have an if statement (in 90% of the cases, with this being one of them) that is followed immediately by a semicolon.

I think it maybe your "y" variable I don't see where you set it equal to anything.

P.S. oh, you did that in your commented out for loop.

Here is something I put together a long time ago to sort stuff for me.

 ``1234567891011121314151617181920212223242526`` ``````template unsigned ArraySize(A &input){ return sizeof(input)/sizeof(input[0]); } template void swap(B &input1,B &input2){ B temp = input1; input1 = input2; input2 = temp; } template void Sort(C &input){ for(unsigned min=0,max=ArraySize(input);mininput[j]?j:minDEX; maxDEX = input[maxDEX]

NOTE: this only works in C++ and not C
Last edited on
Alright All I'm trying to do now is to give out what the highest grade is and what the lowest grade is repeatedly throughout and once more at the end.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758`` ``````#include using namespace std; int main() {int a[101]; int grades; int runtotal=0; int temp; int high=0; int low=0; cout<<"How many grades do you have?"<>grades; for (int x=1; x<=grades;x++) { cout<<"\nGrade on test "<>a[x]; runtotal= runtotal + a[x]; } cout<<"\nThe unsorted list is:"; for(int x=1;x<=grades;x++) {cout<< a[x]<<", "; } for(int x=1; x < grades ; x++) { for(int index=1; index < grades ; index++) {if(a[index] > a[index+1]) { temp=a[index+1]; a[index+1]=a[index]; a[index]=temp; } } } cout<<"\nThe sorted list is:"; for(int x=1; x <= grades ; x++) {cout<< a[x]<<", "; } cout<<"\n"; cout<<"your average is: "<
Last edited on
Topic archived. No new replies allowed.