Hello Everyone,
I am trying to flowchart the following code but I am getting stuck on the nested loops. Can someone help me understand how the code flows.
Code:
#include<stdio.h>
#define SIZE 10
int main()
{
int a[ SIZE ] = { 10, 9, 8, 7 , 6, 5, 4, 3, 2, 1 }; /* Use initializer list to initialize array a */
int counter; /* Variable in which counter will be stored */
int pass; /* Variable in which pass will be stored */
int hold; /* Variable in which hold will be stored */
int swap; /* Variable in which swap will be stored */
printf( "Data items in original order\n" );
/* Output original array */
for( counter = 0; counter < SIZE; counter ++ ) {
printf( "%4d", a[ counter ] );
} /* End for */
printf( "\n\n" );
/* Bubble sort */
for( pass = 1; pass < SIZE; pass ++ ) {
swap = 0;
/* Loop to control number of comparisions per pass */
for( counter = 0; counter < SIZE - 1; counter ++ ) {
/* Compare adjacent elements and swap them if first
element is greater than second element */
if( a[ counter ] > a[ counter + 1 ] ) {
swap = 1;
hold = a[ counter ];
a[ counter ] = a[ counter + 1 ];
a[ counter + 1 ] = hold;
} /* End if */
} /* End for */
printf( "Pass %d: ", pass );
/* Loop to control if a swap has been made per pass */
for( counter = 0; counter <= SIZE - pass; counter ++ )
printf( " %d", a[ counter ]);
printf( "\n" );
if( swap != 1 )
break;
} /* End for */
printf( "\nData items in ascending order\n" );
/* Output sort array */
for( counter = 0; counter < SIZE; counter ++ ) {
printf( "%4d", a[ counter ] );
} /* End for */
printf( "\n" );
return 0; /* Indicates successful termination */
}
Thanks in advance
DMKanz07