Unit 3 · Lesson 418 minAcademic review pending

Keeping related lists in step

Parallel arrays store related facts about the same items at the same subscript, and enumerated types give meaningful names to small sets of values.

Choose explanation

After this lesson

You should be able to

  • Use two or more parallel arrays where index i refers to the same item in each.
  • Declare an enumerated type and use it to replace magic numbers.
  • Explain the main weakness of parallel arrays.
01

One index, several facts

Suppose you have roll numbers and marks for the same students. Keep two arrays where position i in each refers to the same student: roll[3] and marks[3] are that student's number and score. Sorting or searching one then means moving both together.

That coupling is also the weakness. Nothing in the language keeps them aligned — swap two entries in marks and forget roll, and every student now has someone else's score, with no error anywhere. Unit IV's structures exist to fix exactly this.

Two arrays, one meaning per index
#define SIZE 3

int  roll[SIZE]  = {101, 102, 103};
int  marks[SIZE] = { 72,  45,  91};

/* Student roll[i] scored marks[i] */
for (int i = 0; i < SIZE; i++)
    printf("Roll %d scored %d\n", roll[i], marks[i]);
02

Enumerated types name a small set

When a variable can only hold a few fixed values, give them names. enum day {MON, TUE, WED, THU, FRI}; creates named constants numbered from 0 upward. day today = WED; is far clearer than today = 2, and the compiler will not let you misspell WED, whereas it happily accepts a mistyped 2.

You may set values explicitly: enum status {PASS = 1, FAIL = 0};. Otherwise numbering continues from the previous one, so an enum is a readable way to keep a set of related codes consistent.

Names in the code, numbers underneath
enum day {MON, TUE, WED, THU, FRI};   /* MON is 0 ... FRI is 4 */

enum day today = WED;

if (today == FRI)
    printf("Weekend tomorrow\n");

/* An enum is still an integer underneath */
printf("%d\n", today);   /* 2 */
03

Enums index arrays neatly

Because an enum counts from 0, its values are ready-made subscripts. Declare an array sized to the number of days and index it with a day name — the code then reads like the problem instead of like arithmetic.

Add a final NUM_DAYS member and the array size stays correct automatically when you add a day. That trick removes a whole class of size-mismatch bugs.

Try it yourself

Store the number of classes attended on each weekday using an enum for the days, then print the busiest day by name.

Need a hint?

An enum gives you numbers, not text. You will need a parallel array of names to print one.

Check the worked solution

NUM_DAYS sits last in the enum so it equals the count of real days, which sizes both arrays correctly and keeps doing so if you add Saturday. The names array is parallel to classes: index i means the same day in both, which is the entire discipline this lesson is about.

#include <stdio.h>

enum day {MON, TUE, WED, THU, FRI, NUM_DAYS};

int main(void)
{
    int   classes[NUM_DAYS] = {4, 6, 3, 5, 2};
    const char *names[NUM_DAYS] = {"Monday", "Tuesday", "Wednesday",
                                   "Thursday", "Friday"};
    int busiest = MON;

    for (int d = TUE; d < NUM_DAYS; d++)
        if (classes[d] > classes[busiest])
            busiest = d;

    printf("Busiest: %s with %d classes\n", names[busiest], classes[busiest]);
    return 0;
}

Quick check

What is the main risk of using parallel arrays?

Select an answer to check your thinking.

Why this lesson exists

Syllabus mapping

Parallel Arrays and Enumerated Types

Maps to course outcome CO5.