Bubble Sort Algo

The errors i get is:
Error C2109 subscript requires array or pointer type

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

int main()
{
	int  count, itemCount, i, x, l, swap;
	cout << "********************************************\n";
	cout << "Welcome to Bubble Sorting Algorithm System\n";
	cout << "********************************************\n";
	cout << "Enter how many number[s] you wish to sort randomly: \n";
	cin >> count;
	int *array = new int[count];
	cout << "Enter the value: ";
	for (int i = 0; i < count; i++)
	{
		cin >> itemCount[i];
	}
	cout << "Data entered by user is: ";
	for (int x = 0; x < count; x++)
	{
		cout << "\tValue at " << x << "Index: " << itemCount[x]<< endl;
	} 
	cout << endl;
	int swap;
	for (int k = 0; k <= count; k++)
	{
		for (int x = 0; x <= count; x++)
		{
			if (itemCount[x] > itemCount[x + 1])
			{
				swap = itemCount[x];
				itemCount[x] = itemCount[x + 1];
				itemCount[x + 1] = swap;
			}
		}
	}
	cout << "\n********************************************\n" << "\t\t\Bubble sort are: " << "\n********************************************\n";
	for (int l = 1; l < count; l++)
	{
		if (itemCount[l])
		{
				cout << "\tValue at " << l << "Index: " << itemCount[i] << endl;
		}
	}
	return 0;
	}



Every time you used itemCount you should have used array.
I have corrected my code. But the problem is, why i get the output for sorted bubble at value at 0 index : -85......

this are my codes.
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
#include <iostream>
using namespace std;

int main()
{
	int  count, itemCount[10000], swap;
	cout << "********************************************\n";
	cout << "Welcome to Bubble Sorting Algorithm System\n";
	cout << "********************************************\n";
	cout << "Enter how many number[s] you wish to sort randomly: \n";
	cin >> count;
	int *array = new int[count];
	cout << "Enter the value: ";
	for (int i = 0; i < count; i++)
	{
		cin >> itemCount[i];
	}
	cout << "Data entered by user is: \n";
	for (int x = 0; x < count; x++)
	{
		cout << "\tValue at " << x << " Index: " << itemCount[x] << endl;
	}
	cout << endl;

	for (int i2 = 0; i2 < count; i2++)
	{
		for (int x = 0; x < count; x++)
		{
			if (itemCount[x] > itemCount[x + 1])
			{
				swap = itemCount[x];
				itemCount[x] = itemCount[x + 1];
				itemCount[x + 1] = swap;
			}
		}
	}
	cout << "\n********************************************\n" << "\t\t\Bubble sort are: " << "\n********************************************\n";
	for (int i3 = 0; i3 < count; i3++)
	{
		cout << "\tValue at " << i3 << " Index: " << itemCount[i3] << endl;
	}
	return 0;
}


Your code breaks if the user enters a number greater than 10000.

In some places you access itemCount[x + 1], but x is allowed to be count - 1.
Topic archived. No new replies allowed.