# Question understanding casted loop

• 05-22-2009
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.
• 05-22-2009
tabstop
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.
• 05-22-2009
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?
• 05-22-2009
tabstop
Do you see any i's inside your inner for loop?
• 05-22-2009
yes but whats the good of a double for loop in this case ?
• 05-22-2009
tabstop
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.)
• 05-22-2009
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
• 05-22-2009
so in this example will for 10 * 10 will run the loop 100 times ? with the checking 100 times ?
• 05-22-2009
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 ?
• 05-22-2009
itCbitC
Quote:

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.
• 05-22-2009
iMalc
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
• 05-22-2009
itCbitC
Quote:

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.
• 05-22-2009
Quote:

Originally Posted by iMalc
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.
• 05-22-2009
Quote:

Originally Posted by itCbitC
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 ?
• 05-23-2009
itCbitC
Quote:

```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```