4.3Processing Arrays: Maximum, Average and Linear Search
Covers IGNOU MCS-201 §4.4, 4.5
Nearly all array work follows one pattern: a loop visits each element and does something with it. This section applies that pattern to three classic tasks: finding the largest value, computing averages from two arrays, and searching for a value.
The traversal pattern
Traversal means visiting every element once, usually with for (i = 0; i < SIZE; i++). Totals, counts, maximums, printing and searching are all traversals with a different action inside the loop.
Finding the maximum
Parallel arrays
Parallel arrays are separate arrays in which the same index refers to the same item: marks1[i] and marks2[i] both belong to student i.
Linear search
Linear search works on any array, sorted or not. For n elements it needs 1 comparison in the best case (the key is first) and n comparisons in the worst case (the key is last or absent), so its time grows in proportion to n, written O(n).
Key takeaways
Traversal with a for loop is the basis of all array processing.
Find a maximum by starting with the first element and updating on larger values.
Parallel arrays use the same index for related data.
Linear search compares elements one by one: best case 1, worst case n comparisons.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Write a loop that finds the smallest element of int a[SIZE] and its index, and trace it for {7, 3, 9, 1, 5}.
Show worked answer
min = a[0]; idx = 0; for (i = 1; i < SIZE; i++) if (a[i] < min) { min = a[i]; idx = i; }. Trace: start min = 7, idx = 0. i = 1: 3 < 7, so min = 3, idx = 1. i = 2: 9 < 3 is false. i = 3: 1 < 3, so min = 1, idx = 3. i = 4: 5 < 1 is false. Result: the smallest element is 1 at index 3.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/arrays-and-strings/processing-arrays-maximum-average-and-linear-search/ to read this page.