Lab 1 Week 4: - //student ID: //student Name
Lab 1 Week 4: - //student ID: //student Name
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
1 #include <iostream.h>
3
#define KMS_TO_MILES 1.609
int main () 4
{
double kms; /*distance in kilometers */
double mile; /*distance in miles */
5
// get the distance in miles 7
cout << "Enter the distance in kilometers : " ;
cin >> mile;
return (0);
}
8
Computer Programming
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 again and test with several possible inputs. Can you see the
output?
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
Delete preprocessor directive #define KMS_PER_MILE 1.609 and save the file as
test_kms.c. Compile the program. What did you get?
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
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.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
Rewrite the above program that convert kilometers to miles by following this
formula:
1 km = 0.62 mile
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
Computer Programming
LAB 1 Week 4
Activity #3
#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);
}
References:
Labsheet materials of Structured Programming & Database lab. class,
Computer & Information Sciences Department, University Technology Petronas.