473,378 Members | 1,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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, 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*************************
Nov 13 '05 #1
5 2178
"ritchie" <ri*********@yahoo.com> wrote in message
news:3b**************************@posting.google.c om...
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, 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*************************

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
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******@mkwahler.net> wrote in message news:<iF*******************@newsread1.news.pas.ear thlink.net>...
"ritchie" <ri*********@yahoo.com> wrote in message
news:3b**************************@posting.google.c om...
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, 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*************************

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

Nov 13 '05 #3
"ritchie" <ri*********@yahoo.com> wrote in message
news:3b**************************@posting.google.c om...
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*********@yahoo.com> wrote in message
news:3b**************************@posting.google.c om...
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, 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 )

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[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(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: xa******@myrapidsys.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, 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*************************

Nov 13 '05 #6

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

Similar topics

31
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? ...
5
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,...
7
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. ...
4
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!...
18
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...
1
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...
2
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...
3
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...
10
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.