CPPForSchool.com: Variable, Operator & Expression Set 3: Assignment 1
1.
Write a program that takes length as input in feet and inches. The
program should then convert the lengths in centimeters and display it on
screen. Assume that the given lengths in feet and inches are integers.
Based on the problem, you need to design an algorithm as follows:
1. Get the length in feet and inches.
2. Convert the length into total inches.
3. Convert total inches into centimeters.
4. Output centimeters.
To calculate the equivalent length in centimeters, you need to multiply the total inches by 2.54. Instead of using the value 2.54 directly in the program, you will declare this value as a named constant. Similarly, to find the total inches, you need to multiply the feet by 12 and add the inches. Instead of using 12 directly in the program, you will also declare this value as a named constant. Using a named constant makes it easier to modify the program later.
To write the complete length conversion program, follow these steps:
1. Begin the program with comments for documentation.
2. Include header files, if any are used in the program.
3. Declare named constants, if any.
4. Write the definition of the function main.
Before I begin coding algorithms for conversions, I need to create foundation code to ensure the flow of the menu to sub-menu options work correctly. In order to do this, I will use place holder statements to simply output the menu name in order to ensure the code is executing within the expected order.
So far so good, now onto the algorithms!
Since the program will be handling feet and inches, feet will need to first be converted to inches before converting everything into centimeters. There are 12 inches in 1 foot. With that said, the conversion formula for feet to inches will be the following:
feet * 12 == inches
**The information below was already provided, but I did some additional research for my own curiosity**
Admittedly, I had to research the conversion formula for inches to centimeters. It appears that the conversion was eventually settled in a simplified format. However, originally, the best way to convert from inches to centimeters was the following:
cm = in/0.39370 = 2.54000508001016
This was simplified to 1 inch = 2.54 cm. The algorithm used for inches to cm is inches * 2.54.
Now that both equations have been created, it is time to implement code. Notice that I have added constant variables in order to meet the requirements of the assignment, specifically for conversions.
Also, I decided to compare my results with a well established conversion tool, specifically Inch Calculator.
Based on the problem, you need to design an algorithm as follows:
1. Get the length in feet and inches.
2. Convert the length into total inches.
3. Convert total inches into centimeters.
4. Output centimeters.
To calculate the equivalent length in centimeters, you need to multiply the total inches by 2.54. Instead of using the value 2.54 directly in the program, you will declare this value as a named constant. Similarly, to find the total inches, you need to multiply the feet by 12 and add the inches. Instead of using 12 directly in the program, you will also declare this value as a named constant. Using a named constant makes it easier to modify the program later.
To write the complete length conversion program, follow these steps:
1. Begin the program with comments for documentation.
2. Include header files, if any are used in the program.
3. Declare named constants, if any.
4. Write the definition of the function main.
Before I begin coding algorithms for conversions, I need to create foundation code to ensure the flow of the menu to sub-menu options work correctly. In order to do this, I will use place holder statements to simply output the menu name in order to ensure the code is executing within the expected order.
So far so good, now onto the algorithms!
Since the program will be handling feet and inches, feet will need to first be converted to inches before converting everything into centimeters. There are 12 inches in 1 foot. With that said, the conversion formula for feet to inches will be the following:
feet * 12 == inches
**The information below was already provided, but I did some additional research for my own curiosity**
Admittedly, I had to research the conversion formula for inches to centimeters. It appears that the conversion was eventually settled in a simplified format. However, originally, the best way to convert from inches to centimeters was the following:
cm = in/0.39370 = 2.54000508001016
This was simplified to 1 inch = 2.54 cm. The algorithm used for inches to cm is inches * 2.54.
Now that both equations have been created, it is time to implement code. Notice that I have added constant variables in order to meet the requirements of the assignment, specifically for conversions.
Also, I decided to compare my results with a well established conversion tool, specifically Inch Calculator.
Comments
Post a Comment