Thread: Can someone please correct my code?

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    17

    Can someone please correct my code?

    Making a simple program for a school assignment and I cant seem to figure it out... its a basic math function using the exponential growth formula which is correct but the computer is not outputting the right numbers, any help would be appreciated!


    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main()
    {
        int blanketsquares, num1, num2, blanket,remainder;
        printf("How many people are knitting blanket squares in the beginning of the week?\n\n");
        scanf("%d", &num1);
    
    
        printf("How many people are knitting blanket squares each day?\n\n");
        scanf("%d", &num2);
    
    
    //forumulas
    
    
        blanketsquares = pow(num1 + num2,7);
        blanket = blanketsquares / 60;
        remainder = blanketsquares % 60;
    
    
    //statement
    
    
        printf("%d blanketsquares will be made this week!\n\n", &blanketsquares);
        printf("You will be able to make %d blankets and start next week with %d squares.\n\n\n\n", blanket, remainder);
    
    
    
    
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    Quote Originally Posted by Alex Coven View Post
    Making a simple program for a school assignment and I cant seem to figure it out... its a basic math function using the exponential growth formula which is correct but the computer is not outputting the right numbers, any help would be appreciated!


    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main()
    {
        int blanketsquares, num1, num2, blanket,remainder;
        printf("How many people are knitting blanket squares in the beginning of the week?\n\n"); //delete \n\n and put a space after ? Nicer format
        scanf("%d", &num1);
    
    
        printf("How many people are knitting blanket squares each day?\n\n");   //delete \n\n and put a space after ? Nicer format
        scanf("%d", &num2);
    
    
    //forumulas
    
    
        blanketsquares = pow(num1 + num2,7); //put 7.0 instead of 7
        blanket = blanketsquares / 60;
        remainder = blanketsquares % 60;
    
    
    //statement
    
    
        printf("%d blanketsquares will be made this week!\n\n", &blanketsquares); //get rid of &
        printf("You will be able to make %d blankets and start next week with %d squares.\n\n\n\n", blanket, remainder);
    
    
    
    
      return 0;
    }
    Hi,
    I have found a solution but don't know the details. Perhaps someone more experienced can fill in the blanks..

    Please see this website. Both arguments must be of type double
    pow() - C Library Function Example

    From above, the second argument must have a decimal so 7.0 instead of 7. Not sure what is happening with the first value (base value).

    Also, printf does not have an '&' --> second last printf. Please correct
    Last edited by bos1234; 09-03-2013 at 05:59 PM. Reason: added comments to code

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The pow statement is okay as it is. The compiler will automatically "promote" the int values to doubles. https://www.securecoding.cert.org/co...nversion+rules

    The ampersand is the big error, although the overall logic is weird.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Actually, if you pass an integer to a function that expects a double, the integer gets automatically promoted to a double.

    The problem with this program is that you're printing the address of blanketsquares rather than the contents of the variable.
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    As above solutions, code runs fine as it is. Change printf statement
    However, on visual studio the pow function is underlined. Intellisense says:

    Code:
    	4	IntelliSense: more than one instance of overloaded function "pow" matches the argument list:
                function "pow(double _X, int _Y)"
                function "pow(float _X, int _Y)"
                function "pow(long double _X, int _Y)"
                argument types are: (int, int)	                                                                       	19	22	testbed

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by bos1234 View Post
    As above solutions, code runs fine as it is. Change printf statement
    However, on visual studio the pow function is underlined. Intellisense says...
    That means you're compiling it as C++.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Can someone please correct my code?
    No, we do not directly correct code; We correct the coder!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can anybody help me correct this C++ code?
    By Amanda2 in forum C++ Programming
    Replies: 9
    Last Post: 10-04-2007, 04:46 AM
  2. Is this code correct?
    By Sridar in forum C++ Programming
    Replies: 3
    Last Post: 09-05-2004, 03:45 PM
  3. correct this code please
    By enjoy in forum C Programming
    Replies: 4
    Last Post: 04-28-2004, 02:33 PM
  4. Is this code correct?
    By Unimatrix_001 in forum Windows Programming
    Replies: 3
    Last Post: 07-08-2003, 10:23 PM