Bubble Sort Problem

Can't quite figure out why I keep getting this ridiculous number. If somebody can help it would be greatly appreciated thanks.

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
  #include <stdlib.h>
#include <stdio.h>


double inputData(double data[], int *pDataCount) {
	printf("Enter a double value: ");
	scanf("%lf", &data[*pDataCount]);
	(*pDataCount)++;
	return data[*pDataCount - 1];
}

void displayArray(double data[], int dataCount) {
	int i = 0;
	for (i = 0; i < dataCount; i++) {
		printf("%lf \n", data[i]);
	}

}

void sortArray(double data[], int dataCount){
	int i, j, length;
	double temp;

	length = dataCount;
	for ( j = (length - 1); j > 0; j--) {
		for (i = 0; i < dataCount; i++) {
			if (data[i] > data[i + 1]) {
				temp = data[i];
				data[i] = data[i+1];
				data[i + 1] = temp;
			
 			}
		}
		dataCount--;
	}
}

main() {
	double myArray[100], input;
	int dataCount = 0, arraySize, j;

	printf("How big is your array? ");
	scanf("%i", &arraySize);

	for (j = 0; j < arraySize; j++) {

		input = inputData(myArray, &dataCount);
	}

	sortArray(myArray, dataCount);
	displayArray(myArray, dataCount);

	system("Pause");

}
Nobody can help me out on this?
Sorry. Sometimes it takes people a few days to get to stuff.

Line 26 is wrong. You are using dataCount instead of the correct variable.
Hint: Do you see why that would cause a problem on line 27?

Once you fix that, notice that you are introducing a variable (length) that does nothing but pretend it is dataCount. Why not just use dataCount?

Hope this helps.
Topic archived. No new replies allowed.