Syntax too hard for computer?

This is a discussion on Syntax too hard for computer? within the C Programming forums, part of the General Programming Boards category; I wrote a syntax for a small program that sorts numbers. Anyway the program works well for 30 and even ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    4

    Syntax too hard for computer?

    I wrote a syntax for a small program that sorts numbers.
    Anyway the program works well for 30 and even 40 numbers, but when I try to sort 50 numbers or more it gets stuck. It makes no sense that the syntax is to hard for the computer, and I also know that the loop is not stuck because when i try to sort 30 numbers it's ok.. so why does it get stuck? help, anyone?

    Code:
    #include <stdio.h>
    
    void main()
    
    {
    
    double a[100] = {0};
    int counter;
    int input;
    int b;
    int c;
    int f;
    int x;
    int t;
    int m;
    int r;
    double temp;
    
    
    
    input = 0;
    temp = 0;
    b = 0;
    x = 0;
    f = 0;
    t = 0;
    m = 0;
    r = 0;
    counter = 1;
    c = 1;
    
    
    
    printf("How many numbers do you want to sort?\n");
    
    while(scanf("%d", &input) != 1)
    {
    fflush(stdin);
    printf("Wrong Entry. Please enter a number\n");
    }
    
    
    while(counter <= input)
    
    {
    
    printf("Print No.%d\n", counter);
    
    while(scanf("%lf", &a[b]) != 1)
    {
    fflush(stdin);
    printf("Wrong Entry. Please enter a number\n");
    }
    
    counter++;
    b++;
    }
    
    
    b = 0;
    counter = 0;
    
    
    while(counter < input - 1)
    
    {
    
    while(a[b] > a[c])
    {
    
    
    f = input;
    x = input - 1;
    
    temp = a[c];
    
    
    while(f > 0) 
    {
    a[f] = a[x];
    f--;
    x--;
    }
    
    
    a[0] = temp;
    
    
    
    t = c;
    
    m = c + 1;
    r = c + 2;
    
    
    while(t < input)
    
    {
    
    a[m] = a[r];
    r++;
    m++;
    t++;
    
    }
    
    b = 0;
    c = 1;
    counter = 0;
    
    }
    
    
    b++;
    c++;
    counter++;
    
    }
    
    
    a[input] = '\0';
    
    counter = 0;
    b = 0;
    
    while(counter < input)
    {
    
    if(a[b] - (int)a[b] > 0)
    
    {
    printf("%1.1f", a[b]);
    }
    
    else
    
    {
    printf("%1.0f", a[b]);
    }
    
    if(counter < input - 1)
    {
    
    printf(",");
    
    }
    
    b++;
    counter++;
    
    }
    
    printf("\n");
    
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Few points that come to mind:

    1) I would recommend you use int main() instead of void main().
    2) Your indentation is non-existant, and that makes it hard to read.
    3) The amount of local variables you use makes the program hard to follow, at least since most of them are just index variables.
    4) You are flushing stdin. That may work on your compiler for your system, but according to the C standard, such an action is undefined. That means it could cause bugs or problems on your system.
    5) I'm not sure, but I believe you allow your program to go past the boundaries of your array if the user selects a large number of elements to sort, thereby providing the means to cause some hard-to-detect bugs if not just a crash of the program.
    6) You're adding '\0' to what I think is the end of a double array. Unless I'm missing something obvious, this is pointless to even consider doing.
    7) I think you're totally overcomplicating the idea of sorting an array. It shouldn't be this long of a program, or at the very least, it should be quite easier to read, manage, and debug.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're smart enough to code up a sorting program, but can't bother to either use a decent indentation style, or describe your sorting algorithm.

    The former would be a considerable help to everyone to see syntax errors in your code, and the latter would be a great help to others trying to find your program's logic errors.


    I'm going to guess that as the quantity of items to be sorted increases, your program is either overstepping the array boundaries, or overwriting a portion of it's contents, by mistake.

    Adak

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    A one-time indent
    Code:
    #include <stdio.h>
    
    void main()
    {
        double a[100] = { 0 };
        int counter;
        int input;
        int b;
        int c;
        int f;
        int x;
        int t;
        int m;
        int r;
        double temp;
    
        input = 0;
        temp = 0;
        b = 0;
        x = 0;
        f = 0;
        t = 0;
        m = 0;
        r = 0;
        counter = 1;
        c = 1;
    
        printf("How many numbers do you want to sort?\n");
        while (scanf("&#37;d", &input) != 1) {
            fflush(stdin);
            printf("Wrong Entry. Please enter a number\n");
        }
    
        while (counter <= input)
        {
            printf("Print No.%d\n", counter);
    
            while (scanf("%lf", &a[b]) != 1) {
                fflush(stdin);
                printf("Wrong Entry. Please enter a number\n");
            }
    
            counter++;
            b++;
        }
    
        b = 0;
        counter = 0;
    
        while (counter < input - 1)
        {
            while (a[b] > a[c]) {
                f = input;
                x = input - 1;
                temp = a[c];
                while (f > 0) {
                    a[f] = a[x];
                    f--;
                    x--;
                }
    
                a[0] = temp;
                t = c;
                m = c + 1;
                r = c + 2;
    
                while (t < input)
                {
                    a[m] = a[r];
                    r++;
                    m++;
                    t++;
                }
    
                b = 0;
                c = 1;
                counter = 0;
            }
    
            b++;
            c++;
            counter++;
        }
    
        a[input] = '\0';
    
        counter = 0;
        b = 0;
        while (counter < input) {
            if (a[b] - (int) a[b] > 0)
            {
                printf("%1.1f", a[b]);
            }
            else
            {
                printf("%1.0f", a[b]);
            }
            if (counter < input - 1) {
                printf(",");
            }
            b++;
            counter++;
        }
        printf("\n");
    }
    > but when I try to sort 50 numbers or more it gets stuck
    Are you sure?
    Because those nested loops look horribly inefficient.

    If 40 takes 1 second, and adding 1 doubles the execution time, then 50 will be taking 17 MINUTES.
    That might look stuck to the casual observer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  2. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  3. Driver Problem
    By todd14 in forum C Programming
    Replies: 3
    Last Post: 04-04-2007, 11:46 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM

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