d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Simple Help With Basic C > Sorting Problem
12Next
Add Reply New Topic New Poll
Member
Posts: 51
Joined: Feb 7 2013
Gold: 0.00
Mar 14 2013 08:21pm
I can achieve my code mostly it ascends and descends however it's not SORTING like I want it to
eg 9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9

I am suppose to build a program that uses a single-dimension array to store
10 numbers input by a user. After inputting the numbers, the
user should see a menu with two options to sort and print the
10 numbers in ascending or descending order.

Code
#include <stdio.h>
#include <stdlib.h>

int main(void)

{

   int  iNumbers[10];
   int  iEntry=0;
   int  x=0;

   printf("Enter 10 numbers\n");
     
   
   for (x=0; x<=9; x++) {
       scanf("%d", &iNumbers[x]);
       printf("\n\t%d iNumbers\n", iNumbers[x]);
       printf("\n\t%d X\n", x);
   }
   
   printf("\n\nWhich order would you like to see your numbers?");
   printf("\n1)\tAscending\n");
   printf("\n2)\tDescending\n");
   scanf("%d", &iEntry);
   printf("\n\t%d iEntry\n\n", iEntry);
   switch(iEntry)  {
       case 1:
       printf("\n\n%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n\n",iNumbers[0],iNumbers[1],iNumbers[2],iNumbers[3],iNumbers[4],iNumbers[5],iNumbers[6],iNumbers[7],iNumbers[8],iNumbers[9]);
       break;
       case 2:
       printf("\n\n%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n\n",iNumbers[9],iNumbers[8],iNumbers[7],iNumbers[6],iNumbers[5],iNumbers[4],iNumbers[3],iNumbers[2],iNumbers[1],iNumbers[0]);
       break;
       }
           system ("pause");
}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 14 2013 08:27pm
Can you point out which line is supposed to do the actual sort? because i dont see it.
Member
Posts: 51
Joined: Feb 7 2013
Gold: 0.00
Mar 14 2013 08:28pm
None I just need a short refresher on what code sorts and where I should put it
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 14 2013 08:34pm
you shouldnt be printing in asc/desc. print from index 0 to 9 in both cases, and put it AFTER the switch. only the sort should be inside the switch.

what is your assignment exactly? are you supposed to write your own sort or are you allowed to use a library sort?
Member
Posts: 51
Joined: Feb 7 2013
Gold: 0.00
Mar 14 2013 08:36pm
Build a program that uses a single-dimension array to store
10 numbers input by a user. After inputting the numbers, the
user should see a menu with two options to sort and print the
10 numbers in ascending or descending order.

however I read the chapter while waiting and it doesn't really explain how to sort :/
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 14 2013 08:49pm
Quote (Xarai @ Mar 14 2013 10:36pm)
Build a program that uses a single-dimension array to store
10 numbers input by a user. After inputting the numbers, the
user should see a menu with two options to sort and print the
10 numbers in ascending or descending order.

however I read the chapter while waiting and it doesn't really explain how to sort :/


go google bubble sort. its most likely the simplest version of the sort you can implement.

and then for printing the numbers to the screen i suggest something like this

Code
 
int i = 0;
switch(iEntry)  {
      case 1:
      while(i++<9) printf("%d\t", iNumbers[i]);
      case 2:
      i = 9;
      while(i-->0) printf("%d\t", iNumbers[i]);
      break;
      }
Member
Posts: 51
Joined: Feb 7 2013
Gold: 0.00
Mar 15 2013 06:33pm
my apologies for the jumbled up mess I am about to post
after playing around all day I am still having a problem -.- I can get it to ascend but my case statement switch isn't working very well
if someone could help me with where I should properly place my case switch and make this work I would be much obliged however I've tried lots and today the teacher is gone until tuesday so I am on my own
maybe help out a little with explanations or point me in the right directions and please no.. hey go google this :/ im all googled out today

Code
#include <stdio.h>
#include <stdlib.h>

int main()
{
 int array[100], c, d, swap;
 int n=10;
 int iEntry=0;

 printf("Enter %d integers\n", n);

 for (c = 0; c < n; c++)
   scanf("%d", &array[c]);
   
   printf("\n\nWhich order would you like to see your numbers?");
   printf("\n1)\tAscending\n");
   printf("\n2)\tDescending\n");
   scanf("%d", &iEntry);
   printf("\n\t%d iEntry\n\n", iEntry);
   switch (iEntry)

 for (c = 0; c < ( n - 1 ); c++)
 {
   for (d = 0; d < n - c - 1; d++)
   {
     if (array[d] > array[d+1]) /* For decreasing order use < */
     {
       swap       = array[d];
       array[d]   = array[d+1];
       array[d+1] = swap;
     }
   }
 }

 printf("Sorted list in ascending order:\n");

 for ( c = 0; c < n; c++ )
    printf("%d\n", array[c]);
    system ("pause");
 return 0;
}
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 15 2013 10:22pm
Quote (Xarai @ Mar 15 2013 08:33pm)
my apologies for the jumbled up mess I am about to post
after playing around all day I am still having a problem -.- I can get it to ascend but my case statement switch isn't working very well
if someone could help me with where I should properly place my case switch and make this work I would be much obliged however I've tried lots and today the teacher is gone until tuesday so I am on my own
maybe help out a little with explanations or point me in the right directions and please no.. hey go google this :/ im all googled out today

Code
#include <stdio.h>
#include <stdlib.h>

int main()
{
 int array[100], c, d, swap;
 int n=10;
 int iEntry=0;

 printf("Enter %d integers\n", n);

 for (c = 0; c < n; c++)
   scanf("%d", &array[c]);
   
   printf("\n\nWhich order would you like to see your numbers?");
   printf("\n1)\tAscending\n");
   printf("\n2)\tDescending\n");
   scanf("%d", &iEntry);
   printf("\n\t%d iEntry\n\n", iEntry);
   switch (iEntry)

 for (c = 0; c < ( n - 1 ); c++)
 {
   for (d = 0; d < n - c - 1; d++)
   {
     if (array[d] > array[d+1]) /* For decreasing order use < */
     {
       swap       = array[d];
       array[d]   = array[d+1];
       array[d+1] = swap;
     }
   }
 }

 printf("Sorted list in ascending order:\n");

 for ( c = 0; c < n; c++ )
    printf("%d\n", array[c]);
    system ("pause");
 return 0;
}


your switch should be after you sort your list.

you are also missing your case statement.

your code should look something like this

Code
int main()
{

 ask for how many numbers you want to store
 store user input
 loop to store desired number of numbers in the array

  ask if they want assending or decending numbers
  store user input

  sort the array of numbers

  switch for assending or decending
  case accending
      print the array normally
  case decending
      print the array backwards

return 0;
}


also you should create a dynamic array using malloc rather then using a static on of 100 or limit your user so that they cannot enter more than 100 numbers. thats just asking to be exploited with a buffer overflow.

edit:: for the simplest way to write the array backwards or forwards view my previous reply. you can also use for loops but they do not look as pretty.

This post was edited by AbDuCt on Mar 15 2013 10:25pm
Member
Posts: 51
Joined: Feb 7 2013
Gold: 0.00
Mar 16 2013 10:07am
Update it's almost perfectly working except 1 tiny problem
whenever I hit 1 or 2 for ascend or descend it says 1 or 2 I havent quite figured out why yet I am sure it's a small error
could someone help me find it?

Code
#include <stdio.h>
#include <stdlib.h>
int main( void ) {
   int array[100], c, d, swap;
   int n=10;
   int iEntry=0;
       printf("Enter %d integers\n", n);
       for (c = 0; c < n; c++)
           scanf("%d", &array[c]);
       printf("\n\nWhich order would you like to see your numbers?");
       printf("\n1)\tAscending\n");
       printf("\n2)\tDescending\n");
       scanf("%d", &iEntry);
       printf("\n\t%d iEntry\n\n", iEntry);
       for (c = 0; c < ( n - 1 ); c++) {
           for (d = 0; d < n - c - 1; d++) {
               if ( iEntry == 1? array[d] > array[d+1] : array[d] < array[d+1] ) {
                   swap = array[d];
                   array[d] = array[d+1];
                   array[d+1] = swap;
               }
           }
       }
       printf("Sorted list in %s order:\n", iEntry == 1? "ascending":"descending");
       for ( c = 0; c < n; c++ )
           printf("%d\n", array[c]);
       system ("pause");
   return 0;
}
Member
Posts: 51
Joined: Feb 7 2013
Gold: 0.00
Mar 16 2013 10:10am
ehh... that's showing their input -.- ^^ ok well thats my finished code i feel dumb
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll