Problems?!

Feb 14, 2012 at 1:40am
Input the scores, store in array, find the average of those scores, then display the scores out of the array in ascending order. everything works but the sort
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
#include<iostream>

using namespace std;

void sort(int* Array, int num) 
{
	int i, k;
	for(i=0; i< (num - 1); ++i)
	{
		for(k = i + 1; k > 0; --k)
		{
			if(Array[i] < Array[i-1])
			{
				
				int temp = Array[i];
				Array[i] = Array[i - 1];
				Array[i - 1] = temp;
			}
			
		}
	}
}


int main()
{
	int  i, scores, num, sum, avg, b=0; 
	int *Array;

	cout << "How many scores would you like to enter?" <<endl;
	cin >>num;
	Array = new int [num];
	
	for (i=0; i<num; i++)
		{
	cout << "Please enter score " <<endl;
	cin >> Array[i];
	sum = Array[i] + b;
	}


		avg=sum/num;

	cout<< "The average score is " << avg <<endl;
	sort(Array, num);
	cout << Array[i];
	

	system("pause");
	return 0;
}
Feb 14, 2012 at 1:49am
change the condition of 'k' in the 'for cicle': //(k=i+1;k>0;--k) ----> to ----> (k=i+1;k<num;k++)
and
1
2
3
4
5
6
7
8
9
10
11
12
13
//change this
if(Array[i] < Array[i-1]){
    int temp = Array[i];
    Array[i] = Array[i-1];
    Array[i - 1] = temp;
}

//to this
if(Array[i] > Array[k]){
    int temp = Array[i];
    Array[i] = Array[k];
    Array[k] = temp;
}
Last edited on Feb 14, 2012 at 1:54am
Feb 14, 2012 at 2:01am
Also your sum code wont work like you want it to.

At the moment the sum is equal to whatever the last number of the array entered was + b (b is always equal to 0 in this case).

Change line 38 to
sum += Array[i]

That will add each element of the array to the sum each time it loops.
Topic archived. No new replies allowed.