Thread: Today's easy question: Can you spot the problem?

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    99

    Today's easy question: Can you spot the problem?

    I can't, but I know it's something easy...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
        int first_eight_powers_of_two[7];
        int i;
        double sum_thing=2;
        int j;
            for(i=0; i<8; i++)
            {
                first_eight_powers_of_two[i]=sum_thing;
                sum_thing*=2;
            }
        i=0;
        
            do
            {
                printf("2 to the %d is:  %ld\n", i+1, first_eight_powers_of_two[i]);
                i++;
            }while(i<8);
    
        return EXIT_SUCCESS;
    }

  2. #2
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Your compiler would help you, if you just let it. Enable all warnings in GCC, and you'll get
    Code:
    gratiafide.c: In function ‘main’:
    gratiafide.c:18:13: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 3 has type ‘int’ [-Wformat]
    gratiafide.c:8:9: warning: unused variable ‘j’ [-Wunused-variable]
    gratiafide.c:11:38: warning: array subscript is above array bounds [-Warray-bounds]
    gratiafide.c:18:19: warning: array subscript is above array bounds [-Warray-bounds]
    Just start at line 5. Doesn't it strike you as odd that you say "eight", but define the array to have just seven elements?

  3. #3
    Dweeb dojha00's Avatar
    Join Date
    Feb 2012
    Location
    Global
    Posts
    23
    Quote Originally Posted by gratiafide View Post

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
        int first_eight_powers_of_two[7];
        int i;
        double sum_thing=2;
        int j;
            for(i=0; i<8; i++)
            {
                first_eight_powers_of_two[i]=sum_thing;
                sum_thing*=2;
            }
        i=0;
        
            do
            {
                printf("2 to the %d is:  %ld\n", i+1, first_eight_powers_of_two[i]);
                i++;
            }while(i<8);
    
        return EXIT_SUCCESS;
    }
    Make both of them i<7 because it has 7 elements starting from 0 to 6.
    What is life??

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    99
    Last night while laying in bed I realized the problem was that I had the array of size [7] without even looking at my program....

    I had done 7 because I wanted to see if 7 meant 0-7, but as dojha00 points out it is actually 0-6. Thanks for the help

    BTW how do I turn on all the compiler error notifications in code blocks? Also, is there a way to make code blocks compatible with a c99 compiler? Thanks.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    settings -> compiler and debugger...

    Check out the "compiler flags" at your disposal.

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    99
    Nominal animal,

    Can you please explain the warning #2 your complier got? "gratiafide.c:18:13: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 3 has type ‘int’ [-Wformat]"

    Isn't %ld correct with double variables?

    Thanks.

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    99
    Or if anyone else could explain it, that'd be swell.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    {
        int first_eight_powers_of_two[7];
        int i;
    
        // ...
    
        printf("2 to the %d is:  %ld\n", i+1, first_eight_powers_of_two[i]);
    }
    "first_eight_powers_of_two[7]" is an array of integers - which doesn't appear to be what you want - but that would use %d since it's [an integer that's] not long.

    "%f" is used to print doubles with "printf()".

    You're probably thinking of needing "%lf" for doubles with "scanf()"

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Are you sure this is correct: first_eight_powers_of_two[i]=sum_thing;
    You should decide if you want double (floating point double precision) or integer. If you truly want to convert one to the other then please use floor(...) and/or (int) casting.
    Then when you have figured out which type of data to store, then the printf format specifier will be obvious: %f for float or %d for int.

  10. #10
    Registered User
    Join Date
    Oct 2012
    Posts
    99
    Yeah, I originally had it as an int, but when I was getting the array problem I changed it to double thinking that I had hit the ceiling. Thanks for the info.

    I don't know if there are any compiler settings in particular you can suggest for me. That is a long list, and I tried selecting about a third of them but then couldn't compile things at all.

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Don't start selecting things that you don't understand! What do you want included? "Enable all compiler warnings" is a good one to have checked.

  12. #12
    Registered User
    Join Date
    Oct 2012
    Posts
    99
    Any way to make it c99 compatible?

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Check the internet.

  14. #14
    Registered User
    Join Date
    Oct 2012
    Posts
    99
    I have Pelles. That's c99 compatible. But I kind of like Code Blocks.

  15. #15
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. easy C question on problem with loop/output
    By prafiate in forum C Programming
    Replies: 12
    Last Post: 06-10-2012, 04:01 PM
  2. easy question
    By mrsirpoopsalot in forum C Programming
    Replies: 5
    Last Post: 09-07-2006, 05:47 AM
  3. Easy question, (should be) easy answer... ;-)
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-12-2002, 09:36 PM
  4. Can't spot the problem...am i even on the right track?
    By iluvmyafboys in forum C++ Programming
    Replies: 8
    Last Post: 02-05-2002, 09:26 PM
  5. Spot the problem..a test question, pls help!
    By Unregistered in forum C++ Programming
    Replies: 10
    Last Post: 02-05-2002, 01:40 AM