COMP122 Structured Programming
COMP122 Structured Programming Page 1
Course Title: Structured Programming
Term: Fall 2013
Item: Lab04
Due Date: End of Week04
Problem01:
Construct a C++ program that prompts the user for an input (a number), then finds the number in the Fibonacci sequence of numbers. By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. Your code will involve Embedded Loops – use While for the first one and FOR loop for the second one.
Problem01:
Construct a C++ program that prompts the user for an input (a number), then finds the number in the Fibonacci sequence of numbers. Your code will involve Embedded Loops – use While for the first one and FOR loop for the second one.
Logic Design:
1. Prompt the user for a number (first and second numbers in Fibonacci are 1s).
2. Accept the number into a variable
3. Begin an outer loop (use a While Loop structure)
If input value < 1
1. Display a message indicating that the number must be greater than 1
2. Allow the user to try again
Else
If input > 2
Outer Loop Begin the inner loop - use a FOR Loop structure:
for(int kounter = 2; kounter < input; kounter++)
{
Inner loop 1. backwardNumber2 = backwardNumber1
2. backwarNumber1 = currentNumber
3. currentNumber = backwardNumber1 + backwardNumber2
}
End of inner loop
End of Outer loop
4. Display input, backwardNumber1, backwardNumber2, and currentNumber
Documentation:
Task #1: Identify all variables to be used in your program. Indicate their datatypes
Using the table given below, identify each variable and its appropriate datatype that is required to make this program work.
|
Variable Name |
Datatype |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Task #2: Design the IPO chart for your program
Task #3: Programming:
Using a C++ Console Application, code and execute the program that represents your IPO chart.
Task #4: Test your program with the following:
|
Input |
BackwardNumber1 |
BackwardNumber2 |
Fibonacci Sequence |
|
12 |
89 |
55 |
144 |
|
46 |
1134903170 |
701408733 |
1836311903 |
Sample Output:
Sample Output
Problem02: Temperature conversion
Code a C++ program that accepts a temperature and a conversion type. You program will then convert each one to either Fahrenheit or Celsius, depending on the Conversion type that the user enters.
Display the temperature and its equivalent. Hint: this requires a loop.
The formula for temperature conversion:
Fahrenheit = Celsius * (9/5) + 32
Celsius = (Fahrenheit – 32) * 5/9
Logic Design:
1. Prompt user for number of temperatures to convert (no more than 5)
2. Accept temperature into its variable
3. Begin a loop from 1 through numTemp (number of temperatures to convert)
1. Prompt the user to enter a temperature to be converted
2. Accept temperature into its variable
3. Prompt the user to enter a conversion type ( F for Fahrenheit or C for Celsius). Any other character is illegal – display an indicative message.
4. Accept conversion type into its variable
5. Perform necessary calculation based on conversion type:
Fahrenheit = Celsius * (9/5) + 32
Celsius = (Fahrenheit – 32) * 5/9
Hint : you will need an IF statement to determine which of the two calculations you will need
6. Display the following:
a. input temperature
b. conversion type
c. converted temperature
End Loop
Documentation:
Task #1: Identify all variables to be used in your program. Indicate their datatypes
Using the table given below, identify each variable and its appropriate datatype that is required to make this program work.
|
Variable Name |
Datatype |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Task #2: Design the IPO chart for your program
Task #3: Programming:
Using a C++ Console Application, code and execute the program that represents your IPO chart.
Task #4: Test your program with the following:
|
|
Temperature |
Conversion Type |
Converted Temperature |
|
First Execution |
68 |
C |
20 Fahrenheit |
|
Second execution |
32 |
F |
64 degrees Celsius |
|
Third execution |
45 |
M |
Illegal conversion type |
Sample Output:
For Submission in eCollege Lab04 DropBox:
a. IPO Charts
b. Data Analysis Sheets
c. Source Code Document (two programs)
d. Screen Shot of Output (two programs)
J. Anagbogu/COMP122/Fall 2013 Lab04/Loops Page 1 of 6