0% found this document useful (0 votes)
22 views4 pages

Lab 1 Week 4: - //student ID: //student Name

This document provides instructions and objectives for Computer Programming Lab 1 Week 4. The lab contains 3 activities: [1] modifying and running a kilometers to miles conversion program, [2] writing a program to input and output fractions, and [3] finding syntax errors in a sample program. The overall goals are to identify key elements of a C program, apply structured programming techniques, and use input/output functions.

Uploaded by

Permata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views4 pages

Lab 1 Week 4: - //student ID: //student Name

This document provides instructions and objectives for Computer Programming Lab 1 Week 4. The lab contains 3 activities: [1] modifying and running a kilometers to miles conversion program, [2] writing a program to input and output fractions, and [3] finding syntax errors in a sample program. The overall goals are to identify key elements of a C program, apply structured programming techniques, and use input/output functions.

Uploaded by

Permata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Computer Programming

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: 2

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;

6 // convert the distance


kms = KMS_TO_MILES * mile;

//display the distance in miles


cout << "That is equivalent to : " << kms << " miles." << endl;

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. What can you see?

___________________________________________________________________
___________________________________________________________________
___________________________________________________________________

Can you see the output?

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.

___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________

Modify above program so that it will produce the following output

Change the datatype of kms variable from double to integer


Computer Programming
LAB 1 Week 4

Rewrite the above program that convert kilometers to miles by following this
formula:

1 km = 0.62 mile

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
Computer Programming
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);
}

References:
Labsheet materials of Structured Programming & Database lab. class,
Computer & Information Sciences Department, University Technology Petronas.

You might also like