
Need help Sorting Arrays with an insert function.
: I'm trying to write a program that will prompt a user to enter an
: integer. Each integer is read one at a time, and entered into an
: array(max of 23). The array elements are then inserted into ascending
: order. After each integer is read, the integer entered and the array
: elements that are used are printed. Requirements are: No Bubble sort, no
: global variables and the insert and print must be separate function. I
: don't know if anything I wrote is even close, I found the insert
: function in my book. The program will not completely compile, I get a
: mismatch error redeclaration error on my array_Insert and array_Print
: function. I'm just learning C and my instructor is moving so fast I
: can't keep. Any help would be greatly appreciated. Here is my junky
: code.
Well, the first rule is to post something that compiles. Try the
changes suggested below.
: /*
: * Read values and sort them using "insertion sort."
: */
: #include <stdio.h>
: #define SIZE 22
: int main(void)
: {
: void array_Insert(int [] );
: void array_Print(int [] );
/* ### it would be better to put the prototypes outside the main
function; further, they need to agree with the actual functions.
Try:
void array_Insert(int [], int);
void array_Print(int [], int);
*/
: int array[SIZE];
: int counter=0;
: while (counter > SIZE)
: {
: counter++;
: printf("please enter a number: ");
: scanf("%d", &array[SIZE]);
: array_Insert(array);
: }
/* ### the loop is basically shot - try:
while (counter < SIZE)
{
printf("please enter a number: ");
fflush(stdout);
scanf("%d", &array[counter]);
array_Insert(array, counter);
counter++;
}
array_Print(array, counter);
*/
: array_Print(array);
: return 0;
: }
:
: void array_Insert(int a[], int num, int val)
/* ### void array_Insert(int a[], int num) */
: {
: int pos;
/* ### int pos, val = a[num]; */
: for (pos = num; pos > 0 && val < a[pos-1]; pos--)
: a[pos] = a[pos-1];
: a[pos] = val;
: }
: void array_Print(int a[], int num)
: {
: int i;
: for (i = 0; i < num; i++)
: printf("%i\n", a[i]);
: }
Will