I have to write a program that that reads an array of integer numbers, stores them in array, sort the numbers in ascending order, and print the sorted array: using the buble sort technique.

Unfortunately I can't seem to get it to work at all, and I know <stdio.h> is probably not efficient but thats what we're supposed to use. This is my first programming class, and any help would be greatly appreciated.


I'm sorry its so horribly written:

#include <stdio.h>
#define N 5

int main ()
{
    int array[N], temp, i, j;
    
    
    printf("please enter 5 numbers to be sorted\n");
    
    
    scanf("%i \n", &array[i]);
       
        
        for(i=0; i<N; i++ ) 
                 {for(j=0; j < N-1; j++){
                        if (array[j] > array[j+1])
                        {temp=array[j];
                        array[j]=array[j+1];
                        array[j+1]=temp;
                                        }
                 
                                         }
                       printf("%i\n", array[i]);
                        }
                   
                   
                   
                   
                      
     
    
    return 0;
    
    
    
    
}

Recommended Answers

All 5 Replies

Why are you printing in the middle of the sort? Don't you need to sort then print the sorted list?

I'm sorry its so horribly written:

Then read this and don't write horrible code anymore :icon_wink:

here i have sorted array to ascending order :)

#include <iostream.h>
#include <conio.h>

const int n=5;

void main(){

	int a[n],l, j, t;

   cout <<  "Enter the no. to be sorted in ascending order.";
    		for (l=0; l<n; l++)
         	cin >> a[l];

	for(l=0; l<n; l++)
   	for(j=l; j<n; j++)
      	if(a[l] > a[j])
          {
         	t=a[l];
            a[l]=a[j];
            a[j]=t;
           }
                  cout << "sorted array is ";
         for(l=0; l<n; l++)
          cout << a[l]  ;
      
getch();
}
commented: Poorly formatted non-compliant code. Plus, you did the OP's homework for them, big no-no. -3

is my print still in the wrong loop?
cause, it still doesn't seem to be printing any results at all, and i'm not sure I'm following the loop correctly...

#include <stdio.h>

#define N 5



int main ()

{

    int array[N], temp, i, j;  //temp is the place holder for the numbers as it sorts them

    

    
    

    	printf("please enter 5 numbers to be sorted\n");

    
	for (i=0; i<n; i++)

                  scanf("%i \n", &array[i]);  //defines i

       

        

        for(i=0; i<N; i++ )  // sets i as a number entered

                 {for(j=i; j < N; j++){        // the program sorts through numbers within a loop overuntil its in order

                        if (array[i] > array[j])

                        {temp=array[i];

                        array[i]=array[j];

                        array[j]=temp;

                                       }  

                 

                                         }

                       

                        }

                   

                   

                   

                   

                      

     

 printf("%i\n", array[i]);    // prints the final results

    return 0;

    

    

    

    

}

look at your loop. Its not being update.

for(i=0; i<N-1; i++ ) { {for(j=i; j < N; j++){

and at last while you trying to execute the sorted value it also should be loop.

for(i=0; i<N; i++)

    printf("%i ", array[i]); // prints the final results

is my print still in the wrong loop?
cause, it still doesn't seem to be printing any results at all, and i'm not sure I'm following the loop correctly...

Come on!!! Format your code properly! Still can't read it.

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.