simple c array help needed

R

ritchie

Hi,

I am writing to ask if anyone can see why my array is not being sorted
correctly?

It's an array of 4 elements(ints 1,2,3,4) but after calling the
selection sort
it comes back sorted as 1,1,2,4.

I have narrowed it down to the sort function. I'm almost positive
that the error lies within sort?
I have included the full program.

Any ideas?

Thanks in advance,
Ritchie.

*******************start code****************************
#include<stdio.h>
#define MAX 4

void sort(int [], int ); //sort function

main()
{
int iArr[MAX] = {1,2,3,4}; //original array
int iTmpArr[MAX]; // tmp array...for copying
// int iMenuChoice = 0;

int i;

//copy iArr to iTmpArr
for(i=0;i<MAX;i++)
{
iTmpArr = iArr;
}

sort(iTmpArr, MAX); //call selection sort
}

void sort( int iTmpArr[], int iMax)
{
int i, iSmallestElement, iSortElement, iTemp;

printf("\n****************Selection Sort****************\n");

for( iSortElement=0; iSortElement <iMax-1; iSortElement++ )
{
iSmallestElement = iSortElement;
for( i=iSortElement +1; i <= iMax; i++ )
{
if( iTmpArr[ i ] < iTmpArr[ iSmallestElement ] )
iSmallestElement = i;
if( iSmallestElement != iSortElement )
{
iTemp = iTmpArr[ iSmallestElement ];
iTmpArr[ iSmallestElement ] = iTmpArr[ iSortElement ];
iTmpArr[ iSortElement ] = iTemp;
}
}
}
printf("Sorted array\n");
for(i=0; i<iMax;i++)
printf("%d ", iTmpArr);

printf("\n");
}

***********************end of code*************************
 
M

Mike Wahler

ritchie said:
Hi,

I am writing to ask if anyone can see why my array is not being sorted
correctly?

It's an array of 4 elements(ints 1,2,3,4) but after calling the
selection sort
it comes back sorted as 1,1,2,4.

I have narrowed it down to the sort function. I'm almost positive
that the error lies within sort?

Yes it does. See below.
I have included the full program.

Any ideas?

Thanks in advance,
Ritchie.

*******************start code****************************
#include<stdio.h>

#include said:
#define MAX 4

void sort(int [], int ); //sort function

main()
{
int iArr[MAX] = {1,2,3,4}; //original array
int iTmpArr[MAX]; // tmp array...for copying
// int iMenuChoice = 0;

int i;

//copy iArr to iTmpArr
for(i=0;i<MAX;i++)
{
iTmpArr = iArr;
}

sort(iTmpArr, MAX); //call selection sort
}

void sort( int iTmpArr[], int iMax)
{
int i, iSmallestElement, iSortElement, iTemp;

printf("\n****************Selection Sort****************\n");

for( iSortElement=0; iSortElement <iMax-1; iSortElement++ )
{
iSmallestElement = iSortElement;
for( i=iSortElement +1; i <= iMax; i++ )
{
if( iTmpArr[ i ] < iTmpArr[ iSmallestElement ] )
iSmallestElement = i;
if( iSmallestElement != iSortElement )
{
iTemp = iTmpArr[ iSmallestElement ];
iTmpArr[ iSmallestElement ] = iTmpArr[ iSortElement ];
iTmpArr[ iSortElement ] = iTemp;
}
}
}
printf("Sorted array\n");
for(i=0; i<iMax;i++)
printf("%d ", iTmpArr);

printf("\n");
}

***********************end of code*************************



The standard response to a query such as yours is "watch it
with a debugger." If you don't have, or can't or won't use
one, you can still do rudimentary debugging by outputting
values of pertinent variables at strategic locations in the code.

Here is your code with such output statements included, which
is what I used to watch your variables, and thus locate the
problem (all my changes/additions are marked with /* MKW */) :

(also your code was not indented, I don't know if this was
intentional or if your newsreader mangled it, I used indentation
to make the code more readable -- a GREAT aid for debugging).

#include <stdio.h>
#define MAX 4

void sort(int [], int ); //sort function

int main(void) /* MKW fixed return type */
{
/* int iArr[MAX] = {1,2,3,4}; //original array */ /* MKW removed */

/* MKW make original nonsorted */
int iArr[MAX] = {4,3,2,1}; /* MKW */

int iTmpArr[MAX]; // tmp array...for copying
// int iMenuChoice = 0;

int i;

//copy iArr to iTmpArr
for(i=0;i<MAX;i++)
{
iTmpArr = iArr;
}

sort(iTmpArr, MAX); //call selection sort
return 0; /* MKW main() must return an int */
}

void show_array(int *array, size_t elems) /* MKW */
{
size_t i = 0; /* MKW */
puts("array contents:"); /* MKW */

for(i = 0; i < elems; ++i) /* MKW */
printf("array[%d] == %d\n", i, array); /* MKW */

putchar('\n'); /* MKW */
}

void sort( int iTmpArr[], int iMax)
{
int i, iSmallestElement, iSortElement, iTemp;

iTemp = 0; /* MKW */
printf("\n****************Selection Sort****************\n");

for( iSortElement=0; iSortElement <iMax-1; iSortElement++ )
{
printf("iSortElement == %d\n", iSortElement); /* MKW */

iSmallestElement = iSortElement;


/* ******* HERE IS THE PROBLEM ****** */
/* for( i=iSortElement +1; i <= iMax; i++ ) /* /* MKW removed */
for( i=iSortElement +1; i < iMax; i++ ) /* MKW */

{
printf("i == %d\n", i); /* MKW */

printf("iSmallestElement == %d\n", /* MKW */
iSmallestElement); /* MKW */

printf("iTmpArr[%d] == %d\n", /* MKW */
i, /* MKW */
iTmpArr); /* MKW */

printf("iTmpArr[%d] == %d\n", /* MKW */
iSmallestElement, /* MKW */
iTmpArr[iSmallestElement]); /* MKW */

printf("iTmpArr[%d] < iTmpArr[%d] == %s\n", /* MKW */
i, /* MKW */
iSmallestElement, /* MKW */
iTmpArr < iTmpArr[iSmallestElement] /* MKW */
? "true" : "false"); /* MKW */

if( iTmpArr[ i ] < iTmpArr[ iSmallestElement ] )
iSmallestElement = i;

printf("iSmallestElement == %d\n", /* MKW */
iSmallestElement); /* MKW */

printf("iSortElement == %d\n", /* MKW */
iSortElement); /* MKW */

printf("iSmallestElement != iSortElement == %s\n", /* MKW */
iSmallestElement != iSortElement /* MKW */
? "true" : "false"); /* MKW */

if( iSmallestElement != iSortElement )
{
iTemp = iTmpArr[ iSmallestElement ];
iTmpArr[ iSmallestElement ] = iTmpArr[ iSortElement ];
iTmpArr[ iSortElement ] = iTemp;
}

printf("iSortElement == %d\n", iSortElement); /* MKW */
printf("iSmallestElement == %d\n", iSmallestElement); /* MKW
*/
printf("iTemp == %d\n", iTemp); /* MKW */

show_array(iTmpArr, iMax); /* MKW */
printf(("Press return")); /* MKW */
fflush(stdout); /* MKW */
getchar(); /* MKW */
putchar('\n'); /* MKW */
}
}
printf("Sorted array\n");

for(i=0; i<iMax;i++)
printf("%d ", iTmpArr);

printf("\n");
}




Output (including debug output) after my fix:

****************Selection Sort****************
iSortElement == 0
i == 1
iSmallestElement == 0
iTmpArr[1] == 3
iTmpArr[0] == 4
iTmpArr[1] < iTmpArr[0] == true
iSmallestElement == 1
iSortElement == 0
iSmallestElement != iSortElement == true
iSortElement == 0
iSmallestElement == 1
iTemp == 3
array contents:
array[0] == 3
array[1] == 4
array[2] == 2
array[3] == 1

Press return

i == 2
iSmallestElement == 1
iTmpArr[2] == 2
iTmpArr[1] == 4
iTmpArr[2] < iTmpArr[1] == true
iSmallestElement == 2
iSortElement == 0
iSmallestElement != iSortElement == true
iSortElement == 0
iSmallestElement == 2
iTemp == 2
array contents:
array[0] == 2
array[1] == 4
array[2] == 3
array[3] == 1

Press return

i == 3
iSmallestElement == 2
iTmpArr[3] == 1
iTmpArr[2] == 3
iTmpArr[3] < iTmpArr[2] == true
iSmallestElement == 3
iSortElement == 0
iSmallestElement != iSortElement == true
iSortElement == 0
iSmallestElement == 3
iTemp == 1
array contents:
array[0] == 1
array[1] == 4
array[2] == 3
array[3] == 2

Press return

iSortElement == 1
i == 2
iSmallestElement == 1
iTmpArr[2] == 3
iTmpArr[1] == 4
iTmpArr[2] < iTmpArr[1] == true
iSmallestElement == 2
iSortElement == 1
iSmallestElement != iSortElement == true
iSortElement == 1
iSmallestElement == 2
iTemp == 3
array contents:
array[0] == 1
array[1] == 3
array[2] == 4
array[3] == 2

Press return

i == 3
iSmallestElement == 2
iTmpArr[3] == 2
iTmpArr[2] == 4
iTmpArr[3] < iTmpArr[2] == true
iSmallestElement == 3
iSortElement == 1
iSmallestElement != iSortElement == true
iSortElement == 1
iSmallestElement == 3
iTemp == 2
array contents:
array[0] == 1
array[1] == 2
array[2] == 4
array[3] == 3

Press return

iSortElement == 2
i == 3
iSmallestElement == 2
iTmpArr[3] == 3
iTmpArr[2] == 4
iTmpArr[3] < iTmpArr[2] == true
iSmallestElement == 3
iSortElement == 2
iSmallestElement != iSortElement == true
iSortElement == 2
iSmallestElement == 3
iTemp == 3
array contents:
array[0] == 1
array[1] == 2
array[2] == 3
array[3] == 4

Press return

Sorted array
1 2 3 4



I did not do any other testing of your sort, so it might or
might not be fully correct yet. I mainly wanted to show you
one way of tracking down a problem.

HTH,
-Mike
 
R

ritchie

Hi Mike,

Thanks for the response.

I did try debugging the program before but got lost somewhere in the
middle.
I had the code indented but it must have got messed up along the way.

The program works if the array is: 4,3,2,1 but if it is , 4,2,3,1
there is a problem.

Thanks again for the help,
Ritchie


Mike Wahler said:
ritchie said:
Hi,

I am writing to ask if anyone can see why my array is not being sorted
correctly?

It's an array of 4 elements(ints 1,2,3,4) but after calling the
selection sort
it comes back sorted as 1,1,2,4.

I have narrowed it down to the sort function. I'm almost positive
that the error lies within sort?

Yes it does. See below.
I have included the full program.

Any ideas?

Thanks in advance,
Ritchie.

*******************start code****************************
#include<stdio.h>

#include said:
#define MAX 4

void sort(int [], int ); //sort function

main()
{
int iArr[MAX] = {1,2,3,4}; //original array
int iTmpArr[MAX]; // tmp array...for copying
// int iMenuChoice = 0;

int i;

//copy iArr to iTmpArr
for(i=0;i<MAX;i++)
{
iTmpArr = iArr;
}

sort(iTmpArr, MAX); //call selection sort
}

void sort( int iTmpArr[], int iMax)
{
int i, iSmallestElement, iSortElement, iTemp;

printf("\n****************Selection Sort****************\n");

for( iSortElement=0; iSortElement <iMax-1; iSortElement++ )
{
iSmallestElement = iSortElement;
for( i=iSortElement +1; i <= iMax; i++ )
{
if( iTmpArr[ i ] < iTmpArr[ iSmallestElement ] )
iSmallestElement = i;
if( iSmallestElement != iSortElement )
{
iTemp = iTmpArr[ iSmallestElement ];
iTmpArr[ iSmallestElement ] = iTmpArr[ iSortElement ];
iTmpArr[ iSortElement ] = iTemp;
}
}
}
printf("Sorted array\n");
for(i=0; i<iMax;i++)
printf("%d ", iTmpArr);

printf("\n");
}

***********************end of code*************************



The standard response to a query such as yours is "watch it
with a debugger." If you don't have, or can't or won't use
one, you can still do rudimentary debugging by outputting
values of pertinent variables at strategic locations in the code.

Here is your code with such output statements included, which
is what I used to watch your variables, and thus locate the
problem (all my changes/additions are marked with /* MKW */) :

(also your code was not indented, I don't know if this was
intentional or if your newsreader mangled it, I used indentation
to make the code more readable -- a GREAT aid for debugging).

#include <stdio.h>
#define MAX 4

void sort(int [], int ); //sort function

int main(void) /* MKW fixed return type */
{
/* int iArr[MAX] = {1,2,3,4}; //original array */ /* MKW removed */

/* MKW make original nonsorted */
int iArr[MAX] = {4,3,2,1}; /* MKW */

int iTmpArr[MAX]; // tmp array...for copying
// int iMenuChoice = 0;

int i;

//copy iArr to iTmpArr
for(i=0;i<MAX;i++)
{
iTmpArr = iArr;
}

sort(iTmpArr, MAX); //call selection sort
return 0; /* MKW main() must return an int */
}

void show_array(int *array, size_t elems) /* MKW */
{
size_t i = 0; /* MKW */
puts("array contents:"); /* MKW */

for(i = 0; i < elems; ++i) /* MKW */
printf("array[%d] == %d\n", i, array); /* MKW */

putchar('\n'); /* MKW */
}

void sort( int iTmpArr[], int iMax)
{
int i, iSmallestElement, iSortElement, iTemp;

iTemp = 0; /* MKW */
printf("\n****************Selection Sort****************\n");

for( iSortElement=0; iSortElement <iMax-1; iSortElement++ )
{
printf("iSortElement == %d\n", iSortElement); /* MKW */

iSmallestElement = iSortElement;


/* ******* HERE IS THE PROBLEM ****** */
/* for( i=iSortElement +1; i <= iMax; i++ ) /* /* MKW removed */
for( i=iSortElement +1; i < iMax; i++ ) /* MKW */

{
printf("i == %d\n", i); /* MKW */

printf("iSmallestElement == %d\n", /* MKW */
iSmallestElement); /* MKW */

printf("iTmpArr[%d] == %d\n", /* MKW */
i, /* MKW */
iTmpArr); /* MKW */

printf("iTmpArr[%d] == %d\n", /* MKW */
iSmallestElement, /* MKW */
iTmpArr[iSmallestElement]); /* MKW */

printf("iTmpArr[%d] < iTmpArr[%d] == %s\n", /* MKW */
i, /* MKW */
iSmallestElement, /* MKW */
iTmpArr < iTmpArr[iSmallestElement] /* MKW */
? "true" : "false"); /* MKW */

if( iTmpArr[ i ] < iTmpArr[ iSmallestElement ] )
iSmallestElement = i;

printf("iSmallestElement == %d\n", /* MKW */
iSmallestElement); /* MKW */

printf("iSortElement == %d\n", /* MKW */
iSortElement); /* MKW */

printf("iSmallestElement != iSortElement == %s\n", /* MKW */
iSmallestElement != iSortElement /* MKW */
? "true" : "false"); /* MKW */

if( iSmallestElement != iSortElement )
{
iTemp = iTmpArr[ iSmallestElement ];
iTmpArr[ iSmallestElement ] = iTmpArr[ iSortElement ];
iTmpArr[ iSortElement ] = iTemp;
}

printf("iSortElement == %d\n", iSortElement); /* MKW */
printf("iSmallestElement == %d\n", iSmallestElement); /* MKW
*/
printf("iTemp == %d\n", iTemp); /* MKW */

show_array(iTmpArr, iMax); /* MKW */
printf(("Press return")); /* MKW */
fflush(stdout); /* MKW */
getchar(); /* MKW */
putchar('\n'); /* MKW */
}
}
printf("Sorted array\n");

for(i=0; i<iMax;i++)
printf("%d ", iTmpArr);

printf("\n");
}




Output (including debug output) after my fix:

****************Selection Sort****************
iSortElement == 0
i == 1
iSmallestElement == 0
iTmpArr[1] == 3
iTmpArr[0] == 4
iTmpArr[1] < iTmpArr[0] == true
iSmallestElement == 1
iSortElement == 0
iSmallestElement != iSortElement == true
iSortElement == 0
iSmallestElement == 1
iTemp == 3
array contents:
array[0] == 3
array[1] == 4
array[2] == 2
array[3] == 1

Press return

i == 2
iSmallestElement == 1
iTmpArr[2] == 2
iTmpArr[1] == 4
iTmpArr[2] < iTmpArr[1] == true
iSmallestElement == 2
iSortElement == 0
iSmallestElement != iSortElement == true
iSortElement == 0
iSmallestElement == 2
iTemp == 2
array contents:
array[0] == 2
array[1] == 4
array[2] == 3
array[3] == 1

Press return

i == 3
iSmallestElement == 2
iTmpArr[3] == 1
iTmpArr[2] == 3
iTmpArr[3] < iTmpArr[2] == true
iSmallestElement == 3
iSortElement == 0
iSmallestElement != iSortElement == true
iSortElement == 0
iSmallestElement == 3
iTemp == 1
array contents:
array[0] == 1
array[1] == 4
array[2] == 3
array[3] == 2

Press return

iSortElement == 1
i == 2
iSmallestElement == 1
iTmpArr[2] == 3
iTmpArr[1] == 4
iTmpArr[2] < iTmpArr[1] == true
iSmallestElement == 2
iSortElement == 1
iSmallestElement != iSortElement == true
iSortElement == 1
iSmallestElement == 2
iTemp == 3
array contents:
array[0] == 1
array[1] == 3
array[2] == 4
array[3] == 2

Press return

i == 3
iSmallestElement == 2
iTmpArr[3] == 2
iTmpArr[2] == 4
iTmpArr[3] < iTmpArr[2] == true
iSmallestElement == 3
iSortElement == 1
iSmallestElement != iSortElement == true
iSortElement == 1
iSmallestElement == 3
iTemp == 2
array contents:
array[0] == 1
array[1] == 2
array[2] == 4
array[3] == 3

Press return

iSortElement == 2
i == 3
iSmallestElement == 2
iTmpArr[3] == 3
iTmpArr[2] == 4
iTmpArr[3] < iTmpArr[2] == true
iSmallestElement == 3
iSortElement == 2
iSmallestElement != iSortElement == true
iSortElement == 2
iSmallestElement == 3
iTemp == 3
array contents:
array[0] == 1
array[1] == 2
array[2] == 3
array[3] == 4

Press return

Sorted array
1 2 3 4



I did not do any other testing of your sort, so it might or
might not be fully correct yet. I mainly wanted to show you
one way of tracking down a problem.

HTH,
-Mike
 
M

Mike Wahler

ritchie said:
Hi Mike,

Thanks for the response.

I did try debugging the program before but got lost somewhere in the
middle.
I had the code indented but it must have got messed up along the way.

I thought that might have been the case. E.g. If your code
contains tabs, they'll often be mangled or omitted by a
newsreader. Change any tabs to sequences of spaces before
posting (many/most editors have an option to do this).
The program works if the array is: 4,3,2,1 but if it is , 4,2,3,1
there is a problem.

Then you've still some more work to do. :)

I still think a good way to track your problem is
to inspect the array periodically during the sort,
as I did in my example.

Post back if you're still stuck.

Good luck!

-Mike
 
A

Al Bowers

Mike said:
I thought that might have been the case. E.g. If your code
contains tabs, they'll often be mangled or omitted by a
newsreader. Change any tabs to sequences of spaces before
posting (many/most editors have an option to do this).




Then you've still some more work to do. :)

I still think a good way to track your problem is
to inspect the array periodically during the sort,
as I did in my example.

Post back if you're still stuck.

It is just a loop problem in function sort.
The function was written as:
> void sort( int iTmpArr[], int iMax)
>{
> int i, iSmallestElement, iSortElement, iTemp;
>
> iTemp = 0; /* MKW */
> printf("\n****************Selection Sort****************\n");
>
> for( iSortElement=0; iSortElement <iMax-1; iSortElement++ )
> {
> printf("iSortElement == %d\n", iSortElement); /* MKW */
> iSmallestElement = iSortElement;

>/* ******* HERE IS THE PROBLEM ****** */
>/* for( i=iSortElement +1; i <= iMax; i++ ) /* /* MKW removed */
> for( i=iSortElement +1; i < iMax; i++ ) /* MKW */
>
> {
> printf("i == %d\n", i); /* MKW */
>
> printf("iSmallestElement == %d\n", /* MKW */
> iSmallestElement); /* MKW */
>
> printf("iTmpArr[%d] == %d\n", /* MKW */
> i, /* MKW */
> iTmpArr); /* MKW */
>
> printf("iTmpArr[%d] == %d\n", /* MKW */
> iSmallestElement, /* MKW */
> iTmpArr[iSmallestElement]); /* MKW */
>
> printf("iTmpArr[%d] < iTmpArr[%d] == %s\n", /* MKW */
> i, /* MKW */
> iSmallestElement, /* MKW */
> iTmpArr < iTmpArr[iSmallestElement] /* MKW */
> ? "true" : "false"); /* MKW */
>
>
> if( iTmpArr[ i ] < iTmpArr[ iSmallestElement ] )
> iSmallestElement = i;
>
> printf("iSmallestElement == %d\n", /* MKW */
> iSmallestElement); /* MKW */
>
> printf("iSortElement == %d\n", /* MKW */
> iSortElement); /* MKW */
>
> printf("iSmallestElement != iSortElement == %s\n", /* MKW >*/
> iSmallestElement != iSortElement /* MKW >*/
> ? "true" : "false"); /* MKW >*/

> if( iSmallestElement != iSortElement )


Here is the problem. This if statement should not be in this
second(nested) for loop. It should be in the first for loop.
See the correction below.

> {
> iTemp = iTmpArr[ iSmallestElement ];
> iTmpArr[ iSmallestElement ] = iTmpArr[ iSortElement ];
> iTmpArr[ iSortElement ] = iTemp;
> }
> printf("iSortElement == %d\n", iSortElement); /* MKW */
> printf("iSmallestElement == %d\n", iSmallestElement); /*
>MKW
>*/
> printf("iTemp == %d\n", iTemp); /* MKW */
> show_array(iTmpArr, iMax); /* MKW */
> printf(("Press return")); /* MKW */
> fflush(stdout); /* MKW */
> getchar(); /* MKW */
> putchar('\n'); /* MKW */
> }
> }
> printf("Sorted array\n");
> for(i=0; i<iMax;i++)
> printf("%d ", iTmpArr);

> printf("\n");
>}
\*******************************************************/

The corrected version:

#include <stdio.h>

#define MAX 4

void sort(int [], int ); //sort function
void show_array(int *array, size_t elems);

int main(void) /* MKW fixed return type */
{
int iArr[MAX] = {4,2,3,1}; /* MKW */
int iTmpArr[MAX]; // tmp array...for copying
int i;

for(i=0;i<MAX;i++)
iTmpArr = iArr;
sort(iTmpArr, MAX); //call selection sort
show_array(iTmpArr,MAX);
return 0; /* MKW main() must return an int */
}

void show_array(int *array, size_t elems) /* MKW */
{
size_t i = 0; /* MKW */
puts("array contents:"); /* MKW */

for(i = 0; i < elems; ++i) /* MKW */
printf("array[%d] == %d\n", i, array); /* MKW */
putchar('\n'); /* MKW */
}

void sort( int iTmpArr[], int iMax)
{
int i, iSmallestElement, iSortElement, iTemp;

for( iSortElement=0; iSortElement <iMax-1; iSortElement++ )
{
iSmallestElement = iSortElement;
for( i=iSortElement +1; i < iMax; i++ ) /* MKW */
{
if( iTmpArr[ i ] < iTmpArr[ iSmallestElement ] )
iSmallestElement = i;
}
if( iSmallestElement != iSortElement )
{
iTemp = iTmpArr[ iSmallestElement ];
iTmpArr[ iSmallestElement ] = iTmpArr[ iSortElement ];
iTmpArr[ iSortElement ] = iTemp;
}
}
}
 
R

ritchie

Hi,

Thanks to all in the group that replied and helped me with this.

I have been hearing a lot about dynamic memory allocation.

I have looked into this a bit but was wondering if anyone knew for my
basic program (below), would this program perform better.. with
dynamically allocated memory for the array rather than '#define' 'ing
the size of the array?

If I was passing the array to a function to be sorted how would I pass
the size of the array ? As it is I '#define MAX 4' and pass MAX as
the array's size into the function.
But how would this work with dynamic mem.....?

From what i've read I think i'd need a variable for the number of
elements:
ell_amt = sizeof( int ), and then malloc ell-amt?
I know of the realloc() function but i'm not sure how i'd implemnt
this to allocate memory for each new element ( using the scanf ).

Any ideas?

(CODE BELOW)--------
Thanks again,
Ritchie
*******************start code****************************
#include<stdio.h>
#define MAX 4

void sort(int [], int ); //sort function

main()
{
int iArr[MAX]; //original array
int iTmpArr[MAX]; // tmp array...for copying
// int iMenuChoice = 0;

int i;
printf("Enter 4 integers to be sorted\n");
for(i=0; i<iMax; i++ )
scanf("%d ", &iArr);
//copy iArr to iTmpArr
for(i=0;i<MAX;i++)
{
iTmpArr = iArr;
}

sort(iTmpArr, MAX); //call selection sort
}

void sort( int iTmpArr[], int iMax)
{
int i, iSmallestElement, iSortElement, iTemp;

printf("\n****************Selection Sort****************\n");

for( iSortElement=0; iSortElement <iMax-1; iSortElement++ )
{
iSmallestElement = iSortElement;
for( i=iSortElement +1; i <= iMax; i++ )
{
if( iTmpArr[ i ] < iTmpArr[ iSmallestElement ] )
iSmallestElement = i;
if( iSmallestElement != iSortElement )
{
iTemp = iTmpArr[ iSmallestElement ];
iTmpArr[ iSmallestElement ] = iTmpArr[ iSortElement ];
iTmpArr[ iSortElement ] = iTemp;
}
}
}
printf("Sorted array\n");
for(i=0; i<iMax;i++)
printf("%d ", iTmpArr);

printf("\n");
}

***********************end of code*************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,102
Messages
2,570,645
Members
47,247
Latest member
GabrieleL2

Latest Threads

Top