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?

A brief translation from C to C++...

#include <iostream>
#include <cstdlib>
#include <time>

void bubble_sort(int n,  int  z[])
{
     int i=0,
         j=0,
         x=0,
         k=0;
     
     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;
             }
          }
          
           cout << "Pass: " << i-1;

           for (k=1; k<=10; k++)
           {
               cout << z[k];
           }

           cout << endl;
     }
}          

int main()
{
    int i=0,
        k=0,
        number[11];
    
    srand(time(NULL));
    
    for (i=1; i<=8;i++)
    {
        number[i] = rand()%100;
    }

    cout << "------  before bubble_sort ------------------------------\n";

    for (k=1; k<=8; k++)
    {
        cout << number[k];
    }

    cout << endl << endl;

    bubble_sort(8, number);

    cout << "------ after bubble_sort ------------------------------\n";

    for (k=1; k<=8; k++) 
    { 
        cout << number [k]; 
    }

    cout << endl;    
        
}

2. How would I program this when I want to type numbers myself instead of random numbers.

If I do this, I would have done all of your homework for you. Give it a try yourself. Show us what you come up with.

well yeah that's what I thought it would be, a confirmation for my example would have sufficed.

If I do this, I would have done all of your homework for you. Give it a try yourself. Show us what you come up with.

if I would know how to do it or have an example, I'd wrote it here that's why I was asking.

I know that this here isn't needed

srand(time(NULL));
 
    for (i=1; i<=8;i++)
    {
        number[i] = rand()%100;
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.