Nexus Learn

Practical 5: From Algorithm & Flowchart to Code (Top-Down Design)

Lab Practical CS-307 Programming Fundamentals 7 min read Updated 8 Nov 2026
On this page
  1. 1. Lab Objectives
  2. 2. Word Bank
  3. 3. Background
  4. 4. Time Plan (50 minutes)
  5. Part A: Warm-up Trace Table (4 min)
  6. Part B: Predict the Output (3 min)
  7. Part C: Bug Hunt (5 min)
  8. Part D: Graded Lab Tasks (32 min · 10 marks)
  9. Part E: Post-Lab Questions (finish as homework if needed)
  10. Marking Rubric (10 marks)
  11. Submission

Duration

Language

Tools

Linked Lecture

50 minutes

C++

Dev-C++ or Code::Blocks (online compiler as backup)

Lecture 7

Mapped outcomes: CLO1 (problem solving) and CLO4 (C++ syntax) at psychomotor level P3 – Guided Response · PLO3, PLO2, PLO5

1. Lab Objectives

By the end of this 50-minute lab, you will be able to:

  • Dry run code with a trace table and predict its output.
  • Find bugs caused by the wrong order of steps or a wrong formula.
  • Turn a given algorithm into C++.
  • Design your own solution with a structure chart and stepwise refinement, then code and test it.

2. Word Bank

Word

Simple meaning

Real-world example

Algorithm

A finite, clear list of steps

A recipe for chai

Structure chart

Diagram of a problem broken into parts

A family tree, but for tasks

Stepwise refinement

Adding detail to each step, level by level

Planning a trip: city → route → bus time

Dry run

Running steps by hand with real values

Rehearsing a speech

Trace table

Values of variables after each step

A cricket scorecard after every over

3. Background

Lecture 7 gave a method: understand → plan → do → check. Plan with a structure chart and stepwise refinement; check with a trace table. In this lab you will practise the method on small real problems. All programs today are sequential (no if or loops yet).

4. Time Plan (50 minutes)

Part

Activity

Time

—

Briefing

2 min

A

Warm-up trace table

4 min

B

Predict the output

3 min

C

Bug hunt (3 bugs)

5 min

D

Graded tasks 1–3

32 min

E

Post-lab questions & submission

4 min

Part A: Warm-up Trace Table (4 min)

Dry run this program on paper and fill the trace table. Then run it to check.

lab5_warmup.cpp

#include <iostream>

using namespace std;

int main() {

int x = 3, y = 8, z;

z = x + y; // step 1

x = z * 2; // step 2

y = x - y; // step 3

z = z % 4; // step 4

cout << x << " " << y << " " << z << endl;

return 0;

}

Step

x

y

z

Output

start

3

8

?

1

2

3

4

cout

Part B: Predict the Output (3 min)

lab5_predict.cpp

#include <iostream>

using namespace std;

int main() {

int n = 472;

int units = n % 10; // line 1

int tens = (n / 10) % 10; // line 2

int hundreds = n / 100; // line 3

int reversed = units * 100 + tens * 10 + hundreds; // line 4

cout << units << " " << tens << " " << hundreds << endl;

cout << "Reversed: " << reversed << endl;

cout << "Sum of digits: " << units + tens + hundreds << endl;

return 0;

}

My prediction (3 lines)

Part C: Bug Hunt (5 min)

Each program has one bug. Two of them build without errors! Write the wrong output (or message), the cause and the fix.

bug1.cpp — wrong order of steps (input 12 10)

#include <iostream>

using namespace std;

int main() {

double length, width;

double area = length * width; // area of a room

cout << "Enter length and width (ft): ";

cin >> length >> width;

cout << "Area: " << area << " sq ft" << endl;

return 0;

}

Wrong output / message, cause and fix

bug2.cpp — wrong formula

#include <iostream>

using namespace std;

int main() {

int quiz1 = 8, quiz2 = 6, quiz3 = 9;

double average = quiz1 + quiz2 + quiz3 / 3.0;

cout << "Average quiz marks: " << average << endl;

return 0;

}

Wrong output / message, cause and fix

bug3.cpp — variable used before it is declared

#include <iostream>

using namespace std;

int main() {

cout << "Enter price of one kg of sugar: ";

cin >> price;

double price;

cout << "Price of 5 kg: " << price * 5 << endl;

return 0;

}

Wrong output / message, cause and fix

Part D: Graded Lab Tasks (32 min · 10 marks)

Rules for every task

Plan first. For Tasks 2 and 3 show your structure chart and Level 3 pseudocode to the instructor before you start coding.

Number your code comments to match the boxes of your chart (// 1. Input, // 2. …).

Money and decimals: 2 decimal places with fixed and setprecision(2).

Make a trace table for the sample input and check it against your program's output.

Task 1: Petrol Cost of a Trip (Easy — 3 marks)

Four friends drive from Faisalabad to Lahore. Code this given algorithm, then complete the trace table for the sample input.

Algorithm (given)

1 INPUT distance, average, pricePerLitre, friends

2 litres ← distance / average

3 cost ← litres × pricePerLitre

4 share ← cost / friends

5 OUTPUT litres, cost, share

Sample run (the petrol price is an example)

Distance (km): 180

Car average (km per litre): 12

Petrol price (Rs. per litre): 265

Number of friends sharing: 4

Petrol needed : 15.00 litres

Total cost : Rs. 3975.00

Each pays : Rs. 993.75

Step

distance

average

price

friends

litres

cost

share

1

180

12

265

4

2

3

4

Task 2: Bus Arrival Time (Medium — 3 marks)

A Daewoo bus leaves Faisalabad at a given time (24-hour clock) and the journey takes a given number of minutes. Find the arrival time. The bus may arrive after midnight, so the hour must start again from 00.

1. Draw a structure chart (top box + 4–5 boxes).

2. Write Level 3 pseudocode.

3. Make a trace table for 22:45 + 200 minutes.

4. Code and test it with both sample runs.

Hints

Change the time to minutes after midnight: hours × 60 + minutes.

A day has 1440 minutes; % 1440 wraps past midnight.

To print 02:05 (with leading zeros): cout << setfill('0') << setw(2) << hour << ":" << setw(2) << minute; (needs <iomanip>).

Sample run 1

Departure time (hour minute, 24-hour): 22 45

Journey time (minutes): 200

Arrival time: 02:05

Sample run 2

Departure time (hour minute, 24-hour): 8 30

Journey time (minutes): 195

Arrival time: 11:45

Structure chart, Level 3 pseudocode and trace table

Task 3: Salary Slip (Challenge — 4 marks)

Make a salary slip for a university employee. Use constants for all rates. Input the basic pay.

Item

Rule

House rent allowance

45% of basic pay

Medical allowance

10% of basic pay

Gross pay

basic + house rent + medical

GP fund (deduction)

8% of basic pay

Income tax (deduction)

5% of gross pay (simplified example rate)

Net pay

gross − (GP fund + income tax)

1. Draw a structure chart with at least 5 boxes.

2. Refine the 'deductions' box to Level 3.

3. Trace table for basic pay = 60000.

4. Code and test.

Sample run

Basic pay (Rs.): 60000

------- SALARY SLIP -------

Basic pay : Rs. 60000.00

House rent : Rs. 27000.00

Medical : Rs. 6000.00

Gross pay : Rs. 93000.00

GP fund : Rs. 4800.00

Income tax : Rs. 4650.00

Deductions : Rs. 9450.00

NET PAY : Rs. 83550.00

Structure chart, refinement and trace table

Part E: Post-Lab Questions (finish as homework if needed)

Q1. In bug1.cpp the program built without errors but gave a wrong area. Which property of a good algorithm was broken, and what was the cause?

Answer

Q2. Why do we use % 1440 in Task 2?

Answer

Q3. Give two benefits of drawing a structure chart before coding Task 3.

Answer

Marking Rubric (10 marks)

Component

Marks

What earns full marks

Task 1

3

Given algorithm coded in the right order; formatted output; trace table complete.

Task 2

3

Structure chart and Level 3 pseudocode; trace table; % 1440 wrap; two-digit time.

Task 3

4

Chart with refinement; trace table; constants; correct allowances, deductions and net pay.

Parts A–C and the post-lab questions are checked in the viva. Planning marks are given even if the code is incomplete.

Submission

  • Show your plan and running programs to the instructor before leaving.
  • Copy each task's structure chart, trace table, final code and output into your Lab Book (Practical 5 pages).
  • Copying code from a classmate gets zero marks for both students.

Next practical: Practical 6 — Decisions I: if and if-else (Week 4, Lecture 8).

← All CS-307 material