Thread: help ASAP

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    4

    Question help ASAP

    I have to write a program which i am having some problems with. I have to do a sort routine. I have to read the numbers form a file called id.txt and using a sort i have to sort these numbers out in order from the biggest to the smallest.
    These numbers have to then be shown on the screen

    can anyone help me plzzzzz

  2. #2
    Registered User Mace's Avatar
    Join Date
    Nov 2001
    Posts
    7
    This is a program my teacher wrote for us as an example. You would have to modify it to work the way you want it to, but as it stands it sorts in ascending order. You must change it to descending and add file input, so you still have some work to do if you don't understand the code. Good luck.

    Code:
    /*
    This program sorts an array's values into
    ascending order using bubble sort.
    */
    
    #include <stdio.h>
    
       
    #define ARRAYSIZE 10
    void bubbleSort(int [], int);
    
    int main()
    {
       int a[ ] = { 6, 4, 8, 10, 12, 89, 68, 45, 2, 37 }; //Initialize the array
       int i;
    
       printf("Data items in original order\n");
       for (i = 0; i < ARRAYSIZE; i++ )
          printf("%d ", a[ i ]);
    
       bubbleSort(a, ARRAYSIZE);
    
       return 0;
    }//end int main()
    
    void bubbleSort(int x[], int arraySize)
    {
       int temp;
       int pass;
       int i;
    
       for ( pass = 0; pass < arraySize - 1; pass++ ) // passes
       {
          for ( i = 0; i < arraySize - pass - 1; i++ ) 
    	  {
             if ( x[ i ] > x[ i + 1 ] ) //swap
    		 {      
                temp = x[ i ];                 
                x[ i ] = x[ i + 1 ];
                x[ i + 1 ] = temp;
             }//end if ( x[ i ] > x[ i + 1 ] ) 
    	  }//end for ( i = 0; i < arraySize - pass; i++ ) 
       }//end for (  pass = 0; pass < arraySize - 1; pass++ )
    	  
    
       printf("\n\nData items in ascending order\n");
    
       for (i = 0; i < arraySize; i++ )
          printf("%d ", x[ i ]);
    
       printf("\n\n");
    }//end void bubbleSort(int x[], int arraySize)

  3. #3
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    It's easy ...

    Here's the program:

    # include <stdio.h>
    # include <conio.h>

    void Sort(int *a, int *b);

    int main()
    {
    int a1, b1, c1;

    clrscr();
    printf("\nEnter 3 numbers: ");
    scanf("%d %d %d", &a1, &b1, &c1);
    Sort(&a1, &b1);
    Sort(&b1, &c1);
    Sort(&a1, &c1);
    printf("\nSorted numbers are: %d, %d, %d", a1, b1, c1);
    getch();
    return 0;
    }

    void Sort(int *a, int *b)
    {
    int temp=0;

    if (*a < *b)
    {
    temp = *a;
    *a = *b;
    *b = temp;
    }
    }
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help ASAP ,
    By Haytham in forum C Programming
    Replies: 3
    Last Post: 05-14-2007, 10:21 AM
  2. Need Help ASAP....
    By Maxwell in forum C++ Programming
    Replies: 16
    Last Post: 09-14-2005, 06:56 PM
  3. Help Needed Please... Asap
    By Shabba in forum C Programming
    Replies: 2
    Last Post: 01-13-2004, 04:24 PM
  4. Count_nums (need help ASAP!)
    By legacye in forum C Programming
    Replies: 6
    Last Post: 11-22-2003, 06:32 PM
  5. Help Needed Asap With Searching An Array!
    By HelpMe++ in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2003, 04:44 AM