Basic Programming-Array

Discussion in 'C' started by Niceman, Nov 25, 2010.

  1. Niceman

    Niceman New Member

    Joined:
    Nov 25, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi All,

    I'm new to "C" Learning basic Programmes(& also in this forum) in C.

    I wrote a program Ascending order of an Array.

    unfortunately its not working. I believe logic is correct, Is there any thing I'm missing in it :(


    Code:
    void main()
    {
    int a[5],i,j,temp;
    clrscr();
    printf("\nenter 5 no.'s:);
    for(i=0;i<5;i++)
    scanf("%d",&a[i]);
    
    for(i=0;i<5;i++)
    {
        for(j=i+1;j<5;j++)
         {
               if(a[i]>a[j])
               temp=a[i];
               a[j]=a[i];
               a[j]=temp;
           }
    }
    
    printf("\nAscending no.'s:);
    for(i=0;i<5;i++)
    printf("\n%d",a[i]);
    
    getch();
    }
    
    Now running prog.:

    entering no
    11
    5
    9
    3
    2
    giving me:
    2
    3
    9
    11
    11

    Can someone please tell me what is it I'm missing..Any file to include is there any logic going wrong?
     
  2. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    http://www.techgeek.in
    try this:-

    Code:
    void main()
    {
    int a[5],i,j,temp;
    clrscr();
    printf("\nenter 5 no.'s:);
    for(i=0;i<5;i++)
    scanf("%d",&a[i]);
    
    for(i=0;i<5;i++)
    {
        for(j=i+1;j<5-i;j++)  //i made change here...
         {
               if(a[i]>a[j])
               temp=a[i];
               a[j]=a[i];
               a[j]=temp;
           }
    }
    
    printf("\nAscending no.'s:);
    for(i=0;i<5;i++)
    printf("\n%d",a[i]);
    
    getch();
    }
    
    
    
    
     
  3. sanket

    sanket New Member

    Joined:
    Nov 27, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    The correct code for arrenging an array in asendind order is given below:-
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
     int a[5],i,j,temp;
     clrscr();
     printf("\nenter 5 no.'s:");
     for(i=0;i<5;i++)
      scanf("%d",&a[i]);
     for(i=0;i<5;i++)
     {
      for(j=i+1;j<5;j++)
      {
       if(a[i]>a[j])
       {
          temp=a[i];
          a[i]=a[j];
          a[j]=temp;
       }
      }
     }
     printf("\nAscending no.'s:");
     for(i=0;i<5;i++)
      printf("\n%d",a[i]);
     getch();
    }
    Your logic is correct but while swapping the numbers you did the wrond logic...
     
  4. go4expert

    go4expert Moderator

    Joined:
    Aug 3, 2004
    Messages:
    306
    Likes Received:
    9
    Trophy Points:
    0
    As Sanket pointed out you are swapping the numbers wrongly.

    You are doing
    Code:
               temp=a[i];
               a[j]=a[i];
               a[j]=temp;
    Where as your a remains unchanged. You should use what Sanket has as swapping logic
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice