Thread: Question understanding casted loop

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    187

    Question understanding casted loop

    Hello i got a problem in my code i did a bubble sort but i did it after trying some stuff i didnt understand what i wrote anyways i will post my code then i will ask my question
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #define NUM 10
    void put_random(int *array)
    {
         int i;
         for(i=0;i<10;i++) 
             array[i]=rand( ) % NUM;
    }
    void sort_it(int *array)
    {
         int i,x;
         for(i=0;i<NUM;i++) {
             for(x=0;x<NUM;x++) {
    			if(array[x]>array[x+1]){
                    int buffer;
    				buffer=array[x+1];
    				array[x+1]=array[x];
    				array[x]=buffer;
    			}
             }
         }
    }
    int main(void)
    {
        int array[10];
        int i;
        srand(time(NULL));
        put_random(array);
        puts("array before sorting\n");
        for(i=0;i<10;i++)
            printf("%d ",array[i]);
        puts("\n");
        sort_it(array);
        puts("Now after\n");
        for(i=0;i<10;i++) 
            printf("%d ",array[i]);
        getchar();
        return 0;
    }
    lets say we have in this 3 ints:
    in double for loop it will check like for example x if we did 3 nums it will check the num 3 times
    so in this example in the sort function downstairs for example in the check we will check array[x] > array[x+1]
    then we do a buffer to save in the x+1 then we switch x+1 =array[x] so for example we have 3,2,1
    so it will check first 3 3 times right ? like for example:
    buffer=2;
    array[x+1]=3;
    array[x]=2;

    so now will be like this 2,3,1

    now it will check again the 2
    is 2 > 3 no its not dont do anything now
    is 2 > 1 yes do now
    now it will be buffer[x+1] whish is 3 not 1 so how could it now be be fixed?
    does the casted for loop check the 3 with all numbers or how does it would it work..
    i know this is noob question but there some stuff in c i dont know how they operate even after doing c for
    2 month now but i wanna understand it so i dont be like those ppl who does code and dont understand what
    happens inside thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't know what you mean by "casted for loop" -- there are no casts in your code.

    Anyway, if you want to watch what's going on, sit down with a piece of paper and your array and do the loop yourself. You'll see how the array gets sorted. I can say this: no number gets checked three times in a row.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    srry i mean double for loop i forgot its name lol.i did write it in a paper but i m confused with what a double for loop does
    check above i know in the like lets say a double for loop like this it will do the num like 3 times like for example this code here
    Code:
    #include <stdio.h>
    int main(void)
    {
        int i;
        char x;
        for(i=0;i<=3;i++) {
            for(x='a';x<='c';x++) {
               printf("%d %c\n",i,x);
               }
            }
        return getchar();
    }
    it will print 0 3 times with each 0 a 0 b 0 c etc
    so i thought it would do the same whish will check each num in the row?
    Last edited by elwad; 05-22-2009 at 11:59 AM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you see any i's inside your inner for loop?

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    yes but whats the good of a double for loop in this case ?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    In this case, it controls how many times the inner loop runs. (If the inner loop only runs once, you won't get the entire array sorted. Again, do it yourself and you'll see why.)

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    yah i will write it in a paper and see i did a 1 for loop i got this as a result
    5 8 4 4 1 2 0 9 7 4
    5 4 4 1 2 0 8 7 4 9
    if i got any questions after i write hole thing i will post it back thanks alot for help i kinda get it but i still havent understood it 100 % lol i feel like an idiot

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    so in this example will for 10 * 10 will run the loop 100 times ? with the checking 100 times ?

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    like for this example here is for one for loop whish loop will run only once
    array= 20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1
    for(x to 20)
    for(x to 20)
    if(x bigger than x+1)
    saver variable=x+1;
    x+1=x
    x=saver variable


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


    but if it run 20 * 20 in this case a nested for loop will get a good result right ?

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by elwad View Post
    so in this example will for 10 * 10 will run the loop 100 times ? with the checking 100 times ?
    After bubbling the largest element to the highest index, the process (for loop) repeats all over again but with a reduced array size since the last element is sorted.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Beware, your sort function has a buffer overrun.
    The largest value of X during the loop is NUM-1, yet your if-statement compares item X+1, which is then at position NUM-1+1 = NUM i.e. position 10 of an array that only has valid indexes of 0 to 9.
    Consequently the sort will sometimes introduce garbage, dropping out one of the real items, and will potentially crash.

    Try only going up to x < NUM-1
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by elwad View Post
    like for this example here is for one for loop whish loop will run only once
    array= 20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1
    for(x to 20)
    for(x to 20)
    if(x bigger than x+1)
    saver variable=x+1;
    x+1=x
    x=saver variable


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


    but if it run 20 * 20 in this case a nested for loop will get a good result right ?
    At this point don't bother with the number of times it needs to loop over to get a good result.
    Bubble the largest element to the top after each iteration and repeat the whole process with a smaller array size.

  13. #13
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    Quote Originally Posted by iMalc View Post
    Beware, your sort function has a buffer overrun.
    The largest value of X during the loop is NUM-1, yet your if-statement compares item X+1, which is then at position NUM-1+1 = NUM i.e. position 10 of an array that only has valid indexes of 0 to 9.
    Consequently the sort will sometimes introduce garbage, dropping out one of the real items, and will potentially crash.

    Try only going up to x < NUM-1
    yah i was thinking about that 2 thanks.

  14. #14
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    Quote Originally Posted by itCbitC View Post
    At this point don't bother with the number of times it needs to loop over to get a good result.
    Bubble the largest element to the top after each iteration and repeat the whole process with a smaller array size.
    if i bubble the largest element and put in the end and get the smaller than it and put it in the end should it be a better sorting than this one or it will be the same ?

  15. #15
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by elwad View Post
    if i bubble the largest element and put in the end and get the smaller than it and put it in the end should it be a better sorting than this one or it will be the same ?
    IMO it may be better to first sketch out the algorithm before trying to codify it, as in
    Code:
    set k to zero
    while k is less than highest array index
        set i to zero
        repeat until i is less than highest array index minus k
            if a[i] is greather than a[i+1]
                swap the two
            increment i
        increment k

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop question
    By JuzMe in forum C++ Programming
    Replies: 11
    Last Post: 04-20-2009, 08:39 AM
  2. Loop question
    By kwood965 in forum C Programming
    Replies: 6
    Last Post: 10-29-2008, 11:12 PM
  3. simple for loop function question
    By felicityxiv in forum C Programming
    Replies: 7
    Last Post: 05-06-2006, 11:43 PM
  4. Please don't laugh...SIMPLE loop question!
    By the_lumin8or in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 01:08 PM
  5. for loop question
    By cdalten in forum C Programming
    Replies: 4
    Last Post: 03-20-2006, 09:42 AM