hi I'm new
I got some questions about bubble-sort, so I registered here.
I'm currently working on this program (bubble-sort) so I looked up some example codes and I found this here, which is practically the right thing I'm looking for.
But I have some questions, like
1. How would printf("%2d. Pass: ", i-1); be with cout, since I'm not really familiar with printf as I always worked with cout.
my guess would be cout<<("Pass: ", i-1); but then there is no space between each number
printf("%3d", z[k]); I think it should be cout<<z[k];
2. How would I program this when I want to type numbers myself instead of random numbers.
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
void bubble_sort(int n, int z[])
{
int i,j,x,k;
for (i=2; i<=n; i++)
{
for (j=n; j>=i; j--)
if (z[j-1] > z[j])
{
x = z[j-1];
z[j-1] = z[j];
z[j] = x;
}
printf("%2d. Pass: ", i-1);
for (k=1; k<=10; k++)
printf("%3d", z[k]);
printf("\n");
}
}
int main()
{
int i,k,number[11];
srand(time(NULL));
for (i=1; i<=8;i++)
number[i] = rand()%100;
printf("------ before bubble_sort ------------------------------\n");
for (k=1; k<=8; k++)
printf("%3d", number [k]);
cout<<("\n\n");
bubble_sort(8, number);
printf("------ after bubble_sort ------------------------------\n");
for (k=1; k<=8; k++)
printf("%3d", number [k]);
cout<<("\n");
}
oh and I get 02293616 or sth. like that on each pass, how can I remove it?