R
ritchie
Hi all!
Still working on this program!
Just to recap, I am writing a program to sort an array with four
different sort algorythms.
I am having a little trouble at the moment though!
Now, I am trying to calculate, with each sort, how many times during
the sort the array elements are compared and swapped.
I am coping the original array into a temp (iTmp) array to be sorted.
Then, I am using a menu to call each Sort function where I am passing
in iTmp plus the array's
size to be sorted.
-----------------------------------------
while ((iMenuChoice = menu()) != QUIT) // print menu & loop until
QUIT
switch( iMenuChoice ) {
case 1: // user enters 1: selection sort....
selectionSort( iTmpArr, SIZE );
break;
.
.
.
}
}
-----------------------------------------------
Within each sort function I call 'displaySorted', where I pass in
iTmp(copied array), array size, comparisons & swaps (calculated from
functions).
-------------------------------------
void displaySorted( int iTmpArr[], int iMax, int iComparison, int
iSwaps )
{
int i;
printf("Your Sorted Array:\n");
for(i=0; i<=MAX-1; i++ )
printf("%4d", iTmpArr);
printf("\n");
printf( "Comparisons: %d \n", iComparison );
printf( "Swaps: %d \n", iSwaps );
-----------------------------------------------------+
The problem though, is that each time I call the next sort from the
menu (while still in SWITCH statement until QUIT ) the comparisons and
swaps are being incorrectly calculated.
I know this is because the functions are getting the same array.
I am coping the original array into iTmp BEFORE the SWITCH which is
within the while loop.
Is there anywhere else I should be copying the arrays?
I tried to copy the arrays before each function call in the SWITCH but
this didn't work.
Anyone have any ideas?
Thanks in advance,
Ritchie
Still working on this program!
Just to recap, I am writing a program to sort an array with four
different sort algorythms.
I am having a little trouble at the moment though!
Now, I am trying to calculate, with each sort, how many times during
the sort the array elements are compared and swapped.
I am coping the original array into a temp (iTmp) array to be sorted.
Then, I am using a menu to call each Sort function where I am passing
in iTmp plus the array's
size to be sorted.
-----------------------------------------
while ((iMenuChoice = menu()) != QUIT) // print menu & loop until
QUIT
switch( iMenuChoice ) {
case 1: // user enters 1: selection sort....
selectionSort( iTmpArr, SIZE );
break;
.
.
.
}
}
-----------------------------------------------
Within each sort function I call 'displaySorted', where I pass in
iTmp(copied array), array size, comparisons & swaps (calculated from
functions).
-------------------------------------
void displaySorted( int iTmpArr[], int iMax, int iComparison, int
iSwaps )
{
int i;
printf("Your Sorted Array:\n");
for(i=0; i<=MAX-1; i++ )
printf("%4d", iTmpArr);
printf("\n");
printf( "Comparisons: %d \n", iComparison );
printf( "Swaps: %d \n", iSwaps );
-----------------------------------------------------+
The problem though, is that each time I call the next sort from the
menu (while still in SWITCH statement until QUIT ) the comparisons and
swaps are being incorrectly calculated.
I know this is because the functions are getting the same array.
I am coping the original array into iTmp BEFORE the SWITCH which is
within the while loop.
Is there anywhere else I should be copying the arrays?
I tried to copy the arrays before each function call in the SWITCH but
this didn't work.
Anyone have any ideas?
Thanks in advance,
Ritchie