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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include<iostream>
 using namespace std;

int main()
 {int a[100];
 int grades;
 int runtotal=0;
 cout<<"How many grades do you have?"<<endl;
 cin>>grades;

for (int x=1; x<=grades;x++)
 {
     cout<<"\nGrade on test "<<x<<"?";
     cin>>a[x];
     runtotal= runtotal + a[x];
 }

cout<<"your average is: "<<runtotal/grades<<endl;
 cout<<"\n\n";

system("pause");
 return 0;
}
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.

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
include<iostream>
 using namespace std;

int main()
 {int a[100];
 int grades;
 int runtotal=0;
 int y;
 
 cout<<"How many grades do you have?"<<endl;
 cin>>grades;

for (int x=1; x<=grades;x++)
 {
     cout<<"\nGrade on test "<<x<<"?";
     cin>>a[x];
     //for(int y=1; y<y+1; y++);
    {
     if(a[y]>a[y+1]);
    }
        {int temp = a[y+1];

				a[y+1] = a[y];

				a[y] = temp;
				cout<<"The lowest grade is: "<<a[y] <<endl;
				cout<<"the Highest grade is: "<<a[y+1] <<endl;
        }

     runtotal= runtotal + a[x];
 }

cout<<"your average is: "<<runtotal/grades<<endl;
 cout<<"\n\n";

system("pause");
 return 0;
}
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.

finally... fix your formatting. its very hard to read your code
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.

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
template <typename A>
unsigned ArraySize(A &input){
	return sizeof(input)/sizeof(input[0]);
}

template <typename B>
void swap(B &input1,B &input2){
	B temp = input1;
	input1 = input2;
	input2 = temp;
}

template <typename C>
void Sort(C &input){
	for(unsigned min=0,max=ArraySize(input);min<max;min++){
		unsigned minDEX=min,maxDEX=--max;
		for(unsigned j=min;j<=max;j++){
			minDEX = input[minDEX]>input[j]?j:minDEX;
			maxDEX = input[maxDEX]<input[j]?j:maxDEX;
		}
		if(input[minDEX]==input[maxDEX])
			break;
		swap(input[min],input[minDEX]);
		swap(input[max],input[maxDEX==min?minDEX: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.

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
#include<iostream>

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?"<<endl;
 cin>>grades;

for (int x=1; x<=grades;x++)
 {
     cout<<"\nGrade on test "<<x<<"? ";
     cin>>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: "<<runtotal/grades<<endl;
 cout<<"\n\n";

system("pause");
 return 0;
}


Last edited on
Topic archived. No new replies allowed.