Asalamo Alikom w rahmt Allah w baracato,
Hello Cool Dude,
there are logical error in your program in the second loop when you put the 
value of ( j variable) with 1 as there will be unnecessary swaps and 
Comparisons which results in error in result see this example
According to your program ( but instead of number 5 put 3 number only for easy)
3 2 1 to be sorted
so the first phase:
i=0,j=1  
2 3 1
i=0, j=2
1 2 3
second phase
i=1,j=1
1 2 3
i=1,j=2
1 2 3
Third phase ( error is in this phase as because of the value of j is 1 )
i=2 , j=1
1 3 2
i=2,j=2
1 3 2
so the program swap the array[i] with array[j] (where the initialization for j 
is 1)in the final phase which results in error 
so if you put the value of j with i+1 this will correct your program as there 
will not be any unnecessary swaps and comparisons
According to my point of view
j=i+1
so the first phase:

i=0,j=1  
2 3 1

i=0, j=2

1 2 3

second phase

i=1,j=2

1 2 3

so you save time as your program after correctness run two phases instead of 
three and the result is true.
thanks,
Eng A.Mahmoud

--- On Wed, 8/25/10, COOL DUDE <cooldude9...@hotmail.com> wrote:

From: COOL DUDE <cooldude9...@hotmail.com>
Subject: [c-prog] Help!!! :-(
To: c-prog@yahoogroups.com
Date: Wednesday, August 25, 2010, 11:03 PM







 



  


    
      
      
      

please help me for this program. i'm a beginner. For the below program tell me 
why it doesn't give the right output. Is there any problem in logic or loop i 
given.



please please please help me! :-( :-( :-(



/*write a program to sort five numbers in ascending order*/

/*********************************************************/



#include<iostream.h>

#include<conio.h>

void main()

{

    int array[5],i,j,temp=0;

    clrscr();

    cout<<"Enter any five number:\n";

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

    {

        cin>>array[i];

    }



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

    {

        for(j=1;j<5;j++)

        {

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

            {

                temp=array[i];

                array[i]=array[j];

                array[j]=temp;

            }

        }

    }



cout<<"The sorted array is:";

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

    {

        cout<<"\n"<<array[i];

    }

getch();

}                                         



[Non-text portions of this message have been removed]





    
     

    
    


 



  






      

[Non-text portions of this message have been removed]

Reply via email to