Thread: Very Simple C Program

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    3

    Red face Very Simple C Program

    Hi

    I know this is really simple but i've been stuck trying to work this out for a couple of days now.

    I need to write a simple c program in which you can enter three numbers, in any order, and the resulting output will put them in ascending order.

    I think IF statements or arrays might be needed???

    If any one can give me a hand i'd really appreciate it.

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    385
    Post the code you have so far and then someone can probably nudge you in the right direction.
    Wandering aimlessly through C.....

    http://dbrink.phpwebhosting.com

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    Here you go...
    Code:
    #include <stdio.h>
    
    #define RANGE 3
    
    int main(
            int argc,
            int *argv[]
            )
    {
      int numbers[RANGE], ctr, a, b;
    
    /*
    ** Read in the numbers ( limit set using RANGE definition above )
    */
      for( ctr=0 ; ctr < RANGE ; ctr++ )
      {
        printf( "\nEnter next number: " );
        /** You would normally check for non integers **/
        /** before this next statement **/
        scanf( "%d", &numbers[ctr] );
      }
    
    /*
    ** Sort the array
    */
      for( ctr=0 ; ctr < RANGE ; ctr++ )
      {
        if( numbers[ctr] < numbers[ctr+1] )
        {
          /** If the current number is less than the next number, **/
          /** swap them round and start again **/
          /** I think this is known as a bubble sort **/
          a = numbers[ctr];
          b = numbers[ctr+1];
          numbers[ctr] = b;
          numbers[ctr+1] = a;
    
          ctr = -1; /** Effectively restarts loop **/
                    /** ( -1 because loop executes ctr++ ) **/
        }
      }
    
    /*
    ** Print array
    */
      printf( "\nNumbers in sorted order:\n\n" );
    
      for( ctr=0 ; ctr < RANGE ; ctr++ )
      {
        printf( "%d\n", numbers[ctr] );
      }
    
      printf( "\n" );
    
      return( 0 );
    }

    &#91;code]&#91;/code]tagged by Salem

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I think this is known as a bubble sort
    It is, but you've implemented it incorrectly, and caused a nasty little bug

    The problem is here:
    >>for( ctr=0 ; ctr < RANGE ; ctr++ )
    >>if( numbers[ctr] < numbers[ctr+1] )
    Assuming RANGE is 3:
    when ctr is 2, the if statement will check numbers[2] against numbers[3]. See the problem? I'll leave that one for you to ponder over....
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    Yeah as hammer said its bubble sort try this
    Code:
    void sort( char s [] )
    {
             int cnt_main, cnt_sub;
             int hold;
              hold =0;
             
              for ( cnt_main =0; cnt_main <= SIZEofarray-1; cnt_main++)
                             for( cnt_sub = 0; cnt_sub <= SIZEofarray-2;cnt_sub++ )
                                       if ( s[i] > s[i+1] ){
                                               hold = s[i];
                                               s[i] = s[i+1];
                                               s[i+1] = hold;
                                       }
    }
    Ahh well if u guys do find a mistake or anything well i am alil tipsy at the movement ...ahh well just rty it ... or u can try quit sort which is faster than bubble..
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM