Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab Exercises 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Lab Exercises 1(Updated)

EXERCISE 1:
1.

The annotation of program 1.1 is as in page 5 will be as follows:


1
2
3
4
5
6
7
8
9
10
11

//Program 1.1
//Nama: Masitah Ghazali
#include <stdio.h>
#include <conio.h>
int main(void)
{
printf ("Hai!! Apa khabar?\n");
getch();
return 0;
}

Briefly annotate what each line of code means.


As for example:
Answer: Line 1 and 2 are comments. A comment is a piece of descriptive text (letters,

number or special symbols) which explains some aspect of a program. Program


comments are considered as non-executable statements that are totally ignored by the
compiler and are only intended for human readers. In program 1.1 above, the symbol //
are used to begin the comment in a line.
Answer: Lines 4 and , Line 7, Line 8, Line 9, Line 10, Line 11 and Line 12?

2.

Given program 1.3 below:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

/* Program 1.3
Nama pengaturcara: Masitah Ghazali
Tujuan aturcara : Masukkan nama dan tahun lahir
serta kira usia pengguna pada tahun ini */
#include <stdio.h>
#include <conio.h>

//Pre-processor directive
//Pre-processor directive

int main()
{
const int TahunIni=2010;
char nama[20];
int tahunlahir, umur;
printf ("Masukkan nama anda: ");
scanf ("%s", &nama);
printf ("Masukkan tahun anda dilahirkan: ");
scanf ("%d", &tahunlahir);
umur = TahunIni - tahunlahir;

23
24
25
26
27

i.

Type, save, compile, and run program 1.3 above. What will be the output of the
program for the given input?
Briefly annotate what each line of code means.

ii.

3.

printf ("Nama anda: %s", nama);


printf ("\nUsia anda: %d", umur, "tahun.");
getch();
return 0;
}

Given program 1.4 below:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

i.
ii.

//Program 1.4
//Nama pengaturcara: Masitah Ghazali
#include <stdio.h>
#include <conio>
int main (void)
{
int workDays;
float workHours, payRate, weeklyPay;
workDays = 5;
workHours = 6.5;
payRate = 20.50;
weeklyPay = workDays * workHours * payRate;
printf ("Weekly Pay = %f", weeklyPay);
printf ("\n");
getch();
return 0;
}

Type, save, compile, and run the program. What will be the output of the
program?
Briefly annotate what each line of code means.

Exercise 2. Develop the pseudocode as in Algorithm 2.3 that converts the distance in mile to
kilometer. Then trace the content of the variables and determine the output of Algorithm
2.3.
Algorithm 2.3: Convert the distance in mile to kilometer
1.
2.
3.
4.

Start
Set KM_PER_MILE to 1.609
Set Distance_Mile to 100
Convert the distance in mile to kilometers using the
formula:
Distance_Kilo = KM_PER_MILE x Distance_Mile

5.

Display the distance in kilometer

Exercise 3. Develop a pseudocode that assigns values to variables as in Algorithm 2.4. Then
trace the content of the variables and determine the output of the Algorithm 2.4.

Algorithm 2.4: Determine the content of variables


1.
2.
3.
4.
5.
6.

Start
Set Data1 = 5
Set Data2 = 2.5
Data3 = Data1 + Data2
Data2 = Data3 x Data1
Display Data1, Data2, Data3

Exercise 4. Develop a pseudocode that reads the radius of a circle, and computes the area of
the circle as in Algorithm 2.5.

Algorithm 2.5: Compute the area of a circle


1.
2.
3.
4.

i.

Start
Set PI = 3.14159
Read the Radius
Calculate the area of a circle using the formula:
Area = Radius x Radius x PI

Trace the content of the variables and determine the output of the Algorithm 2.5
if the input for Radius is:
3
10
150

Exercise 5. Develop a pseudocode that reads the money collection of a user. The
algorithm computes the number of Ringgit and cents (50, 20, 10 5, and 1 cent) that can
be extracted from the collection. Tips: You can use the Modulus operator that obtains
the remainder from a division.
i.

Trace the content of the variables and determine the output of Algorithm 2.6 if
the input for the money collection is as follows:
a.
927
b.
1274
c.
6785
d.
35565

Algorithm 2.6: Compute Money Change from Collection to


Ringgit and cents
1.
2.
3.

Start
Read the Collection
Determine the total for ringgit using the formula:
Ringgit = Collection / 100

4.

Determine the remaining amount using the formula:


Remain_Amt = Collection Modulus 100

5.

Determine the total 50 cents in the remaining


amount using the formula:
Fifty_Cent = Remain_Amt / 50

6.

Determine the remaining amount using the formula:


Remain_Amt = Remain_Amt Modulus 50

7.

Determine the total 20 cents in the remaining


amount using the formula:
Twenty_Cent = Remain_Amt / 20

8.

Determine the remaining amount using the formula:


Remain_Amt = Remain_Amt Modulus 20

9.

Determine the total 10 cents in the remaining


amount using the formula:
Ten_Cent = Remain_Amt / 10

10. Determine the remaining amount using the formula:


Remain_Amt = Remain_Amt Modulus 10

EXERCISE 6:
1.

Using the Microsoft Word software, draw a flowchart as in Figure 2.10 that calculates
the payment for normal and overtime working hours of an employee. Then trace the
content of the variables and determine the output displayed from the algorithm.

START

Set NormalPayRate = 10.00

Set NormalHours = 20

Set OvertimePayRate = 5.00

Set OverTimeHours = 8

Calculate TotalPay = NormalPayRate x


NormalHours + OverTimePayRate x
OverTimeHours

Display TOTAL

END

Figure 2.10

2.

Using the Microsoft Word software, draw a flowchart as in Figure 2.11 that calculates
the total cost of item purchased.

Start

Read
ItemName

Read
PricePerUnit

Read Quantity

TotalCost = Quantity * PricePerUnit

Display "Total
payment"

Read Payment

PayChange = Payment - TotalCost

Display ItemName,
Payment, TotalCost and
PayChange

End

Figure 2.11

i.

Trace the content of the variables and determine the output of the algorithm if
the input values are as follows:
a.

ItemName: Pencil
PricePerUnit: 0.50
Quantity: 15
Payment: 10.00

b.

ItemName: Note book


PricePerUnit: 2.00
Quantity: 5
Payment: 50.00

c.

ItemName: Pencil
PricePerUnit: 3
Quantity: 1
Payment: 5.00

EXERCISE 9:
Given the following set of problems. Analyze each problem and identify the input, formula and the
output. Write the pseudocode and draw the flowchart for the problem.

1.

Read three numbers. Calculate the sum of those three numbers and find the average.
Display all the three numbers, as well as the total of those numbers and the in average.
[Baca tiga nombor. Kira dan jumlahkan ketiga-tiga nombor dan cari purata. Paparkan ketigatiga nombor, dan juga jumlah ketiga-tiga nombor serta puratanya.]

Output:

Input:

Formula:

Pseudocode design of the problem:

Flowchart design of the problem:

2.

Calculate the average weight of four students. Attempt to analyze this problem by
stating its input, output and the formula required to determine the average weight.
Display each students weight and the average weight calculated.
Output:

Input:

Formula:

Pseudocode design of the problem:

Flowchart design of the problem:

3.

Convert the time entered by the user in the form of day, hours and minute to minutes.
The conversion unit is as follows:
1 day is equivalent to 24 hours
1 hour is equivalent to 60 minutes
Output:

Input:

Formula:

Pseudocode design of the problem:

Flowchart design of the problem:

4.

Convert the temperature entered by the user in the unit of Fahrenheit to the unit of
Celcius. The conversion formula is as follows:
Fahrenheit =
Output:

Celsius x 9
5

+ 32

Input:

Formula:

Pseudocode design of the problem:

Flowchart design of the problem:

EXERCISE 10:
Given the following problem. Analyze the problem and design its solution using structured chart. Then
write the appropriate pseudocode or flowchart.

1.

A student has designed an object as shown in Figure 2.12. You need to calculate both
the perimeter and the area of the object, based on the length and the width of the
rectangle entered by the user, as well as the height of the triangle.

Figure 2.12
Calculate the perimeter
and area of an object

Get the input

Calculate the
total perimeter

Calculate the
total area

Display the
total perimeter
and total area

values

Calculate the
perimeter of
circle

Calculate the
perimeter of a
triangle side

Calculate the
area of circle

Calculate the area


of rectangle

Calculate the
area of triangle

You might also like