Sorting Problems

This is a discussion on Sorting Problems within the C Programming forums, part of the General Programming Boards category; This is a program that will ask the user for 4 numbers and sorts them from highest to lowest. It ...

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

    Sorting Problems

    This is a program that will ask the user for 4 numbers and sorts them from highest to lowest. It compiles fine with no errors but when I run the program it will not display the sorted array.
    Code:
    //C source
    #include <stdlib.h>
    #define MAX 4
    
    int main()
    {
    int i,a[MAX],t,k;
    
    printf("Enter some numbers (4)\n>>> ");
    for(i=0;i<MAX;i++)
        scanf("%d",&a[i]);
    for(k=MAX-1;k>0;k--){
         for(i=0;i<k;i++){
               if(a[i]>a[i+1]){
                    t=a[i];
                    a[i]=a[i+1];
                    a[i+1]=t;
               }
         }
    }
    printf("The sorted array is\n>>>");
    for(i=0;i<MAX;i++)
        printf("%d",a[i]);
    getch();
    exit(0);
    }
    I have done this one w/o the help of books or anything so there may be a logic error that I couldn't find but it seems to me like it should work.

    Sample Excecution:
    [running]
    Enter some numbers (4)
    >>> 7841

    [/running]
    If i hit control+z several times it will stop but turn out a number like 7469473631893648765. Really weird.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    29
    This I know Salem. I used a for loop to get the 4 digits that the user would input and used the for loop again to print out the 4 sorted digits.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    fgets() seems to make it work...


    Code:
    #define MAX 4
    
    int main()
    {
    int i,a[MAX],t,k;
    char buff[10];
    printf("Enter some numbers (4)\n>>> ");
    for(i=0;i<MAX;i++)
        a[i] = atoi(fgets(buff, 10, stdin));
    for(k=MAX-1;k>0;k--){
         for(i=0;i<k;i++){
               if(a[i]>a[i+1]){
                    t=a[i];
                    a[i]=a[i+1];
                    a[i+1]=t;
               }
         }
    }
    printf("The sorted array is\n>>>");
    for(i=0;i<MAX;i++)
        printf("%d\n",a[i]);
    getch();
    return 0;
    }
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting problems.
    By Sedvan in forum C++ Programming
    Replies: 2
    Last Post: 02-14-2008, 12:13 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  5. Sorting structures
    By RedRum in forum C++ Programming
    Replies: 2
    Last Post: 05-23-2002, 12:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21