Lab Exercises 1
Lab Exercises 1
Lab Exercises 1
EXERCISE 1:
1.
//Program 1.1
//Nama: Masitah Ghazali
#include <stdio.h>
#include <conio.h>
int main(void)
{
printf ("Hai!! Apa khabar?\n");
getch();
return 0;
}
2.
/* 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.
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.
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.
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.
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
Start
Read the Collection
Determine the total for ringgit using the formula:
Ringgit = Collection / 100
4.
5.
6.
7.
8.
9.
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 NormalHours = 20
Set OverTimeHours = 8
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
Display "Total
payment"
Read Payment
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.
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:
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:
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:
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:
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
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 triangle