You are on page 1of 4

TBB2073 Structured Programming & Database LAB 1 Week 4

LAB 1 WEEK 4
Objectives By the end of this lab session, you will be able to 1. Identify the importance of major elements in a C program, 2. Apply the structured approach in developing a programming solution, 3. Use input/output functions in C, and 4. Apply good programming principles (i.e the use of comments).

Activity #1 Label all elements in the C++ program below.

//student ID : //Student Name:

#include <iostream.h> #define KMS_TO_MILES 1.609 4 int main () { double kms; /*distance in kilometers */ double mile; /*distance in miles */ 5 // get the distance in miles cout << "Enter the distance in kilometers : " ; cin >> mile; // convert the distance kms = KMS_TO_MILES * mile; //display the distance in miles cout << "That is equivalent to : " << kms << " miles." << endl; return (0); } 7 3

TBB2073 Structured Programming & Database LAB 1 Week 4

By using a text editor of a C++ compiler package, type the program and save it as kms.cpp into your own directory (student ID). Now, compile the program. If you have some errors, try to correct the syntax errors and compile it again. Repeat this process until your program has no syntax error. Run the program. What can you see? I cant see the result because it is shown too fast. ___________________________________________________________________ ___________________________________________________________________ ___________________________________________________________________ Can you see the output? Add a function named system(pause); before return 0. We use this function to stop (pause) the screen display Run the program again and test with several possible inputs. Can you see the output? Yes,the system has been paused Delete preprocessor directive #define KMS_PER_MILE 1.609 and save the file as test_kms.c. Compile the program. What did you get? Its error. KMS_TO_MILES is undeclared._______________ ___________________________________________________________________ Now, declare KMS_PER_MILE as a variable and assign 1.609 to its memory cell. i.e double KMS_PER_MILE = 1.609; Compile and run the program. Test the running program with the same input set as the previous step. Now, what did you get? Give your comments. The program can be excuted because eventhough we did not define KMS_TO_MILE, but we did declare and assignes a fix value to it.__________________________________________________________________ Modify above program so that it will produce the following output

Change the datatype of kms variable from double to integer Rewrite the above program that convert kilometers to miles by following this formula:

1 km = 0.62 mile

TBB2073 Structured Programming & Database LAB 1 Week 4

and print the result. Compile and run the program. Test with several possible inputs.

Activity #2 Write a program that prompts the user to enter three fractions numbers and then prints them, first forward and then reverse, as shown in the following design. Ensure that the result displayed is in 2 decimal places.

Figure 2 Modify your program so that it display the output vertically (each on one line). Refer to figure 3 below

Figure 3

TBB2073 Structured Programming & Database LAB 1 Week 4

Activity #3 Find 8 syntax errors in the following program: #include iostream int main(); { cout << "Please enter two numbers:" cin << x ; y; // cout << "The sum of " << x << "and " << y << "is : " x+y << "\n";

return(0);