problem with the output...

Hw to change the output as below:

Enter integer:3
3
Enter integer:8
3
8
Enter integer:5
3
5
8


Tis is my coding:

#include <iostream>
using namespace std;
void bubblesort(int[],int);
int main(){
int num[5],x,i,y;
for( i=0;i<5;i++){
cout<<"Enter array index no. "<<i<<":";
cin>>num[i];}
bubblesort(num, 5);
for(int j = 0; j < 5; j++)
cout<<num[j]<<"\t";
return 0;
}
void bubblesort(int z[], int size)
{ int buffer;
for(int x = 0; x < 5; x++)
{ for(int y = x + 1; y < 5; y++)
{ if(z[x] > z[y])
{ buffer = z[x];
z[x] = z[y];
z[y] = buffer;
}
}
}
}
my output
Enter array index no. 0:5
Enter array index no. 1:9
Enter array index no. 2:3
Enter array index no. 3:2
Enter array index no. 4:8
2 3 5 8 9 Press any key to continue . . .



Last edited on
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 bubblesort(int[],int);

int main()
{
    int num[5],x,i,y;

    for( i=0;i<5;i++)
    {
        cout<<"Enter array index no. "<<i<<":";
        cin>>num[i];

        bubblesort(num, i+1);
        for(int j = 0; j < i+1; j++)
            //cout<<num[j]<<"\t";
            cout<<num[j]<<"\n";
    }

    //I put the following inside the previous
    //loop and did some slight modification

    /*************************
    bubblesort(num, 5);
    for(int j = 0; j < 5; j++)
        cout<<num[j]<<"\t";
    *************************/

    return 0;
}

void bubblesort(int z[], int size)
{
    int buffer;

    //for(int x = 0; x < 5; x++)
    for(int x = 0; x < size; x++)
    {
        //for(int y = x + 1; y < 5; y++)
        for(int y = x + 1; y < size; y++)
        {
            if(z[x] > z[y])
            {
                buffer = z[x];
                z[x] = z[y];
                z[y] = buffer;
            }
        }
    }
}

Other than that, good job! :)
Last edited on
thanks!
Topic archived. No new replies allowed.