d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Simple Help With Basic C > Sorting Problem
Prev12
Add Reply New Topic New Poll
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 16 2013 12:48pm
Quote (Xarai @ Mar 16 2013 12:07pm)
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?


printf("\n\t%d iEntry\n\n", iEntry);

try reading through your code line by line it makes it easier to find these things. also if your properly format your code

id make it look something more like this


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

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


adding a bit of space to group similiar function calls can make it clearer what each chunk is trying to do.

This post was edited by AbDuCt on Mar 16 2013 12:51pm
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll