Duration | Language | Tools | Linked Lecture |
|---|---|---|---|
50 minutes | C++ | Dev-C++ or Code::Blocks (online compiler as backup) | Lecture 17 (and 18) |
Mapped outcomes: CLO4 (C++ syntax and semantics) at psychomotor level P3 – Guided Response · PLO2, PLO5
1. Lab Objectives
By the end of this 50-minute lab, you will be able to:
- Declare and initialise one-dimensional arrays with a constant size.
- Read values into an array and print them forwards and backwards with for loops.
- Use an array twice: first to total and average, then to compare each value with the average.
- Find too-many-values, off-by-one and integer-average bugs.
2. Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Array (list) | Many values of one type under one name | The 7 daily temperatures of a week |
Element | One value in the array | Wednesday's temperature |
Index | Position number, starting at 0 | Seat number minus 1 |
Traverse | Visit every element with a loop | Reading every name on the attendance sheet |
Out of bounds | An index below 0 or ≥ size | Asking for seat 31 in a 30-seat coaster |
3. Background
Reminder const int SIZE = 7; int temp[SIZE]; // declare int b[5] = {7, 9}; // 7 9 0 0 0
for (int i = 0; i < SIZE; i++) // read cin >> temp[i]; for (int i = SIZE - 1; i >= 0; i--) // print backwards cout << temp[i] << " "; |
4. Time Plan (50 minutes)
Part | Activity | Time |
|---|---|---|
— | Briefing | 2 min |
A | Warm-up trace | 3 min |
B | Predict the output | 4 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 (3 min)
lab12_warmup.cpp #include <iostream> using namespace std;
int main() { int a[5] = {3, 8, 1, 6, 4}; a[2] = a[0] + a[4]; a[4] = a[1] - a[3]; a[0] = a[a[4]]; for (int i = 0; i < 5; i++) cout << a[i] << " "; cout << endl; return 0; } |
Draw the five boxes after each assignment and write the output |
Part B: Predict the Output (4 min)
lab12_predict.cpp #include <iostream> using namespace std;
int main() { int b[6] = {2, 4, 6}; int sum = 0; for (int i = 0; i < 6; i++) sum += b[i]; cout << "Sum: " << sum << endl; cout << "Size: " << sizeof(b) / sizeof(b[0]) << endl; for (int i = 5; i >= 0; i -= 2) cout << b[i] << " "; cout << endl; return 0; } |
My prediction |
Part C: Bug Hunt (5 min)
bug1.cpp — will not compile
#include <iostream> using namespace std;
int main() { int marks[5] = {70, 80, 90, 60, 50, 40}; for (int i = 0; i < 5; i++) cout << marks[i] << " "; cout << endl; return 0; } |
Error or wrong output, cause and fix |
bug2.cpp — wrong total
#include <iostream> using namespace std;
int main() { int price[5] = {120, 250, 80, 45, 300}; int total = 0; for (int i = 0; i < 4; i++) total += price[i]; cout << "Total bill: Rs. " << total << endl; return 0; } |
Error or wrong output, cause and fix |
bug3.cpp — wrong average
#include <iostream> using namespace std;
int main() { int marks[4] = {71, 84, 66, 90}; int total = 0; for (int i = 0; i < 4; i++) total += marks[i]; double avg = total / 4; cout << "Average: " << avg << endl; return 0; } |
Error or wrong output, cause and fix |
Part D: Graded Lab Tasks (32 min · 10 marks)
Rules Use a const int for every array size. Every loop over an array must run from 0 to size − 1 (or n − 1). Save as lab12_task1.cpp, lab12_task2.cpp, lab12_task3.cpp. |
Task 1: Week of Temperatures (Easy — 3 marks)
Read the maximum temperature of each of 7 days into an array (prompt with the day number). Print a table of day number and temperature, then print the temperatures in reverse order.
Sample run Enter the maximum temperature of each day: Day 1: 34 Day 2: 36 Day 3: 35 Day 4: 31 Day 5: 29 Day 6: 33 Day 7: 38 Day Temp 1 34 2 36 3 35 4 31 5 29 6 33 7 38 Reverse order: 38 33 29 31 35 36 34 |
Task 2: Class Average and Above-Average List (Medium — 3 marks)
Read the number of students n (1–30, validated) and each student's marks (0–100, validated) into an array. Print the total, the average with 2 decimals, and the marks that are above the average.
Sample run Number of students (1-30): 0 Number of students (1-30): 4 Marks of student 1 (0-100): 71 Marks of student 2 (0-100): 120 Marks of student 2 (0-100): 84 Marks of student 3 (0-100): 66 Marks of student 4 (0-100): 90 Total: 311 Average: 77.75 Above average: 84 90 |
Task 3: Weekly Sales Report With a Chart (Challenge — 4 marks)
A shop on Satiana Road keeps its sales for Monday to Sunday. Store the day names in a string array and read each day's sales (0 or more, validated). Print a report with the day, sales, the percentage of the week's total (1 decimal) and a bar of stars, one * for every full Rs. 1000. End with the week total.
Sample run Sales on Mon (Rs.): 4500 Sales on Tue (Rs.): 6200 Sales on Wed (Rs.): -100 Sales on Wed (Rs.): 3800 Sales on Thu (Rs.): 5100 Sales on Fri (Rs.): 9300 Sales on Sat (Rs.): 12750 Sales on Sun (Rs.): 8000 Day Sales % Chart (* = Rs. 1000) Mon 4500 9.1 **** Tue 6200 12.5 ****** Wed 3800 7.7 *** Thu 5100 10.3 ***** Fri 9300 18.7 ********* Sat 12750 25.7 ************ Sun 8000 16.1 ******** Week total: Rs. 49650 |
Part E: Post-Lab Questions (finish as homework if needed)
Q1. Why must the percentage in Task 3 be printed in a second loop, after all sales have been read?
Answer |
Q2. int score[8]; — what are the first and last indexes, and how many bytes does it use on our lab PCs?
Answer |
Q3. A student writes for (int i = 1; i <= 7; i++) cin >> temp[i]; for int temp[7]. Name both problems.
Answer |
Marking Rubric (10 marks)
Component | Marks | What earns full marks |
|---|---|---|
Task 1 | 3 | Array with const size; input with prompts; table; reverse order. |
Task 2 | 3 | Validated n and marks; total and decimal average; above-average list. |
Task 3 | 4 | String day names; validated sales; percentage and star bar after input; aligned output. |
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.
- Copy the final code and outputs into your Lab Book (Practical 12 pages).
- Copying code from a classmate gets zero marks for both students.
Next practical: Practical 13 — Arrays II: search, max/min & counting (this week). |