473,748 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

simple c array help needed

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, iSmallestElemen t, iSortElement, iTemp;

printf("\n***** ***********Sele ction Sort*********** *****\n");

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

printf("\n");
}

*************** ********end of code*********** **************
Nov 13 '05 #1
5 2217
"ritchie" <ri*********@ya hoo.com> wrote in message
news:3b******** *************** ***@posting.goo gle.com...
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 <stdlib.h> /* MKW */
#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, iSmallestElemen t, iSortElement, iTemp;

printf("\n***** ***********Sele ction Sort*********** *****\n");

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

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[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, iSmallestElemen t, iSortElement, iTemp;

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

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

iSmallestElemen t = 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("iSmalle stElement == %d\n", /* MKW */
iSmallestElemen t); /* MKW */

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

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

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

if( iTmpArr[ i ] < iTmpArr[ iSmallestElemen t ] )
iSmallestElemen t = i;

printf("iSmalle stElement == %d\n", /* MKW */
iSmallestElemen t); /* MKW */

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

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

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

printf("iSortEl ement == %d\n", iSortElement); /* MKW */
printf("iSmalle stElement == %d\n", iSmallestElemen t); /* MKW
*/
printf("iTemp == %d\n", iTemp); /* MKW */

show_array(iTmp Arr, 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
iSmallestElemen t == 0
iTmpArr[1] == 3
iTmpArr[0] == 4
iTmpArr[1] < iTmpArr[0] == true
iSmallestElemen t == 1
iSortElement == 0
iSmallestElemen t != iSortElement == true
iSortElement == 0
iSmallestElemen t == 1
iTemp == 3
array contents:
array[0] == 3
array[1] == 4
array[2] == 2
array[3] == 1

Press return

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

Press return

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

Press return

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

Press return

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

Press return

iSortElement == 2
i == 3
iSmallestElemen t == 2
iTmpArr[3] == 3
iTmpArr[2] == 4
iTmpArr[3] < iTmpArr[2] == true
iSmallestElemen t == 3
iSortElement == 2
iSmallestElemen t != iSortElement == true
iSortElement == 2
iSmallestElemen t == 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
Nov 13 '05 #2
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" <mk******@mkwah ler.net> wrote in message news:<iF******* ************@ne wsread1.news.pa s.earthlink.net >...
"ritchie" <ri*********@ya hoo.com> wrote in message
news:3b******** *************** ***@posting.goo gle.com...
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 <stdlib.h> /* MKW */
#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, iSmallestElemen t, iSortElement, iTemp;

printf("\n***** ***********Sele ction Sort*********** *****\n");

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

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[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, iSmallestElemen t, iSortElement, iTemp;

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

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

iSmallestElemen t = 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("iSmalle stElement == %d\n", /* MKW */
iSmallestElemen t); /* MKW */

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

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

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

if( iTmpArr[ i ] < iTmpArr[ iSmallestElemen t ] )
iSmallestElemen t = i;

printf("iSmalle stElement == %d\n", /* MKW */
iSmallestElemen t); /* MKW */

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

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

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

printf("iSortEl ement == %d\n", iSortElement); /* MKW */
printf("iSmalle stElement == %d\n", iSmallestElemen t); /* MKW
*/
printf("iTemp == %d\n", iTemp); /* MKW */

show_array(iTmp Arr, 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
iSmallestElemen t == 0
iTmpArr[1] == 3
iTmpArr[0] == 4
iTmpArr[1] < iTmpArr[0] == true
iSmallestElemen t == 1
iSortElement == 0
iSmallestElemen t != iSortElement == true
iSortElement == 0
iSmallestElemen t == 1
iTemp == 3
array contents:
array[0] == 3
array[1] == 4
array[2] == 2
array[3] == 1

Press return

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

Press return

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

Press return

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

Press return

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

Press return

iSortElement == 2
i == 3
iSmallestElemen t == 2
iTmpArr[3] == 3
iTmpArr[2] == 4
iTmpArr[3] < iTmpArr[2] == true
iSmallestElemen t == 3
iSortElement == 2
iSmallestElemen t != iSortElement == true
iSortElement == 2
iSmallestElemen t == 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

Nov 13 '05 #3
"ritchie" <ri*********@ya hoo.com> wrote in message
news:3b******** *************** ***@posting.goo gle.com...
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
Nov 13 '05 #4


Mike Wahler wrote:
"ritchie" <ri*********@ya hoo.com> wrote in message
news:3b******** *************** ***@posting.goo gle.com...
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.


It is just a loop problem in function sort.
The function was written as:
void sort( int iTmpArr[], int iMax)
{
int i, iSmallestElemen t, iSortElement, iTemp;

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

for( iSortElement=0; iSortElement <iMax-1; iSortElement++ )
{
printf("iSortEl ement == %d\n", iSortElement); /* MKW */
iSmallestElemen t = 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("iSmalle stElement == %d\n", /* MKW */
iSmallestElemen t); /* MKW */

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

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

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

printf("iSmalle stElement == %d\n", /* MKW */
iSmallestElemen t); /* MKW */

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

printf("iSmalle stElement != iSortElement == %s\n", /* MKW >*/
iSmallestElemen t != iSortElement /* MKW >*/
? "true" : "false"); /* MKW >*/ if( iSmallestElemen t != 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[ iSmallestElemen t ];
iTmpArr[ iSmallestElemen t ] = iTmpArr[ iSortElement ];
iTmpArr[ iSortElement ] = iTemp;
} printf("iSortEl ement == %d\n", iSortElement); /* MKW */
printf("iSmalle stElement == %d\n", iSmallestElemen t); /*
MKW
*/
printf("iTemp == %d\n", iTemp); /* MKW */ show_array(iTmp Arr, 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");
}

\************** *************** *************** ***********/

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(iTmp Arr,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, iSmallestElemen t, iSortElement, iTemp;

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


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

Nov 13 '05 #5
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[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, iSmallestElemen t, iSortElement, iTemp;

printf("\n***** ***********Sele ction Sort*********** *****\n");

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

printf("\n");
}

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

Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
14340
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? Basically: "Press any key to continue..." I beleive that I am looking for is something along the lines of a....
5
7245
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files, two aspects which I want to incorporate into my program eventually. That aside, my most pressing problem right now is how to get rid of the newline in the input when I use fgets(). Now I have looked around on the net, not so much in this group...
7
6745
by: War Eagle | last post by:
I have two byte arrays and a char (the letter S) I was to concatenate to one byte array. Here is what code I have. I basically want to send this in a one buffer (byte array?) through a socket. SWXXXXXXXXXYYYYZZZZZZZZZZZZZZZZZZZZZ Where S is the command for SEND and should just be the character S. Where W is a byte representing how long the filename (testfile.txt) is. In this case 12. Where XXXXXXX is converted from a string that...
4
3977
by: NT | last post by:
Hi there! I am puzzled by what I think must be a subtlety of C++ when list-initializing member variables in derived classes... Here's what I mean... class A { // warning l337proggy alert! ;-) public: A(int v=9812); // default value provided since this base constructor
18
1923
by: Bob Cummings | last post by:
Not sure if this is the correct place or not. Anyhow in school we were taught that when trying to calculate the efficiency of an algorithm to focus on something called FLOPs or Floating Point operations and disregard all integer operations. My question is this. I am writing a simulation for animal dispersement through large landscapes. We are loading data from several different files to simulate the environment the animals will be...
1
1511
by: dadevil | last post by:
Hi guys, this is my first post around here, and here it goes I need to create an array to store info about a number "n" of persons, so i create this structure ( each person have one name and one adress) __value stuct person{ // is this correct? ("__value") string name; string adress; }
2
3572
by: Rich | last post by:
I need to pass an array from C# to a managed C++ function (that will in turn call unmanaged C code). My knowledge of managed C++ syntax is limited (obviously). This needs to be a reference parameter so the function can change the contents of the array and give it back to the caller. How should I declare this array parameter in the managed C++ function?
3
1856
by: nembo kid | last post by:
I have an issue with a simple function that has to make a linear search for a key into an array. If the key is found in the array, the function it has to return 1 to the caller and pass array index through a out parameter. The issue is that the out parameter is not being updated. If I return the position to the caller (instead to use a out parameter) all is ok.
10
1614
by: Ray D. | last post by:
This is probably very simple but I'm blanking and I haven't found a tutorial that mentions this so I figure I'll ask here - I want to access multiple array elements at once, as shown below. This syntax gives me an error, so what is the correct method? Thanks for the help!! char Message .... Message
0
8995
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9558
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9378
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6798
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6077
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2216
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.