Duration | Language | Tools | Linked Lecture |
|---|---|---|---|
120 minutes | C++ | Dev-C++ or Code::Blocks (online compiler as backup) | Lecture 2 |
Mapped outcomes: CLO4 (C++ syntax and semantics) and CLO1 (problem solving) at psychomotor level P3 – Guided Response · PLO2, PLO3, PLO5
1. Lab Objectives
By the end of this lab, you will be able to:
- Declare variables of type int, double, char, bool and string, and give them values.
- Take input from the user with cin and show results with cout.
- Use the arithmetic operators + - * / % in real calculations.
- Avoid the integer division trap and the garbage value trap.
- Trace (dry run) a program and predict its output before running it.
2. Word Bank (Difficult Words Made Easy)
Word | Simple meaning | Real-world example |
|---|---|---|
Variable | A named box in memory that holds a value which can change. | A labelled jar in the kitchen: "Sugar". You can refill it with a different amount. |
Data type | The kind of value a variable can hold. | A shoe box holds shoes, not water. An int box holds whole numbers, not text. |
Declare | To create a variable by writing its type and name. | Writing a name on an empty jar. |
Initialise | To give a variable its first value. | Filling the jar on the same day you label it. |
Prompt | A message that tells the user what to type. | An ATM screen saying "Enter your PIN". |
Garbage value | An unknown leftover value in a variable that was never given a value. | Using an old jar without cleaning it — something unknown is still inside. |
Integer division | Dividing two whole numbers; the decimal part is thrown away. | Sharing 7 rotis between 2 people: each gets 3, 1 is left over. |
Modulus (%) | The remainder after division. | The 1 roti left over in the example above. |
Dry run | Following a program by hand, line by line, writing the value of each variable. | Checking a bill line by line with a calculator. |
3. Background: Memory Boxes and the Input Arrow
Every variable is a box in RAM. cin >> x; takes what the user types and puts it into box x. cout << x; takes a copy of the value in box x and shows it on the screen. The arrows point the way the data moves.
Statement | Direction of data | Memory after (user types 19) |
|---|---|---|
int age; | — | age = ? (garbage) |
cin >> age; | keyboard → age | age = 19 |
cout << age; | age → screen | age = 19 (unchanged) |
Real-world example A bank deposit slip is like cin: money goes into your account (variable). A bank statement is like cout: it shows the balance, but the money stays in the account. |
4. Time Plan
Part | Activity | Time |
|---|---|---|
A | Warm-up: type, run and trace | 15 min |
B | Predict the output | 15 min |
C | Bug hunt: find and fix errors | 20 min |
D | Graded lab tasks | 60 min |
E | Post-lab questions and submission | 10 min |
Part A: Warm-up — Type, Run and Trace (15 min)
Type this program (do not copy-paste), save it as lab2_warmup.cpp, then compile and run it. Enter your own age and height.
lab2_warmup.cpp #include <iostream> using namespace std;
int main() { int age; double height;
cout << "Enter your age: "; cin >> age; cout << "Enter your height in feet: "; cin >> height;
cout << "Next year you will be " << age + 1 << " years old." << endl; cout << "Your height is " << height << " feet." << endl; return 0; } |
Sample output Enter your age: 19 Enter your height in feet: 5.6 Next year you will be 20 years old. Your height is 5.6 feet. |
Trace table: fill in the value of each variable after each line runs (use ? for garbage).
Line executed | age | height |
|---|---|---|
int age; | ||
double height; | ||
cin >> age; (you type 19) | ||
cin >> height; (you type 5.6) |
Part B: Predict the Output (15 min)
First write your prediction for each line in the table. Then type and run the program and write what really happened. If they are different, find out why.
lab2_predict.cpp #include <iostream> using namespace std;
int main() { int a = 17, b = 5; cout << a / b << endl; // 1 cout << a % b << endl; // 2 cout << a / 2.0 << endl; // 3 cout << "a + b" << endl; // 4 cout << a + b << endl; // 5 double d = 7 / 2; cout << d << endl; // 6 return 0; } |
# | Statement | My prediction | Actual output |
|---|---|---|---|
1 | cout << a / b; | ||
2 | cout << a % b; | ||
3 | cout << a / 2.0; | ||
4 | cout << "a + b"; | ||
5 | cout << a + b; | ||
6 | double d = 7 / 2; cout << d; |
Part C: Bug Hunt (20 min)
Each program has one mistake. Bugs 1–3 stop the program from compiling (compile errors). Bugs 4–5 compile and run, but give a wrong answer — these are logic errors, and the compiler cannot find them for you. Type each program, find the bug, fix it, and run again.
Bug 1 (bug1.cpp)
#include <iostream> using namespace std;
int main() { int age; cout << "Enter your age: "; cin << age; cout << "You are " << age << " years old." << endl; return 0; } |
Error message / wrong output, and your fix |
Bug 2 (bug2.cpp)
#include <iostream> using namespace std;
int main() { char grade = "A"; cout << "Your grade is " << grade << endl; return 0; } |
Error message / wrong output, and your fix |
Bug 3 (bug3.cpp)
#include <iostream> using namespace std;
int main() { int a = 40, b = 25; total = a + b; cout << "Total = " << total << endl; return 0; } |
Error message / wrong output, and your fix |
Bug 4 (bug4.cpp)
#include <iostream> using namespace std;
int main() { int a = 3, b = 4; double avg = (a + b) / 2; cout << "Average: " << avg << endl; // expected 3.5 return 0; } |
Error message / wrong output, and your fix |
Bug 5 (bug5.cpp)
#include <iostream> using namespace std;
int main() { int price, qty; int bill = price * qty; cout << "Enter price and quantity: "; cin >> price >> qty; cout << "Bill = Rs. " << bill << endl; return 0; } |
Error message / wrong output, and your fix |
Part D: Graded Lab Tasks (60 min)
Rules for every task Print a clear prompt before every cin. Choose the right data type for each value. Use meaningful variable names (totalBill, not x). Test your program with the sample input given and check you get the sample output. |
Task 1: Student Profile (Easy — 3 marks)
Write a program that inputs a student's roll number, first name, section letter, CGPA and whether the student lives in the hostel (1 for yes, 0 for no). Store each value in a variable of the correct type (int, string, char, double, bool) and print a neat profile using \t.
Sample run Enter roll number: 1024 Enter first name: Ali Enter section (A/B/C): B Enter CGPA: 3.45 Hostelite? (1 = yes, 0 = no): 1
======= STUDENT PROFILE ======= Roll No : 1024 Name : Ali Section : B CGPA : 3.45 Hostelite : 1 =============================== |
Save as lab2_task1.cpp. Show the running program to your instructor.
Task 2: General Store Bill (Medium — 4 marks)
In Practical 1 you printed a fixed receipt. Now the program will calculate it. Input the quantity and unit price of Milk, Bread and Eggs. Calculate the total for each item and the grand total, and print a bill with aligned columns.
Sample run Milk - quantity and unit price: 2 230 Bread - quantity and unit price: 1 150 Eggs - quantity and unit price: 1 420
Item Qty Price Total Milk 2 230 460 Bread 1 150 150 Eggs 1 420 420 Grand Total = Rs. 1030 |
Save as lab2_task2.cpp. Show the running program to your instructor.
Task 3: Distance Converter (Medium — 4 marks)
The Faisalabad–Lahore motorway distance is about 130 km. Write a program that inputs a distance in kilometres and prints it in metres, centimetres and miles. Use a constant for miles per km: const double MILES_PER_KM = 0.621371;
Sample run Enter distance in km: 2.5 Metres : 2500 Centimetres : 250000 Miles : 1.55343 |
Save as lab2_task3.cpp. Show the running program to your instructor.
Task 4: Digit Splitter (Medium — 4 marks)
Input a 3-digit number (for example 472). Using only / and %, print its hundreds digit, tens digit and units digit, and the sum of the digits.
Sample run Enter a 3-digit number: 472 Hundreds digit: 4 Tens digit : 7 Units digit : 2 Sum of digits : 13 |
Save as lab2_task4.cpp. Show the running program to your instructor.
Task 5: Cricket Run-Rate Calculator (Challenge — 5 marks)
In a T20 match an innings has 20 overs = 120 balls. Input the target, the runs scored so far, and the balls bowled so far. Print: overs bowled in cricket style (75 balls = 12.3 overs), current run rate (runs per over), runs needed, balls left, and the required run rate.
Sample run Enter target: 181 Enter runs scored so far: 96 Enter balls bowled so far: 75 Overs bowled : 12.3 Current run rate : 7.68 Runs needed : 85 Balls left : 45 Required run rate : 11.3333 |
Save as lab2_task5.cpp. Show the running program to your instructor.
Part E: Post-Lab Questions
Q1. Why should you print a prompt before every cin?
Answer |
Q2. What is the difference between 7 / 2 and 7.0 / 2 in C++?
Answer |
Q3. Bug 4 and Bug 5 had no compiler error, but the answers were wrong. What do we call such errors and how can we find them?
Answer |
Q4. Which data type would you use for: (a) number of students, (b) bus fare in rupees and paisa, (c) blood group letter, (d) is the fee paid?
Answer |
Q5. In Task 4, how does (n / 10) % 10 give the tens digit of 472?
Answer |
Marking Rubric (20 marks)
Component | Marks | What earns full marks |
|---|---|---|
Task 1 | 3 | Correct data type for all five values; neat labelled output. |
Task 2 | 4 | Correct item totals and grand total; aligned columns; prompts present. |
Task 3 | 4 | Correct conversions; constant used for miles per km. |
Task 4 | 4 | All three digits and the sum correct using only / and %. |
Task 5 | 5 | Overs in x.y style; both run rates correct (no integer division); const for 120 balls. |
Note: Parts A–C and the post-lab questions are checked in the viva. A program that does not compile gets at most half marks for that task.
Submission
- Show your running programs to the instructor before leaving the lab.
- Put all five .cpp files (lab2_task1.cpp – lab2_task5.cpp) in a folder named with your roll number, zip it, and submit it as instructed.
- Copy the final code and output of each task into your Lab Book (Practical 2 pages) and get it signed.
- Copying code from a classmate gets zero marks for both students. Discussing ideas is fine; copying code is not.
Next practical: Operators & Expressions — precedence, increment/decrement and type casting (linked to Lectures 3 and 4). |