• C

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;

}
}


SamooramadAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

van_dyCommented:
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
0
shah1d1698Commented:
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
0
van_dyCommented:
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
0
The Firewall Audit Checklist

Preparing for a firewall audit today is almost impossible.
AlgoSec, together with some of the largest global organizations and auditors, has created a checklist to follow when preparing for your firewall audit. Simplify risk mitigation while staying compliant all of the time!

SamooramadAuthor Commented:
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
0
SamooramadAuthor Commented:
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.
0
van_dyCommented:
please post the code that you are compiling
0
SamooramadAuthor Commented:
#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;

}
}


0
van_dyCommented:
#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[]);  dont pass like this, since you arent using the bubble sort algo for sorting
   for(j = 0; j < M; ++j){
  sorting(j, numbers);
   }
   printf("\n");

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

}

void sorting( int start,int *num ) //you just wrote 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;

}
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SamooramadAuthor Commented:
thank you :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.