Your Ad Here

Thursday, February 26, 2009

Write a C++ program to sort a list of numbers in ascending order.

#include(iostream.h) // place this '<' & '>' instead of '(' & ')' before iostream.h
#include(conio.h)
void main( )
{
int a[10], n, i, j, temp;
clrscr( );
cout<<"Enter the no. of elements:";
cin>> n;
cout<<"Enter the array elements:";
for(i=0; i< n; i++)
cin>>a[i];
//------Operation part-------
for( i=0; i< n; i++)
{
for(j=i; j< n-1; j++)
{
if(a[i]> a[j+1] )
{
temp= a[i];
a[i]= a[j+1];
a[j+1]= temp;
}
}
}

cout<<"Elements after sorting:";
for( i=0; i< n; i++)
cin>> a[i];
getch ( );
}


Note
:- If u have any doubt regarding this program or logic, please feel free to contact me.

29 comments:

  1. its very useful for computer students thanks for your good job

    ReplyDelete
  2. thank u it was very helpfull

    ReplyDelete
  3. thanks alot, i still need to know functions you used like conio.h getch and clrscr

    ReplyDelete
    Replies
    1. conio.h is the header file where we have the functions like getch and clrscr.
      clrscr- is a function that is used to clear the screen after u come out of the program after seeing the output so next time wen u run the program u dont see previous output.
      getch - helps in displaying the output on the screen
      .

      Delete
  4. Hi there,
    For displaying the numbers in descending order is it ok if we change the last part of displaying in the program as below:
    for(i=n;i<0;i--)
    {
    cout<<a[i]
    }

    ReplyDelete
    Replies
    1. no just change this condition if(a[i]> a[j+1] ) to if(a[i]< a[j+1] ) this..!! u will get all the numbers in descending order..!!

      Delete
  5. give the code for arrange the given marks in ascending order using file operations in c++

    ReplyDelete
  6. thanks brooo.

    avoid clrscr() and
    change cin>> to cout<< in the last line.......errors!!!

    ReplyDelete
  7. is the prograM exceuing/ WORKING

    ReplyDelete
  8. Executed successfully............!!!!

    ReplyDelete
  9. BUGOOO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    MALI MAN UI!!!

    BUGO NA PROGRAMMER!!!!!!!!!!!!!!!!!

    ReplyDelete
  10. its simple n easy to undrstnd for beginers.. thank you

    ReplyDelete
  11. its simple n easy to undrstnd for beginers.. thank you

    ReplyDelete
  12. do you know how to program ascending in java way..

    ReplyDelete
  13. Hey dude great programme...
    But just one question...
    In the last for lop I guess it should be cout>>a[i]...
    Just check it out...

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. Why is it compulsory to have an array whose length is constant.
    Why to restrict the user to enter only 10 values. Can't there be run time initialization in array???

    int n;
    cin>>n;
    int arr[n];
    for(int i =0;i>n;i++)
    cin>>arr[i];


    Why this code isn't acceptable. What is the solution for such problem. kindly reply.

    ReplyDelete
  16. can we write
    void main()
    int main ()
    both toghether

    ReplyDelete
  17. what is the meaning of int a[10]

    ReplyDelete
  18. int a[10],temp;
    i could not understand this what doe this mean
    i would be very thankfull if someone clears my doubts

    ReplyDelete
  19. cout<<"enter the array elements:";
    what does this means and what are arrays?

    ReplyDelete
  20. what is meant by i++
    and can anyone explain me the logic please request
    my email id dhankhar7924@gmail.com

    ReplyDelete