UNIVERSITY OF AGRICULTURE, FAISALABAD
Department of Computer Science
LECTURE 19 · STUDENT NOTES
Mid-Term Review & Problem-Solving Practice
CS-307 Programming Fundamentals · BSCS Semester 1
Week | Slides | Linked practical | Reference ID |
7 | Lecture19_Midterm_Review.pptx | — | N19 |
Learning Outcomes
SLO | By the end of this lecture you can… | Builds CLO | Bloom level |
|---|---|---|---|
SLO 19.1 | Recall and connect the main ideas of Lectures 1–18 (from data types to arrays) | CLO4: Describe the syntax and semantics of the C++ programming language. | C2 |
SLO 19.2 | Trace code, predict output and find bugs in mid-term style questions | CLO4: Describe the syntax and semantics of the C++ programming language. | C4 |
SLO 19.3 | Plan and write a complete program for an exam problem using top-down design | CLO1: Identify the necessary properties of good problem-solving techniques. | C3 |
How to use these notes Read these notes after the lecture and before the practical. Section numbers (for example §3.2) are used in the question bank, so you can always find where an answer comes from. Type every code example yourself — do not copy-paste. |
1. The Mid-Term at a Glance
The mid-term is in Week 8 and is worth 30% of the course (24 of 80 marks). It covers Lectures 1–19 and Practicals 1–13. The date, time and room are on the department date sheet. (Slide L19-S02)
Part | Questions | Marks |
|---|---|---|
A MCQs | 6 × 1 | 6 |
B Short questions | 3 × 3 | 9 |
C Long question | 1 × 9 | 9 |
Total | 24 |
2. The Big Picture
The course so far has four units; each builds on the one before. (Slide L19-S05)
Figure 19.1 – The four units of the first half of the course (Slide L19-S05)
A typical long question joins them: input (Unit 1) → validate with a loop (Unit 3) → decide (Unit 2) → store and scan an array (Unit 4) → output.
3. Revision Summary
3.1 Basics and operators (Lectures 1–7)
(Slide L19-S06)
Topic | Remember | Example |
|---|---|---|
Integer division | int / int drops the decimal | 7 / 2 → 3; 7 / 2.0 → 3.5 |
Remainder % | last digit; even/odd | 17 % 5 → 2; n % 10 |
Precedence | * / % before + −, left to right | 17 % 5 * 2 + 7 / 2 → 7 |
Postfix x++ | use the old value, then add 1 | x = 5; y = x++ + 2 → y 7, x 6 |
= vs == | = assigns, == compares | if (m = 100) is always true |
Casting | cast before dividing | (double) total / n |
Build stages | preprocessor → compiler → linker → loader | 'undefined reference' = linker error |
Problem solving | properties: finite, definite, input, output, effective | top-down design, trace tables |
3.2 Decisions (Lectures 8–10)
(Slide L19-S07)
Structure | Use it for | Trap |
|---|---|---|
if / if-else | one or two ways | stray ; after if ( ); missing braces |
else-if ladder | ranges (grades, slabs) | order: the first true condition wins |
switch | exact int/char values (menus) | missing break → fall-through; case needs a constant |
?: | choosing one of two values | brackets inside cout |
3.3 Loops (Lectures 11–16)
(Slide L19-S08)
Loop | Condition checked | Minimum runs | Best for |
|---|---|---|---|
while | before the body | 0 | unknown count, sentinel |
do-while | after the body | 1 | menus, input validation |
for | before the body | 0 | known count |
- break leaves the loop; continue skips to the next round (in a while loop, make sure the update still happens).
- Patterns: count (start 0), sum (start 0), product (start 1), max/min (start at the first value), flag (start false), search (break when found).
- Nested loops: the inner loop runs completely for every round of the outer loop (rows × columns).
- Traps: missing update (infinite loop), i <= n instead of i < n, a ; after for ( ) or while ( ).
3.4 Arrays (Lectures 17–18)
(Slide L19-S09)
- Declare with a constant size: const int MAX = 30; int a[MAX]; Missing values in { } become 0; too many values is a compile error.
- Indexes run from 0 to size − 1. Elements are side by side: address of a[i] = start + i × element size. C++ does not check the index.
- Traverse with for (int i = 0; i < n; i++). Search: pos = -1, break when found, decide 'not found' after the loop.
- Max/min: start at the first element and keep the position. Arrays cannot be copied with =.
4. Sample Mid-Term Paper
Attempt the paper in 40 minutes without looking at the answers in §5. The questions follow the real pattern. (Slides L19-S10 to S14)
Part A — MCQs (6 × 1 = 6 marks)
1. 17 % 5 * 2 + 7 / 2 is:
A) 7 B) 7.5 C) 5 D) 8
2. int x = 5; int y = x++ + 2; The values of y and x are:
A) 8, 6 B) 7, 6 C) 7, 5 D) 8, 5
3. Which loop always runs its body at least once?
A) while B) for C) do-while D) none
4. switch (2) with case 1 Tea, case 2 Coffee, case 3 Juice, default Water and no break statements prints:
A) Coffee B) Tea Coffee C) Coffee Juice Water D) Water
5. int a[6] = {1, 2}; a[4] is:
A) 0 B) 2 C) garbage D) a compile error
6. The message "undefined reference to area(double)" comes from the:
A) preprocessor B) compiler C) linker D) loader
Part B — Short Questions (3 × 3 = 9 marks)
Q1. Trace (3). Make a trace table for the program below and write its exact output.
q_trace.cpp #include <iostream> using namespace std;
int main() { int a[5] = {4, 9, 2, 7, 5}; int s = 0; for (int i = 0; i < 5; i++) { if (a[i] % 2 == 1) s += a[i]; else a[i] = 0; } cout << "s = " << s << endl; for (int i = 0; i < 5; i++) cout << a[i] << " "; cout << endl; return 0; } |
Q2. Predict the output (3). Write the exact output. (Hint: count the rounds of the while loop.)
q_predict.cpp #include <iostream> using namespace std;
int main() { int x = 10, steps = 0; while (x > 1) { if (x % 2 == 0) x = x / 2; else x = 3 * x + 1; steps++; } cout << "Steps: " << steps << endl;
for (int i = 1; i <= 3; i++) { for (int j = 1; j <= i; j++) cout << i * j << " "; cout << endl; } cout << 17 % 5 * 2 + 7 / 2 << endl; return 0; } |
Q3. Find and fix (3). The program below has three errors: two stop it from building and one gives a wrong answer. Give the line, the kind of error and the fix for each.
q_bug.cpp #include <iostream> using namespace std;
int main() { int marks[4] = {70, 85, 64, 91} int total = 0; for (int i = 0; i < 4; i++); total += marks[i]; cout << "Average: " << total / 4 << endl; return 0; } |
Part C — Long Question (9 marks)
A housing society charges water by flat slab rates: up to 100 units Rs. 5 per unit, 101–200 units Rs. 8 per unit, above 200 units Rs. 12 per unit. Write a C++ program that reads the number of houses n (1–20, validated) and the units used by each house (0 or more, validated), stores the units and bills in arrays, and prints a table of house number, units and bill, the total bill and the house with the highest bill. Plan your answer with top-down design first.
5. Answers and Marking
5.1 Part A
Q | Answer | Reason |
|---|---|---|
1 | A | 17 % 5 = 2; 2 * 2 = 4; 7 / 2 = 3 (integer division); 4 + 3 = 7. |
2 | B | Postfix uses the old value 5, so y = 7; then x becomes 6. |
3 | C | do-while checks its condition after the body (post-test). |
4 | C | Execution starts at case 2 and falls through to the end. |
5 | A | Missing values in the initialiser list become 0. |
6 | C | The function was declared but its body was never linked. |
5.2 Part B
Q1. Odd values are added to s; even values are replaced by 0.
i | a[i] | odd? | s | array after |
|---|---|---|---|---|
0 | 4 | no | 0 | 0 9 2 7 5 |
1 | 9 | yes | 9 | 0 9 2 7 5 |
2 | 2 | no | 9 | 0 9 0 7 5 |
3 | 7 | yes | 16 | 0 9 0 7 5 |
4 | 5 | yes | 21 | 0 9 0 7 5 |
Output s = 21 0 9 0 7 5 |
Q2. x goes 10 → 5 → 16 → 8 → 4 → 2 → 1: six steps. The nested loop prints i × j for j = 1 … i. The last line is 17 % 5 * 2 + 7 / 2 = 4 + 3 = 7.
Output Steps: 6 1 2 4 3 6 9 7 |
Q3.
# | Line | Kind | g++ message / effect | Fix |
|---|---|---|---|---|
1 | int marks[4] = {…} | compile | error: expected ',' or ';' before 'int' | add ; after } |
2 | for (…); | compile | error: 'i' was not declared in this scope (and a 'does not guard' warning) | remove the ; |
3 | total / 4 | logic | prints 77 instead of 77.5 | total / 4.0 |
q_bug_fixed.cpp #include <iostream> using namespace std;
int main() { int marks[4] = {70, 85, 64, 91}; int total = 0; for (int i = 0; i < 4; i++) total += marks[i]; cout << "Average: " << total / 4.0 << endl; return 0; } |
Output Average: 77.5 |
5.3 Part C — model answer
Figure 19.2 – Plan and marking scheme for the long question (Slide L19-S13)
water_bill.cpp #include <iostream> #include <iomanip> using namespace std;
int main() { const int MAX = 20; int units[MAX], bill[MAX], n;
// 1. Input: number of houses and their units do { cout << "Number of houses (1-20): "; cin >> n; } while (n < 1 || n > MAX); for (int i = 0; i < n; i++) { do { cout << "Units for house " << i + 1 << ": "; cin >> units[i]; } while (units[i] < 0); }
// 2. Process: bill of each house (flat slab rate) int total = 0, top = 0; for (int i = 0; i < n; i++) { if (units[i] <= 100) bill[i] = units[i] * 5; else if (units[i] <= 200) bill[i] = units[i] * 8; else bill[i] = units[i] * 12; total += bill[i]; if (bill[i] > bill[top]) top = i; }
// 3. Output: table, total and highest bill cout << setw(6) << "House" << setw(7) << "Units" << setw(7) << "Bill" << endl; for (int i = 0; i < n; i++) cout << setw(6) << i + 1 << setw(7) << units[i] << setw(7) << bill[i] << endl; cout << "Total: Rs. " << total << endl; cout << "Highest bill: house " << top + 1 << " (Rs. " << bill[top] << ")" << endl; return 0; } |
Test run Number of houses (1-20): 4 Units for house 1: 80 Units for house 2: -5 Units for house 2: 150 Units for house 3: 230 Units for house 4: 100 House Units Bill 1 80 400 2 150 1200 3 230 2760 4 100 500 Total: Rs. 4860 Highest bill: house 3 (Rs. 2760) |
Marking scheme | Marks |
|---|---|
Validated input of n and units (do-while) | 2 |
Correct slab ladder for each bill | 2 |
Array use, total and highest bill with position | 3 |
Neat output and a correct test run | 2 |
Boundary check 100 units is in the first slab (≤ 100): 100 × 5 = Rs. 500, not 800. |
6. Exam Tips
(Slide L19-S15)
- Read each question twice and underline the input and the output.
- Write your plan as 3–5 comment lines before the code — it earns marks and keeps the answer tidy.
- Choose the loop: known count → for; validation or menu → do-while; stop value → while.
- Start values: sum 0, product 1, maximum = first element, search position −1.
- For trace and predict questions draw a trace table — marks are given for the working.
- For bug questions give the line, the kind of error and the fix.
- Check one boundary value (e.g. marks 50, 100 units). Leave 3 minutes to check ; { } and i < n.
7. Practise With the Question Bank
Open Database/CS307_Question_Bank.xlsx, filter Week 1–7, and practise MCQs and short questions from each unit. Long questions L09, L12, L14, L16, L18 and P09–P13 are good exam practice.