Link to home
Start Free TrialLog in
Avatar of Samooramad
Samooramad

asked on

I think its the array :)

Hi experts,

I'm using C with Unix and I'm new to it..I have a basic sorting program I'm trying to complete to practice but I cant figure out how to solve the errors.
Please show me how to correct it and explain WHY it is wrong

Thanks

#include <stdio.h>

main(int argc, char *argv[])
{
   #define M 6
   int location,start;
   int enteredNum, h, j,k;
   int numbers[M];


void sorting (int s,int *n );


  printf(" Please Enter 6 numbers to sort\n");

 for(h=0; h<M; h++)
{
 scanf("%d",numbers[h]);

}
   printf("These are the numbers you entered\n");
   for ( j=0; j< M;j++)
   {
    printf("%d",numbers[j]);
   }
for(k=0; k<M; k++)
{
  sorting(j,numbers[]);
}

for(j=0; j< M; j++)
{
 printf("%d",numbers[j]);
}

}
void sorting( start,int *num )
{

        int smallest;
        int temp,h;
        int location;
        smallest= num[start];

   for ( h=start+1; h < M; h++)
   {
     if (num[h] < smallest)
     {
      smallest= num[h];
      location = h;
     }
   }
if (smallest != num[start])
{
   temp =num[start];
   num[start]=num[location];
   num[location]=temp;

}
}


Avatar of van_dy
van_dy

to begin with you scanf() is wrong

 for(h=0; h<M; h++)
{
 >>scanf("%d",numbers[h]);      //this is wrong use scanf("%d", &numbers[h]);

}

now  the sorting function should sort the array all at once.
there is no need to do something like
for(k=0; k<M; k++)        //this appears in your code.
{
  sorting(numbers[], M);
}


just do
sorting(M, numbers);   //the sorting function sorting() will return the sroted array.

i would suggest you to take a look into bubble sorting algorithm
take a look here
http://linux.wku.edu/~lamonml/algor/sort/bubble.html
good enough to explain about bubble sort.
write out the program once again as reccommended and lets know whats the outcome
do u know that there is a built-in sorting function named qsort() which implements Quick Sort algorithm (which is more efficient than Bubble sort) and can relieve u from doing the raw code for sorting??

For quick example u can refer to ---
http://www.cplusplus.com/ref/cstdlib/qsort.html
I think the main aim of your exercise
is to code and understand a sorting
algorithm, rather than simply being
able to use some function from libc.
bubble sort and quick sort are just two
sorting algorithms, bubble sort being a
simpler algoritm. below is your code
modified to work with bubble sorting.

/*--not tested--*/

#include <stdio.h>

main(int argc, char *argv[])
{
   #define M 6
   int location,start;
   int enteredNum, h, j,k;
   int numbers[M];


void sorting (int *n, int s);


  printf(" Please Enter 6 numbers to sort\n");

  for(h = 0; h < 6; h++)
{
 scanf("%d",&numbers[h]);

}
   printf("These are the numbers you entered\n");
   for ( j=0; j< M;j++)
   {
    printf("%d\n",numbers[j]);
   }
  sorting(numbers, M);

  printf("Sorted Numbers\n");
for(j=0; j< M; j++)
{
 printf("%d\n",numbers[j]);
}

}

void sorting(int numbers[], int array_size)
{
  int i, j, temp;

  for (i = (array_size - 1); i >= 0; i--)
  {
    for (j = 1; j <= i; j++)
    {
      if (numbers[j-1] > numbers[j])
      {
        temp = numbers[j-1];
        numbers[j-1] = numbers[j];
        numbers[j] = temp;
      }
    }
  }
}

hope this helps,
van_dy
Avatar of Samooramad

ASKER

thank you for the comments, I'm not really doing this for the sorting..it was just a small program I could try to slowly get used to using C.. I will try the solutions posted and get back to you all

Thanks for the help
I tried the suggestions and still get errors
"filecrrr.c", line 29.21: 1506-046 (S) Syntax error.
"filecrrr.c", line 29.10: 1506-098 (E) Missing argument(s).
"filecrrr.c", line 37.21: 1506-276 (S) Syntax error: possible missing identifier?
"filecrrr.c", line 37.6: 1506-343 (S) Redeclaration of sorting differs from previous declaration on line 11 of "filecrrr.c".
"filecrrr.c", line 37.6: 1506-381 (I) The type "int*" of parameter 2 in the prototype declaration is not compatible with the corresponding parameter type "int" in the nonprototype declaration.
"filecrrr.c", line 44.14: 1506-019 (S) Expecting an array or a pointer to object type.
"filecrrr.c", line 47.10: 1506-019 (S) Expecting an array or a pointer to object type.
"filecrrr.c", line 49.17: 1506-019 (S) Expecting an array or a pointer to object type.
"filecrrr.c", line 53.17: 1506-019 (S) Expecting an array or a pointer to object type.
"filecrrr.c", line 55.10: 1506-019 (S) Expecting an array or a pointer to object type.
"filecrrr.c", line 56.4: 1506-019 (S) Expecting an array or a pointer to object type.
"filecrrr.c", line 56.15: 1506-019 (S) Expecting an array or a pointer to object type.
"filecrrr.c", line 57.4: 1506-019 (S) Expecting an array or a pointer to object type.
please post the code that you are compiling
#include <stdio.h>

main(int argc, char *argv[])
{
   #define M 6
   int location,start;
   int enteredNum, h, j,k;
   int numbers[M];


void sorting (int s,int *n );


  printf(" Please Enter 6 numbers to sort\n");

 for(h=0; h<M; h++)
{
 scanf("%d",&numbers[h]);

}
   printf("These are the numbers you entered\n");
   for ( j=0; j< M;j++)
   {
    printf("%d",numbers[j]);
   }

  sorting(M,numbers[]);


for(j=0; j< M; j++)
{
 printf("%d",numbers[j]);
}

}
void sorting( start,int *num )
{

        int smallest;
        int temp,h;
        int location;
        smallest= num[start];

   for ( h=start+1; h < M; h++)
   {
     if (num[h] < smallest)
     {
      smallest= num[h];
      location = h;
     }
   }
if (smallest != num[start])
{
   temp =num[start];
   num[start]=num[location];
   num[location]=temp;

}
}


ASKER CERTIFIED SOLUTION
Avatar of van_dy
van_dy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thank you :)