Thread: Homework Porblem

  1. #1
    Registered User
    Join Date
    Jun 2006

    Homework Porblem

    I am a beginner in C++ and am currently taking the class online. I have a problem that I do not know where to begin on.

    Ok here is the problem...Write a C++ program that allows users to enter the percentage of the moons face that appears illuminated, and that outputss the surface area of that portion of the moon. The formula for the surface area of a segment of a sphere is

    S= 2 * (radius * radius) * angle of wedge in radians

    There are two radians in a circle, so the hemisphere of the moon that we see accounts for at most 3.1415927 radians. Thus if the user enters 100% (full moon) the angle of wedge is 3.1415927=

    S= 2 * (1738.3 * 1738.3) * 3.1415927= 18985818.672 square kilometers

    If the user enters 50%(first or first quarter) then the angle of the wedge is 3.415927 * 0.5 and so on. Be sure to use proper formating and appropriate comments in your code. Provide appropriate prompts to the user. The output should be labeled clearly and formatted neatly ( limit the decimal precision to three places as in the example above.

    Please help me.

  2. #2
    Registered User
    Join Date
    Apr 2006
    int x;
    double angofwed = 3.1415927, rad = 1738.3, surface;
    cout << "Enter percentage: ";       // prompt
    cin >> x;                                        // input
    x = x / 10;
    surface = 2 * (rad * rad) * (angofwed * x); // works out the surface
    cout << "\n---\n SURFACE: " << surface << " square kilometers"; // displays answer
    Something like that I guess... But I got no idea how you would do it to 3dp, google maybe?

  3. #3
    irregularly symmetrical n3v's Avatar
    Join Date
    Mar 2006
    you don't have to actually have the number really be 3 decimal points, just multiply first, and adjust the output. here's what you should do

    -first, multiply the surface area of the moon by 100, getting everything in front of the decimal point

    -then, calculate the percentage, by multiplying the input by 0.01. like, the user types in 50, multplying by 0.01 produces 0.5

    -multiply the result with the surface area. now you've got the number you want, the decimal number just isn't in place

    -convert the integer to a string, or better yet, an array of chars, and output all except the last 3, then output a dot, then output the last 3.

    but oh, how do we convert an integer into a char array, you ask? one simple command that's in the default libraries: itoa(). check out the msdn entry

    and uh, when it comes to everything else, uh, i think stickygoo pretty much has it right
    Last edited by n3v; 06-24-2006 at 07:06 AM.
    If you make a man a fire,
    he will be warm for a day.
    If you set a man on fire,
    he will be warm for the rest of his life.

  4. #4
    Join Date
    Jan 2002
    Quote Originally Posted by jwmonroe
    I am ... currently taking the class online...Please help me.
    I refer you to cprogramming's homework policy; we are not in the habit of handing out solutions, although you seemed to have struck lucky to some extent with StickyGoo's post.

    Post your own attempt at the code and describe any problems you have encountered.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > and that outputss the surface area of that portion of the moon
    I thought the phases of the moon were defined by crescents

    not segments

    Better make sure you're answering the right question.
    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.

  6. #6
    Registered User
    Join Date
    Apr 2006
    sorry ken, havn't read that policy :S

    I'll remember for next time though.

  7. #7
    Registered User
    Join Date
    Jun 2006
    Are you in the RODP class? I'm taking the RODP class through Columbia State, and have done that same program. I won't give you my code, but if you still need help with the problem, or anything else later in the class, feel free to e-mail me. wave.of.atla AT gmail DOT com

  8. #8
    Registered User
    Join Date
    Jun 2006
    I'm sorry I didn't know there was a no homework policy, but I really do thank you guys for the help.

  9. #9
    Registered User
    Join Date
    Jun 2006
    Yes, I am still in the class. I don't have much more left though. Thank you for the offer for your help, I may need it in the future. This class has been pretty tough for me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of
    Replies: 11
    Last Post: 11-03-2001, 04:39 PM
  2. Homework
    By kermi3 in forum C Programming
    Replies: 10
    Last Post: 09-27-2001, 04:49 PM
  3. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  4. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM