Need help Sorting Arrays with an insert function. 
Author Message
 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.
/*
 * 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 [] );

  int  array[SIZE];
  int counter=0;

  while (counter > SIZE)
  {
 counter++;
  printf("please enter a number: ");
  scanf("%d", &array[SIZE]);

 array_Insert(array);
  }

 array_Print(array);

  return 0;

Quote:
}

void array_Insert(int a[], int num, int val)
{
  int pos;

  for (pos = num; pos > 0 && val < a[pos-1]; pos--)
    a[pos] = a[pos-1];
  a[pos] = val;

Quote:
}

void array_Print(int a[], int num)
{
  int i;

  for (i = 0; i < num; i++)
  printf("%i\n", a[i]);

Quote:
}



Thu, 30 Aug 2001 03:00:00 GMT  
 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



Fri, 31 Aug 2001 03:00:00 GMT  
 Need help Sorting Arrays with an insert function.
hi , The function prototype you declared and the function you implemented
are completly different and hence the mismatch errors.
you declared your array_insert as : array_insert(int []); i.e.the function
expects only
an array of ints as argument , nothing else.But when you implemented it ,it
looks something like array_insert(int a[], int num,int val) i.e it expects
an array and an int and an int.That's wrong.

Change your prototype to array_insert(int [] , int ,int);
and    array_print(int [] , int);

While calling the function , array_insert(array , count , array[count]);
                                         array_print(array,count);

That's it.It should work now.

Quote:

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

>   int  array[SIZE];
>   int counter=0;

>   while (counter > SIZE)
>   {
>  counter++;
>   printf("please enter a number: ");
>   scanf("%d", &array[SIZE]);

>  array_Insert(array);
>   }

>  array_Print(array);

>   return 0;
> }
> void array_Insert(int a[], int num, int val)
> {
>   int pos;

>   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]);
> }



Fri, 31 Aug 2001 03:00:00 GMT  
 Need help Sorting Arrays with an insert function.

Quote:

> Jeff!

> mistakes in code are discussed in other postings. Try to make you familiar
> with the standard functions lsearch(), lfind(), bsearch() and qsort()

He will have a hard time becoming familiar with the non-standard
functions lsearch or lfind if he uses a standard C textbook.  He may
have an even harder time using them with with a standard C compiler.

--




Sun, 02 Sep 2001 03:00:00 GMT  
 Need help Sorting Arrays with an insert function.


Quote:
>Jeff!

>mistakes in code are discussed in other postings. Try to make you familiar
>with the standard functions lsearch(), lfind(), bsearch() and qsort()

Note that while bsearch() and qsort() are standard C library functions
lsearch() and lfind() are not.

--
-----------------------------------------


-----------------------------------------



Mon, 03 Sep 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Need help sorting text into arrays. ASAP thx :)

2. Array Sorting-need major help

3. sorted list of emails -- Insert function

4. Function pointer array help needed

5. Need Help With Returning an array from a function

6. insert function using arrays

7. Advice needed on sorting BIG arrays

8. pasing an array into a sort function

9. Some array sorting algorithms rendered as C functions: sort_n.c

10. Urg help with insert in array using pointers

11. Urgent help with insert into array

12. Help with inserting into array of struct

 

 
Powered by phpBB® Forum Software