Bubble array

Can you Help me with Bubble array please.

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
  #include<iostream>
using namespace std;
int main()
{
	{
		int a[20], i, j, temp;
		for (i = 1; i <= 6; i++)
				{
					cout << "Enter the " << i << " no.to sort:";
					cin >> a[i];
				}
				for (i = 1; i < 6; i++)
				{
					for (j = 1; j < 6; j++)
					{
						if (a[i] > a[i + 1]) {
							temp = a[i];
							a[i] = a[i + 1];
							a[i + 1] = temp;
						}
					}
				}
				for (i = 1; i < 6; i++)
					cout << a[i] << endl;
				system("PAUSE");
				return 0;
			}
	}
This is a function for bubble sort. You need to provide it an array and the array's size.

Try to separate your functions from the main, it's a good programming habit.

Also, it's not safe to use system("PAUSE"), use something like getchar();.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void toBeSorted(int arr[], int size)
{
	bool swap = false;            
	int temp, bottom = size - 1;
	do
	{
		for (int i = 0; i < size; i++)
		{
			for (int k = 0; k < bottom; k++)
			{
				if (arr[k] > arr[k + 1])
				{
					temp = arr[k];
					arr[k] = arr[k + 1];
					arr[k + 1] = temp;
					swap = true;
				}
			}
		}
		bottom--;
	} while (swap != true);

}
Use j inside the inner loop not i.
this what I've done so far
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
#include<iostream>
using namespace std;
int main()
{
	void toBeSorted(int arr[], int size);
	{
		bool swap = false;
		int i, k, temp, bottom = 5 - 1, arr[20];
		while (swap != false)
		{


			{
				for (int i = 0; i < 5; i++)
				{
					cout << "enter no." << i << "/n";
					cin >> arr[k];
					for (int i = 0; i < 5; i++)
					{
						for (int k = 0; k < bottom; k++)
						{
							if (arr[k] > arr[k + 1])
							{
								temp = arr[k];
								arr[k] = arr[k + 1];
								arr[k + 1] = temp;
								swap = true;
							}
						}
					}
					cout << "Result" << arr[k] << endl;
				} while (swap != true);
			}
			getchar();
			return 0;
		}
	}
}

I want to cin numbers and also display the pass process the errors I get are "Warning C4101 'i': unreferenced local variable" and "Error C4700 uninitialized local variable 'k' used"
Last edited on
It seems to me that k isn't accessible inside that for loop. I don't understand why, though. I guess you could create a new variable k?

EDIT:

I get it now. On lines 16 and 17, k does not have any value. the variable k needs to be initialized to be used.
Last edited on
I mean I base my program out of this
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
#include<iostream>
using namespace std;
int main()
{
	int a[20], i, j, temp;
	for (i = 0; i < 10; i++)
	{
		cout << "Enter the " << i << " no.to sort:";
		cin >> a[i];
	}
	for (i = 0; i < 10; i++)
	{
		for (j = 0; j < 10; j++)
		{
			if (a[i] < a[j])
			{
				temp = a[i];
				a[i] = a[j];
				a[j] = temp;
			}
		}
	}
	for (i = 0; i < 10; i++)
		cout << a[i] << endl;
	return 0;
}

but I want to display the passes it have done for example:

Array at beginning: 84 69 76 86 94 91
After Pass #1: 84 76 86 94 91 69
After Pass #2: 84 86 94 91 76 69
After Pass #3: 86 94 91 84 76 69
After Pass #4: 94 91 86 84 76 69
After Pass #5 (done): 94 91 86 84 76 69

like that
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
#include <iostream>
#include <Windows.h>
using namespace std;

int main()
{
       char op;
       int swapHolder = -1;
       int End = 9;
       int Length = 10;

       int MyArray[] = {44 , 13 , 7 , 19 , 554 , 875 , 254 , 1011 , 87};

       for(int counter = Length - 1; counter > 0; counter--)
        {
            for(int i = 0; i < End; i++)
            {
                if(MyArray[i] > MyArray[i + 1])
                {
                    swapHolder = MyArray[i + 1];
                    MyArray[i + 1] = MyArray[i];
                    MyArray[i] = swapHolder;
                }
            }

           // Output the result
             for(int i = 0; i < 9; i++)
            {

                cout << MyArray[i] << " ,";

            }
            End--;
            cout << endl;
        }


        cout << endl << endl;
        cout << "Press [y] to Hide all process: " << endl;
        cin >> op;

        system("cls");

        if(op == 'Y' || op == 'y')
        {

            // Output the result
                for(int i = 0; i < 9; i++)
                {
                    cout << MyArray[i] << " ,";
                }
                End--;
                cout << endl;
        }

    }
[/code][/code]
Last edited on
Hope it help you
why does it show -858993460 ?? on the pass?
Sorry!
didnt understand can you explain more?
Topic archived. No new replies allowed.