Nexus Learn

Practical 13: One-Dimensional Arrays II: Search, Max/Min & Counting

Lab Practical CS-307 Programming Fundamentals 6 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 (3 min)
  6. Part B: Predict the Output (4 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 18 (and 17)

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

1. Lab Objectives

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

  • Search an array with linear search and report the position or 'not found'.
  • Find the maximum and minimum of an array together with their positions.
  • Count the elements that follow a rule, using several counters in one loop.
  • Find the max-starts-at-0, early 'not found' and = in place of == bugs.

2. Word Bank

Word

Simple meaning

Real-world example

Key

The value we are searching for

Your roll number on the exam seating plan

Linear search

Check elements one by one from the start

Reading down the attendance sheet

Position

Index of an element (seat = index + 1)

Batting order number of the top scorer

Counter

A variable that starts at 0 and goes up by 1

Counting the ducks in an innings

Duck

A score of 0 in cricket

A batsman out without scoring

3. Background

Reminder

int pos = -1; // search

for (int i = 0; i < N; i++)

if (a[i] == key) { pos = i; break; }

int maxPos = 0; // maximum with position

for (int i = 1; i < N; i++)

if (a[i] > a[maxPos]) maxPos = i;

int count = 0; // counting

for (int i = 0; i < N; i++)

if (a[i] >= 50) count++;

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)

lab13_warmup.cpp

#include <iostream>

using namespace std;

int main() {

int a[6] = {14, 9, 27, 3, 27, 11};

int pos = 0;

for (int i = 1; i < 6; i++) {

if (a[i] > a[pos]) pos = i;

}

cout << a[pos] << " at index " << pos << endl;

return 0;

}

Trace table (i, a[i], pos) and output

Part B: Predict the Output (4 min)

lab13_predict.cpp

#include <iostream>

using namespace std;

int main() {

int v[8] = {5, 2, 5, 8, 5, 1, 2, 5};

int c = 0;

for (int i = 0; i < 8; i++)

if (v[i] == 5) c++;

cout << "Fives: " << c << endl;

int i = 0;

while (i < 8 && v[i] != 8) i++;

cout << "First 8 at index " << i << endl;

int last = -1;

for (int k = 0; k < 8; k++)

if (v[k] == 2) last = k;

cout << "Last 2 at index " << last << endl;

return 0;

}

My prediction

Part C: Bug Hunt (5 min)

bug1.cpp — wrong maximum

#include <iostream>

using namespace std;

int main() {

int t[5] = {-6, -2, -9, -4, -3}; // night temperatures

int highest = 0;

for (int i = 0; i < 5; i++)

if (t[i] > highest) highest = t[i];

cout << "Highest: " << highest << endl;

return 0;

}

Wrong output, cause and fix

bug2.cpp — too many messages

#include <iostream>

using namespace std;

int main() {

int code[4] = {301, 302, 305, 310};

int key = 305;

for (int i = 0; i < 4; i++) {

if (code[i] == key) cout << "Found" << endl;

else cout << "Not found" << endl;

}

return 0;

}

Wrong output, cause and fix

bug3.cpp — every PIN 'exists'

#include <iostream>

using namespace std;

int main() {

int pin[3] = {4321, 1111, 2580};

int key = 9999;

bool found = false;

for (int i = 0; i < 3; i++)

if (pin[i] = key) found = true;

cout << (found ? "PIN exists" : "PIN not found") << endl;

return 0;

}

Wrong output, cause and fix

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

Rules

Use pos = -1 for searches and decide 'not found' only after the loop.

Maximum and minimum start from the first element.

Save as lab13_task1.cpp, lab13_task2.cpp, lab13_task3.cpp.

Task 1: Exam Seat Finder (Easy — 3 marks)

The registered roll numbers of a section are stored in order: 2101, 2104, 2109, 2113, 2118, 2122, 2125, 2131, 2136, 2140. Read roll numbers until 0 is entered. For each, print the seat number (index + 1) or 'not registered'.

Sample run

Enter roll number (0 to stop): 2118

2118 is at seat 5

Enter roll number (0 to stop): 2100

2100 is not registered

Enter roll number (0 to stop): 2140

2140 is at seat 10

Enter roll number (0 to stop): 0

Search finished.

Task 2: Cricket Scorecard Analysis (Medium — 3 marks)

Read the runs of 11 players (0 or more, validated) into an array. Print the team total, the top scorer and the lowest scorer (player number and runs), and how many fifties (50 or more) and ducks (0) there were.

Sample run

Runs by player 1: 45

Runs by player 2: 0

Runs by player 3: 102

Runs by player 4: 67

Runs by player 5: -3

Runs by player 5: 0

Runs by player 6: 12

Runs by player 7: 50

Runs by player 8: 88

Runs by player 9: 3

Runs by player 10: 0

Runs by player 11: 21

Team total: 388

Top scorer: player 3 (102)

Lowest: player 2 (0)

Fifties: 4, ducks: 3

Task 3: Result Analysis (Challenge — 4 marks)

Read n (1–40, validated) and each student's marks (0–100, validated). Using the grade ladder A ≥ 85, B ≥ 70, C ≥ 60, D ≥ 50, F below 50, count the students in each grade. Print the highest mark and every student who got it, then the average and how many students are above the average.

Sample run

Number of students (1-40): 6

Marks 1: 72

Marks 2: 91

Marks 3: 105

Marks 3: 58

Marks 4: 91

Marks 5: 44

Marks 6: 66

A: 2 B: 1 C: 1 D: 1 F: 1

Highest: 91 (student 2 4)

Average: 70.3333, above average: 3

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

Q1. In Task 1, why is the message 'not registered' printed after the search loop and not inside it?

Answer

Q2. In Task 2, what would the program print as the top scorer if all 11 players scored 0, and why?

Answer

Q3. Why does Task 3 need two loops to print every student with the highest mark?

Answer

Marking Rubric (10 marks)

Component

Marks

What earns full marks

Task 1

3

Sentinel loop; linear search with break; one message per roll number.

Task 2

3

Validated input; total; top and lowest with positions; fifties and ducks.

Task 3

4

Grade counts; every student with the highest mark; average and above-average count.

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 13 pages).
  • Copying code from a classmate gets zero marks for both students.

Next: Week 8 mid-term (covers Practicals 1–13). Then Practical 14 — Sorting Arrays (Week 9).

← All CS-307 material