Connecting Tech Pros Worldwide Help | Site Map

simple c array help needed

ritchie
Guest
 
Posts: n/a
#1: Nov 13 '05
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[i] = iArr[i];
}

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[i]);

printf("\n");
}

***********************end of code*************************
Mike Wahler
Guest
 
Posts: n/a
#2: Nov 13 '05

re: simple c array help needed


"ritchie" <ritchie_s01@yahoo.com> wrote in message
news:3bee6ba6.0311241406.2a588fc3@posting.google.c om...[color=blue]
> 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?[/color]

Yes it does. See below.
[color=blue]
> I have included the full program.
>
> Any ideas?
>
> Thanks in advance,
> Ritchie.
>
> *******************start code****************************
> #include<stdio.h>[/color]

#include <stdlib.h> /* MKW */
[color=blue]
> #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[i] = iArr[i];
> }
>
> 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[i]);
>
> printf("\n");
> }
>
> ***********************end of code*************************[/color]


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[i] = iArr[i];
}

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[i]); /* 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[i]); /* 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[i] < 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[i]);

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


ritchie
Guest
 
Posts: n/a
#3: Nov 13 '05

re: simple c array help needed


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" <mkwahler@mkwahler.net> wrote in message news:<iFwwb.13707$n56.12461@newsread1.news.pas.ear thlink.net>...[color=blue]
> "ritchie" <ritchie_s01@yahoo.com> wrote in message
> news:3bee6ba6.0311241406.2a588fc3@posting.google.c om...[color=green]
> > 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?[/color]
>
> Yes it does. See below.
>[color=green]
> > I have included the full program.
> >
> > Any ideas?
> >
> > Thanks in advance,
> > Ritchie.
> >
> > *******************start code****************************
> > #include<stdio.h>[/color]
>
> #include <stdlib.h> /* MKW */
>[color=green]
> > #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[i] = iArr[i];
> > }
> >
> > 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[i]);
> >
> > printf("\n");
> > }
> >
> > ***********************end of code*************************[/color]
>
>
> 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[i] = iArr[i];
> }
>
> 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[i]); /* 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[i]); /* 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[i] < 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[i]);
>
> 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[/color]
Mike Wahler
Guest
 
Posts: n/a
#4: Nov 13 '05

re: simple c array help needed


"ritchie" <ritchie_s01@yahoo.com> wrote in message
news:3bee6ba6.0311251032.7018c288@posting.google.c om...[color=blue]
> 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.[/color]

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).
[color=blue]
>
> The program works if the array is: 4,3,2,1 but if it is , 4,2,3,1
> there is a problem.[/color]

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


Al Bowers
Guest
 
Posts: n/a
#5: Nov 13 '05

re: simple c array help needed




Mike Wahler wrote:
[color=blue]
> "ritchie" <ritchie_s01@yahoo.com> wrote in message
> news:3bee6ba6.0311251032.7018c288@posting.google.c om...
>[color=green]
>>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.[/color]
>
>
> 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).
>
>[color=green]
>>The program works if the array is: 4,3,2,1 but if it is , 4,2,3,1
>>there is a problem.[/color]
>
>
> 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.
>[/color]

It is just a loop problem in function sort.
The function was written as:
[color=blue]
> 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;[/color]

[color=blue]
>/* ******* 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[i]); /* 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[i] < 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 >*/[/color]
[color=blue]
> if( iSmallestElement != iSortElement )[/color]


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.

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

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[i] = iArr[i];
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[i]); /* 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;
}
}
}




--
Al Bowers
Tampa, Fl USA
mailto: xabowers@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

ritchie
Guest
 
Posts: n/a
#6: Nov 13 '05

re: simple c array help needed


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
[color=blue]
>
> *******************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;[/color]
printf("Enter 4 integers to be sorted\n");
for(i=0; i<iMax; i++ )
scanf("%d ", &iArr[i]);
[color=blue]
> //copy iArr to iTmpArr
> for(i=0;i<MAX;i++)
> {
> iTmpArr[i] = iArr[i];
> }
>
> 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[i]);
>
> printf("\n");
> }
>
> ***********************end of code*************************[/color]
Closed Thread