Thread: What do I need to fix?

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    14

    What do I need to fix?

    With the inputs 5, 6, 14, and 4.5, the result should be 374 but i keep getting 4200774. What do i need to fix
    Code:
    #include <stdio.h>
    
    #define PI 3.141592654
    
    int main()
    {
    int layers, radius_top, radius_bottom, feed, y;
    float cake, x, lay_area;
    
    printf("How many layers will your cake have? \n");
    scanf("%d", &layers);
    
    printf("What is the radius of the top layer?\n");
    scanf("%d", &radius_top);
    
    printf("What is the radius of the bottom layer?\n");
    scanf("%d", &radius_bottom);
    
    printf("How much cake in cross-sectional area does each guest need?\n");
    scanf("%f", &cake);
    
    x = (radius_bottom - radius_top) / (layers - 1);
    
    for (y = layers; y > 0; y--)
    { lay_area = (radius_top * radius_top) * PI;
    feed += lay_area / cake;
    radius_top += x; }
    
    printf("Your cake will feed %d guests.", feed);
    
    return 0;
    
    }
    Last edited by Salem; 09-24-2011 at 12:39 AM. Reason: restore original code

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you have one layer, you're going to divide by zero. That's bad. You also never initialize feed.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    14
    Thanks. I didn't realize I had to initialize feed. That was the only problem

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by R3myD View Post
    That was the only problem
    Right, because dividing by zero isn't a problem in math...


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    14
    It is, but the program is giving the correct answers when all i did was initialize feed

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    WOW... massively bad form... You should NEVER edit the first message in a thread...

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    WOW... massively bad form... You should NEVER edit the first message in a thread...
    Don't worry, it looked like this:
    Code:
    #include <stdio.h>
    
    #define SMALL 50
    #define LARGE 200
    
    int main(void)
    {
    float small_cost, large_cost, invite_cost = 0;
    int invites, largepack, smallpack;
    
    printf("What is the cost of a small package (in dollars)?\n");
    scanf("%f", &small_cost);
    
    printf("What is the cost of a large package (in dollars)?\n");
    scanf("%f",&large_cost);
    
    printf("How many invitations are you sending out?\n");
    scanf("%d", &invites);
    
    while (invites >= 150)
    { invites -= 200;
    largepack++; }
    while (invites > 0)
    { invites -= 50;
    smallpack++; }
    
    printf("You should order %d small package(s).", smallpack);
    printf("You should order %d large package(s).", largepack);
    
    invite_cost = (smallpack * small_cost) + (largepack * large_cost);
    
    printf("our cost for invitations will be $%f.", invite_cost);
    
    return 0;
    
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    14
    Oh, I didn't know. I just didn't want it to be copied

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by R3myD View Post
    Oh, I didn't know. I just didn't want it to be copied
    Well in its current state, if it's copied, they'll get a bad grade.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quzah beat me to it...

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by R3myD View Post
    Oh, I didn't know. I just didn't want it to be copied
    Thing is, WE want it left there so that the ensuing discussion at least appears to make sense....

  12. #12
    Registered User
    Join Date
    Sep 2011
    Posts
    14
    That's fine. No problem

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I put the original back for context.

    > Oh, I didn't know. I just didn't want it to be copied
    Your defence is "you published first", so if there is ever any argument, you can always say "look here".
    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.

Popular pages Recent additions subscribe to a feed