Thread: Help with break statement in random number sorting C program

  1. #1
    Registered User CSheridan's Avatar
    Join Date
    Jul 2014
    Location
    Arkansas, United States
    Posts
    3

    Help with break statement in random number sorting C program

    Hello. I am currently learning the C language (to start), and I purchased the Absolute Beginner's Guide to C Programming. While I wait for my C Programming Language 2nd edition to arrive, this is all I have as a resource (except for online tutorials). I have read that this book is not reliable, and I agree since it seems that I'm just crunching a bunch of code without a full understanding of what I'm doing.

    The program I am stuck on is used to generate random numbers, then sort them (found in Chapter 23, p. 212). When I compile it, I get an error stating "break statement not within loop or switch. I'm finding that there is an issue with a break statement, but I don't know enough yet. Can anyone help me? Thanks.

    ps. If my indenting is sloppy, forgive me.

    Code:
    /* Will generate random numbers, then sort them.*/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    main()        
    {
        int ctr, inner, outer, didSwap, temp;
        int nums[10];
        time_t t;    
        srand(time(&t));
        
        for (ctr = 0; ctr < 10; ctr++)
        {
            nums[ctr] = (rand() % 99) + 1;        
        }
        
        {
            printf("%d\n", nums[ctr]);
        }
        
        //Sort the array
        
        for (outer = 0; outer < 9; outer++);
        {
            didSwap = 0;
            for (inner = outer; inner < 10; inner++)
            {
                if (nums[inner] < nums[outer])
                {
                    temp = nums[inner];
                    nums[inner] = nums[outer];
                    nums[outer] = temp;
                    didSwap = 1;
                }
            
        }
        
            if (didSwap == 0)
            {
                break;
            }
           
            
        }
        
        printf("\nHere is the list after the sort: \n");
        for (ctr = 0; ctr < 10; ctr++)
      {
        printf("%d\n", nums[ctr]);
      }   
        
        return 0;
    }

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,671
    You have an unwanted semi-colon here:

    Code:
    for (outer = 0; outer < 9; outer++); // <--

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,671
    Now you might ask, "Why such a silly error message?" but it actually makes sense. Here's the explanation.

    When you add a semicolon to a for loop like that, you're telling the compiler "Loop this many times, but do nothing else". It's effectively the same as

    Code:
    for (outer = 0; outer < 9; outer++) {}
    accomplishing only moving outer from 0 up to 9 incrementally. In fact, if you looked at the compiler's output, it's quite possible the compiler optimized that line of code directly to the equivalent of:

    Code:
    outer = 9;
    So, by terminating that for loop in such a way, the braces you added simply setup a new scope

    Code:
    {
            didSwap = 0;
            for (inner = outer; inner < 10; inner++)
            {
                if (nums[inner] < nums[outer])
                {
                    temp = nums[inner];
                    nums[inner] = nums[outer];
                    nums[outer] = temp;
                    didSwap = 1;
                }
             
            }
         
            if (didSwap == 0)
            {
                break;
            }
            
             
        }
    This scope obviously has no loop in it where the break statement is, and the error is generated.

    By the way, if you turn up the warning levels in your compiler, you may get warnings for these types of errors (and expose other things you should fix):

    Code:
    cc     nope.c   -o nope
    nope.c:7:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
    main()
    ^~~~
    nope.c:42:13: error: 'break' statement not in loop or switch statement
                break;
                ^
    nope.c:25:40: warning: for loop has empty body [-Wempty-body]
        for (outer = 0; outer < 9; outer++);
                                           ^
    nope.c:25:40: note: put the semicolon on a separate line to silence this warning
    2 warnings and 1 error generated.
    make: *** [nope] Error 1

  4. #4
    Registered User CSheridan's Avatar
    Join Date
    Jul 2014
    Location
    Arkansas, United States
    Posts
    3
    Wow. Thank you for the prompt reply. I will need to look into changing the compiler settings. Also, the semicolon was completely overlooked and out of habit.

    I tried to compile again, and it was successful; however, it sorted the numbers correctly, but did not list the first set like it was supposed to. It looked like this:

    2346224

    Here is the list after the sort:
    8
    10
    24
    27
    46
    47
    79
    81
    82
    93

    RUN SUCCESSFUL (total time: 265ms)


    That first set of numbers shouldn't look like this. I'll keep trying different things unless someone gives me a hint. But hey, I'm closer than I was previously! :-)

  5. #5
    Registered User CSheridan's Avatar
    Join Date
    Jul 2014
    Location
    Arkansas, United States
    Posts
    3
    Looks like I figured it out. I forgot a little piece of code:

    Code:
     printf("\nHere is the list before the sort: \n");
        for (ctr = 0; ctr < 10; ctr++)    
        {
            printf("%d\n", nums[ctr]);
        }
    Now the output changed to:

    Here is the list before the sort:
    18
    93
    83
    98
    84
    46
    34
    38
    14
    79

    Here is the list after the sort:
    14
    18
    34
    38
    46
    79
    83
    84
    93
    98

    RUN SUCCESSFUL (total time: 188ms)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number sorting program
    By switchback in forum C Programming
    Replies: 7
    Last Post: 07-25-2008, 05:22 PM
  2. Break in If statement
    By plain in forum C++ Programming
    Replies: 10
    Last Post: 09-07-2006, 01:53 PM
  3. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  4. Replies: 2
    Last Post: 12-25-2003, 01:31 AM
  5. break statement
    By Motisa in forum C++ Programming
    Replies: 2
    Last Post: 10-04-2002, 04:52 PM

Tags for this Thread