printing array elements in ascending order

This is a discussion on printing array elements in ascending order within the C Programming forums, part of the General Programming Boards category; plz explain me if u can.........that what r the changes that should be made in my code above in those ...

  1. #16
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    plz explain me if u can.........that what r the changes that should be made in my code above in those 2 loops when the array elelments have to be printed in ascending as well as in descending order........
    so that i can know the logic behind that..........
    i'll be highly greatful to u........

  2. #17
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Okay...one last time I'll say this....
    Code:
    for(j=i-1;j<10;j++)
    You are counting too high. In the final pass of the loop, you will be comparing a[9] to a[9 +1] which doesn't exist. Array a only has elements [0..9]. That would be a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], and a[9]. As you can see...a[9 +1] (which is the same as a[10]) is NOT in that range.

    Also, I have NO idea why you went from for(j=j+1; j<10; j++) to for(j=i-1; j<10; j++).

    For each pass through the loop, you need to begin the bubble sort at the first element of the array which would be a[0]. So why are you trying to start the sort at a[i-1]? The initial value of j should be equivilent to the first element of the array.

    edit: Aaaah, I see why you tried j=i-1, but now you're mixing pieces from somebody else's sort method, with the bubble sort that you're trying to use. I recommend you stick with the bubble sort method. Other methods are better to sort arrays, but stick with the bubble sort method as a learning tool, until you see the logic behind it.
    Last edited by Scribbler; 10-24-2004 at 10:08 PM.

  3. #18
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    i really don't know im sorry to say this.........i know u might be getting frustrating by this.......but i have to give this assignment by today itself....and i really don't have too much of time left....but i am just trying to understand what is the logic behind using the required condition in a for loop....for which i m doing repeated mistakes.........i have tried everything to make my code work......but it's just out of my reach it seems now.........coz i just don't know how it's gonna be run.........so plz just give me the answer in short......for"for" loops.
    i think im grtting confused with the concepts of this incerasing and decreasing order and bubble sorting of for loops...........so could u just tell me what should be done "in this case" when i have to print in ascending order and when i'll have to print in descending order by using bubble sort. what will be the 2 "for" loops....and what does bubble sort do.....by using 2 loops...how do they actually work.....?

  4. #19
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Sorry, but one thing I will NOT do is give answers for assignments.

    If it's something you don't understand, I strongly urge you to discuss it with your teacher/instructor. Your instructor needs to be aware of what he/she may need to focus some attention for each student. And if I give you the answer, then you wouldn't learn anything.

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Give up. Noone wants to read that crap you call a post. If you ever decide to post on this board again, do the following:
    1) Go read the Announcements.
    2) Do as they instruct.
    3) Use complete sentences.
    4) Punctuate properly.
    5) Use whole words. Don't give us horribly abbreviated drivel.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #21
    Registered User
    Join Date
    Oct 2004
    Posts
    21
    These changes in the for loop will help.

    for( i=0; i< 10; i++){
    for( j=i; j < 10; j++) {
    if( a[i] > a[j]) {
    temp = a[j];
    a[j] = a[j+1];
    a[j+1] = temp;
    }
    }
    }

    PS: But, it is not the best sorting method.

  7. #22
    Registered User
    Join Date
    Oct 2004
    Posts
    21
    Give up. Noone wants to read that crap you call a post. If you ever decide to post on this board again, do the following:
    1) Go read the Announcements.
    2) Do as they instruct.
    3) Use complete sentences.
    4) Punctuate properly.
    5) Use whole words. Don't give us horribly abbreviated drivel.

    Quzah
    You seem more interested in non-technical help!!!!!

  8. #23
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by sangi
    You seem more interested in non-technical help!!!!!
    Aw, birds of a feather. Did you find a friend in stupidity?

    I see you're still not using code tags.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #24
    Registered User
    Join Date
    Oct 2004
    Posts
    21
    As the name of the board suggest, i would rather concentrate on technical help than giving suggestions on the style of postings.

  10. #25
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Quote Originally Posted by sangi
    These changes in the for loop will help.

    for( i=0; i< 10; i++){
    for( j=i; j < 10; j++) {
    if( a[i] > a[j]) {
    temp = a[j];
    a[j] = a[j+1];
    a[j+1] = temp;
    }
    }
    }

    PS: But, it is not the best sorting method.
    Despite not using code tags...you're wrong as well. This won't work. Try not to give the answer...especially if you don't know it.
    Last edited by Scribbler; 10-24-2004 at 11:57 PM.

  11. #26
    Registered User
    Join Date
    Oct 2004
    Posts
    21
    yaa. right. It won't work. sorry for that.
    The swap should be between a[j] and a[i].

  12. #27
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Still wrong. Then entire logic structure is wrong. Here's the output you get...

    Code:
    ENTER ARRAY ELELMENTS: 8 9 7 4 9 2 3 10 11 12
    SORTED ARRAY IS: 8 4 2 3 7 -1073745496 9 9 10 11
    Hint: The hilited output did not come from any element in the range a[0...9]. And they still aren't sorted properly for another reason.
    Last edited by Scribbler; 10-25-2004 at 12:07 AM.

  13. #28
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    so what it would be then.........every 1 is giving their differnet opinions.............could any 1 plz tell me here that what do we mean by bubble sort ......the actual implementation of the 2 for loops that r used in it...how do they actually work....?
    and how can we use the bubble sort in printing the array in ascending and descending order.............?plz plz help me out here with the concepts and logics here...........im really getting confused now!!!!!!!!!!!!!!!
    any kind of help would be really appreciated..................!!!!!!!!!!!!!!!!!

  14. #29
    Registered User
    Join Date
    Oct 2004
    Posts
    21
    This is the code which gives the correct results. I have tried this.
    Code:
    #include<stdio.h>
    int main(void)
    {
    int a[5],temp,i,j;
    printf("ENTER ARRAY ELELMENTS:");
    for(i=0;i<5;i++)
          scanf("%d",&a[i]);
    fflush(stdin);
    for(i=0;i<5;i++)
    {
            for(j=i;j<5;j++)
            {
                 if(a[i]>a[j])
                {
                  temp=a[j];
                  a[j]=a[i];
                  a[i]=temp;
                }
            }
    }
    printf("SORTED ARRAY IS:");
    for(i=0;i<5;i++)
    printf("%d ",a[i]);
    return 0;
    }

  15. #30
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Seriously...how can I get more specific without giving you the answer?

    THINK ABOUT IT.

    The only flaw is this line...
    Code:
    for(j=i-1;j<10;j++)
    And I've already told you 2 things...
    1) the initial value of j should point to the first element of the array, so j=i-1 is incorrect. The first time this for loop is executed, i-1 will equal 0, that is fine...however when i is 1, then j will equal 1 and a[0] will be overlooked on the second pass. On the third pass elements a[0] and a[1] will be overlooked. Think about it.

    and

    2) the loop is counting too high. In the final pass of this loop you will be comparing a[9] to a[9+1]. a[9+1] does NOT exist.

Page 2 of 2 FirstFirst 12
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, 10:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21