Reoccurring number?

So I've been trying to write this code, which does some pretty straightforward things like find the average and minimum number, but I can't seem to get it running despite checking it over multiple times. Could anyone let me know what I'm doing wrong?

Output:

Enter value:
31
14
63
24
-999
Unsorted list of numbers: 31 14 63 24
Average: 33
Unsorted list of numbers: -858993460 14 24 31
Average: -2.14748e+008
Largest number: -858993460
Smallest number: -858993460
Range: 0


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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  //Aidan Satterwhite
//Assignment 1

#include <iostream>
using namespace std;

void input(int ulist[30], int& n);
void Bubblesort(int ulist[30], int slist[30], int n);
void print(int list[30], int n);
void minMaxRange(int ulist[30], int n);
double average;
int n, sum;

void main(){
	int ulist[30], slist[30];

	input(ulist, n);
	print(ulist, n);
	Bubblesort(ulist, slist, n);
	print(slist, n);
	minMaxRange(ulist, n);
	cin >> n;
}

void input(int ulist[30], int& n){
	int i(0), value;

	cout << "Enter value: " << endl;
	cin >> value;

	while (i < 29 && value != -999){
		i++;
		ulist[i] = value;
		if (i < 29){
			cin >> value;
		}
	}

	n = i;
}

void Bubblesort(int ulist[30], int slist[30], int n){
	int i, j, temp;

	for (i = 1; i <= n; i++){
		slist[i] = ulist[i];
	}
	for (j = 1; j <= n; j++){
		for (i = 1; i <= n; i++){
			if (slist[i] > slist[i + 1]){
				temp = slist[i];
				slist[i] = slist[i + 1];
				slist[i + 1] = temp;
			}
		}
	}
}

void print(int list[30], int n){
	int i;
	sum = 0;

	cout << "Unsorted list of numbers: ";
	
	for (i = 1; i <= n; i++){
		cout << list[i] << " ";
		sum = sum + list[i];
	}

	average = sum / n;
	cout << endl;
	cout << "Average: " << average << endl;
}

void minMaxRange(int ulist[30], int n){
	int i, max = ulist[0], min = ulist[0], range;

	for (i = 1; i <= n; i++){
		if (ulist[i] < min){
			ulist[i] = min;
		}
		if (ulist[i] > max){
			ulist[i] = max;
		}
	}

	range = max - min;

	cout << "Largest number: " << max << endl;
	cout << "Smallest number: " << min << endl;
	cout << "Range: " << range << endl;
}
for (i = 1; i <= n; i++){Array indices starts from 0 and last valid index is size - 1.
Canonical for loop for looping over all array elements is for(int i = 0; i < size; ++i)
I see that you already got an answer, but also I'd recommend you to avoid using bubble sort, it's average case complexity is O(n^2) and it will slow down your program most of the time. Try using Merge Sort, Heap Sort or Quick Sort with median pivot instead, they got O(n log n) average complexity.
Ok, I changed it, but now the output's even more off than before:

Enter value:
32
44
1
94
72
-999
Unsorted list of numbers: -858993460 32 44 1 94 72
Average: -1.71799e+008
Unsorted list of numbers: -858993460 -858993460 1 32 44 72
Average: -3.43597e+008
Largest number: -858993460
Smallest number: -858993460
Range: 0
How did you change it?
I changed all of the for(i=1;i-=n;i++) to for(i=0;i<-n;i++).
i<-n What? is this? Why i is les than negative n?
Sorry, I mean i<=n
Why i<=n? You have just n elements, so element a[n] does not exist at all. Last element is a[n-1]. Look again on canonical loop I posted. Loop for iterating over whole array looks exactly like that. Every character is there for a reason.
I was trying to make it so that the value -999 was not included in the "Unsorted list of numbers" section.
I was trying to make it so that the value -999 was not included in the "Unsorted list of numbers" section.
Just decreas array size by 1 so it would not be grabbed by loop
Topic archived. No new replies allowed.