Thread: printing array elements in ascending order

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    63

    printing array elements in ascending order

    hi
    could u plz help me out with this array problem...in which i have to print "array elements in ascending order"....while compilation there is no error but when i run the code it shows the output as -- 24768, which is very weird. i have wriiten the code like this:::


    #include<stdio.h>
    #include<conio.h>
    int main(void)
    {
    int a[10],temp,i,j;
    clrscr();
    printf("ENTER ARRAY ELELMENTS:");
    for(i=0;i<10;i++)
    scanf("%d",&a[i]);
    fflush(stdin);
    for(i=0;i<10;i++)
    {
    for(j=j+1;j<10;j++)
    {
    if(a[i]>a[i+1])
    {
    temp=a[i];
    a[i]=a[i+1];
    a[i+1]=temp;
    }
    }
    }
    printf("SORTED ARRAY IS:");
    for(i=0;i<10;i++)
    printf("%d",a[i]);
    getch();
    return 0;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I'm sorry, but your code just isn't what I've come to expect from a promising Cprog member such as yourself. I was expecting a program more like this:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      int array[10];
      int n, i, j;
    
      /* Fill the array */
      printf ( "Enter up to ten numbers: " );
      fflush ( stdout );
      for ( n = 0; n < 10; n++ ) {
        if ( scanf ( "%d", &array[n] ) != 1 )
          break;
      }
    
      /* Sort the array by insertion */
      for ( i = 1; i < n; i++ ) {
        int save = array[i];
    
        for ( j = i - 1; j >= 0 && array[j] > save; j-- )
          array[j + 1] = array[j];
    
        array[j + 1] = save;
      }
    
      /* Print the sorted array */
      for ( i = 0; i < n; i++ )
        printf ( "%d\n", array[i] );
    
      return 0;
    }
    I'm going to have to fail you for this post, hopefully that will encourage you to do better in the future.

    Your score: F
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    im sorry to say this but this code has been there in my book too.....it's correct ...i have checked that........so what's wrong in my code then........could u just tell me that........i hope u could make me....understand.......a bit better.......plz help me out with my code....the way i have written...........
    i'll be really greatful to u..........

  4. #4
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Code:
    if(a[i]>a[i+1])
    First of all, you should be subscripting with j instead of i since the comparison is inside a nested for loop, you're just comparing the same two array elements repeatedly. ie. you're comparing if (a[0] > a[0+1]) ten times, then looping to if (a[1] > a[1+1]) ten times.

    Code:
     for(j=j+1;j<10;j++)
    What happens when j == 9? you'll be comparing a[9] with a[9 + 1] which doesn't exist. And j=j+1...what is the initial value of j?
    Last edited by Scribbler; 10-24-2004 at 04:38 PM.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Quote Originally Posted by galmca
    im sorry to say this but this code has been there in my book too.....it's correct ...i have checked that........so what's wrong in my code then........could u just tell me that........i hope u could make me....understand.......a bit better.......plz help me out with my code....the way i have written...........
    i'll be really greatful to u..........
    Your punctuation-to-content ratio is way too high.

    >this code has been there in my book too
    May I suggest another book then?

    >it's correct
    Are you sure?

    >i have checked that
    Try again.

    >so what's wrong in my code then
    Would you like me to categorize and alphabetize by commonality and seriousness of the error?

    >#include<conio.h>
    Nonportable.

    >clrscr();
    Really really nonportable.

    >printf("ENTER ARRAY ELELMENTS:");
    You forgot to flush stdout so that non-Windows users can run your program. Though, since you use conio that isn't going to happen anyway, but you could at least pretend to care.

    >scanf("%d",&a[i]);
    Lack of error checking for an input function. Not only that, an input function that is notorious for being difficult to work with unless you have a PhD in scanf format modifiers.

    >fflush(stdin);
    Undefined behavior, fflush doesn't work with input streams.

    >for(j=j+1;j<10;j++)
    j is still uninitialized at this point, that's another check mark for undefined behavior. It's a good thing that undefined behavior doesn't accumulate nasal demons after the first offense.

    >printf("SORTED ARRAY IS:");
    You forgot to flush stdout again.

    >printf("%d",a[i]);
    And again.

    >getch();
    Nonportable.

    And your sorting algorithm is just plain wrong in many ways. Even if it weren't wrong I would complain about your use of such a horrible sorting algorithm when there are easier alternatives.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    oh........o.k......
    i got some hint what u want to sayabout j=j+1,,,,but still.......in that code.....what r the mistakes that i should not make..by writing those lines...could u just add those lines which i have been missingin that code and correct those lines which should not have been there.
    so that my code can work.........plz....

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    it's stilll giving the weird output --24768 when i run this code even after putting the necessary fflush(stdout) after every printf statement...............
    so what's wrong now....in that code?

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >so what's wrong now....in that code?
    I told you, the sorting algorithm is completely wrong. Start over and use the code I posted as a template. You should be using a decent insertion sort instead of that attempt at bubble sort anyway.
    My best code is written with the delete key.

  9. #9
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    As I mentioned before...

    Your counter is counting too high. At one point you're comparing a[9] with a[9+1] which doesn't exist. The array only defines a[0] through a[9].

  10. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    You've maybe read this before but here's again the solution to your and our problem...

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    so what do i do to make it correct then...?
    what should i place in that for loop of j then........?

  12. #12
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    could u plz make the changes in my code at the necessary places coz im unable to do so.....as i don't know what should be placed in those loops in order to make them work in a right manner......so that this program can run efficiently...........!!!!!

  13. #13
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    I'd recommend you backpedal to the beginning of your textbook and start from the beginning. It's painfully obvious you're trying to get ahead of yourself if you're not understanding the advice being given. Especially since there's errors in the most basic of principles and logic.

  14. #14
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    #include<stdio.h>
    #include<conio.h>
    int main(void)
    {
    int a[10],temp,i,j;
    clrscr();
    printf("ENTER ARRAY ELELMENTS:");
    for(i=0;i<10;i++)
    scanf("%d",&a[i]);
    fflush(stdin);
    for(i=1;i<10;i++)
    {
    for(j=i-1;j<10;j++)
    {
    if(a[j]>a[j+1])
    {
    temp=a[j];
    a[j]=a[j+1];
    a[j+1]=temp;
    }
    }
    }
    printf("SORTED ARRAY IS:");
    for(i=0;i<10;i++)
    printf("%d",a[i]);
    getch();
    return 0;
    }
    is that correct now..........?i have made few changes......and if it's not correct then i know that there must be some mistake in the for loop so just let me know how can i make it correct and if possible then plz explain me in brief..... but keeping the rest of a code as it is......waiting for ur reply..........

  15. #15
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    i have spent 6 hours now on this code only .........on understanding the logics behind this array and waiting for ur replies..........but im still not able to find out what's wrong now.....after making changes.......?plz let me know by making changes in my above code and also explaining thereby the working of how exactly these 2 for loops for i and j are working.......
    plz reply soon.. guys.......!!!!!!!!!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sort an Array in Ascending Order ?
    By Coding in forum C++ Programming
    Replies: 5
    Last Post: 01-09-2008, 08:32 PM
  2. Having trouble printing from an array
    By Sir Andus in forum C Programming
    Replies: 2
    Last Post: 10-30-2006, 01:48 PM
  3. Small problem with printing elements of an array
    By DLR in forum C Programming
    Replies: 17
    Last Post: 03-09-2006, 06:57 PM
  4. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM