CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2010
    Posts
    3

    Write an algorithm for this problem (Simple)

    Okay so I'm in a community college, i'm taking a programming class and I have no clue what i'm doing. Can someone help me out with this one problem? The book we use doesn't have examples like these. Thanks.

    Write an algorithm for this problem:

    The user enters a length from the keyboard in centimeters. The length is then converted to inches (to the nearest inch) to switch to the English measurement system. The length is then output in yards, feet, and inches.

    Two parts must be submitted:

    1. To do list
    2. Algorithm

    Submissions must be in Word or RTF format.

    This is an algorithm and must not contain C++ code.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Write an algorithm for this problem (Simple)

    Well, what have you tried?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Sep 2010
    Posts
    3

    Re: Write an algorithm for this problem (Simple)

    Quote Originally Posted by laserlight View Post
    Well, what have you tried?
    Give me a few minutes and i'll get back to you on that one. In the process of reading the chapter more thoroughly, sorry if it came off as asking for the answer, i'm just confused as to where to start.

  4. #4
    Join Date
    Sep 2010
    Posts
    3

    Re: Write an algorithm for this problem (Simple)

    To do list:
    Get the length in centimeters.
    Calculate inches.
    Output - Yards:Feet:Inches

    Algorithm:
    Prompt for centimeters.
    Get centimeters.
    Yards = Centimeters div 91.44
    Centimeters = Yards mod 91.44
    Feet = Centimeters div 30.48
    Centimeters = Feet mod 30.48
    Inches = Centimeters div 2.54

    I took an example that was converting seconds, minutes and hours and tried to work around that. Still somewhat confused :\.

  5. #5
    Join Date
    Sep 2010
    Posts
    7

    Re: Write an algorithm for this problem (Simple)

    Quote Originally Posted by burdturd View Post
    To do list:
    Get the length in centimeters.
    Calculate inches.
    Output - Yards:Feet:Inches

    Algorithm:
    Prompt for centimeters.
    Get centimeters.
    Yards = Centimeters div 91.44
    Centimeters = Yards mod 91.44
    Feet = Centimeters div 30.48
    Centimeters = Feet mod 30.48
    Inches = Centimeters div 2.54

    I took an example that was converting seconds, minutes and hours and tried to work around that. Still somewhat confused :\.
    let me try to explain the idea in the centimeter/meter/kilometer because im not familiar with the other measurement system.

    read cm; //or inch

    so uv got the amount of cm's in one variable. to get the amount of maximum km's we divide the amount of cm's by 100,000( 1km = 100,000 cm) and the integer type variable will round it off downwards giving us the amount of kilometers.

    for example if the amount of cm's is 523,112, the km will be 523112/100000 = 5.23112 = 5 km's,
    so the command is:

    km = cm / 100000;

    so we know that the amount of km's yearns in the km variable (5). now before we move to calculating the amount of meters we must update our cm variable and remove the amount of kilometers which we already posses in another variable.
    so from 523,122 we need to remove 5 km which is 500,000 cm. so we do this:

    cm = cm - km * 100000;
    OR:
    cm = cm modulo 100000; // which means: what is the remainder of dividing 523122 by 100000 and the answer is: 23122

    now the cm variable contain 23,112 which is the amount of cm's and meters. to get the meters its the same idea:

    m = cm / 100; // m = 231

    and then:

    cm = cm - m * 100; // cm = 12;
    OR
    cm = cm modulo 100;

    by now the cm's, m's and km's variables contain all of their right amounts and all the is left is to print them:

    exactly the same idea with the inches yards and feet, or hours minutes and second.
    i hope that was helpful, if u got any questions please ask.
    Last edited by HappyNerd; September 28th, 2010 at 04:27 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured