MCS-201 · Unit 4

Arrays and Strings

Section 3 of 9

Block 1 · Unit 4Arrays and Strings9 min · Section 3 of 9

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 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)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 1 · multiple choice

In a linear search of 50 elements, how many comparisons are needed when the key is not present?

Show answer

Answer: C. 50

Every element must be compared before concluding the key is absent, so 50 comparisons. 25 is the average for a key that is present.

Question 2 · multiple choice

Why is max = a[0] a better starting value than max = 0?

Show answer

Answer: B. It is correct even when all values are negative

If every value is negative, 0 would wrongly be reported as the maximum. Starting from an actual element is always correct.

Question 3 · multiple choice

Does linear search require the array to be sorted?

Show answer

Answer: D. No

Linear search simply checks each element in turn, so the order does not matter.

Question 4 · multiple choice

What does this program print?

C · count_even.c
#include <stdio.h>

int main(void)
{
    int a[6] = {3, 8, 12, 7, 10, 5};
    int i, count = 0;
    for (i = 0; i < 6; i++)
        if (a[i] % 2 == 0)
            count++;
    printf("%d\n", count);
    return 0;
}
Show answer

Answer: C. 3

The even values are 8, 12 and 10, so the count is 3. 30 would be their sum, not the count.

Question 5 · multiple choice

What does this fragment print for int a[5] = {4, 9, 2, 9, 6}; when searching for 9 with a loop that breaks on the first match and then prints the index?

Show answer

Answer: A. 1

The first 9 is at index 1, and break stops the search there. Index 3 holds the second 9, which is never reached.

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.

↑ ↓ move↵ openTab switch type