Skip to content

Lab · C Programming (from zero to systems)

Lab: a class's marks in arrays

Labbeginner· about 2 hours· 4 steps

The brief

A polytechnic lecturer keeps internal marks for eight students in two arrays: roll numbers and marks. You write functions for the average and the topper, sort both arrays together by roll number with bubble sort, and look students up with binary search, which you first write and test in JavaScript.

Why it matters

College result systems, attendance registers and exam portals all keep records in arrays, sort them and search them. Bubble sort and binary search are asked in almost every campus placement test, and the parallel-arrays trap (sorting one array but not the other) is a real bug that mixes up students' marks.

Skills you practise

  • functions and prototypes
  • passing arrays with their length
  • parallel arrays
  • bubble sort
  • binary search
  • casts in averages

You need

  • gcc (Linux, macOS with Xcode command line tools, or Windows with MSYS2 or WSL)

What you will have at the end

search.js with a tested binarySearch(), and marks.c: a C program with functions for the average and topper, a bubble sort that keeps roll numbers and marks together, and binary-search lookups.

How it is marked (rubric, 100 points)
Correct resultsEvery check passes: average 66.75, topper 105, the sorted list and all three lookups.45
Good functionsPrototypes at the top, each function does one job, and every array is passed with its length.25
Algorithms understoodYou can explain bubble sort's passes and why binary search needs a sorted array and low <= high.20
Clean buildNo warnings with -Wall -Wextra.10

0/4 steps done

Step 1 of 4

Binary search, tested in JavaScript

binarySearch(rolls, key) returns the index of key in a sorted array, or -1 when it is missing.

Binary search finds a value in a sorted array by halving the range each time: look at the middle, and if the key is bigger, search only the right half; if smaller, only the left half. Keep two indexes, low and high, and stop when low passes high.

Write it here first, where it is tested; step 4 uses the same loop in C. For 8 students it needs at most 4 looks instead of 8, and for a university with 80,000 roll numbers about 17 instead of 80,000.

Run shows what your code prints. Check runs the step's tests. Ctrl+Enter checks too.