Browse papers
A

Section A: Long Answer Questions

Attempt all questions.

5 questions
1long10 marks

A civil engineering survey records the chainage and reduced level (RL) at nn stations along a road alignment. Write a C program that:

  1. Reads the number of stations nn and then reads nn reduced levels (in metres) into an array.
  2. Computes and prints the highest RL, the lowest RL, and the average RL.
  3. Counts how many stations have an RL above the average.

Also, by hand, trace your program for the data set {1245.30, 1248.75, 1243.10, 1250.40, 1247.45}\{1245.30,\ 1248.75,\ 1243.10,\ 1250.40,\ 1247.45\} and show the final printed output.

Program

#include <stdio.h>

int main(void) {
    int n, i, countAbove = 0;
    printf("Enter number of stations: ");
    scanf("%d", &n);

    float rl[100], sum = 0.0f, avg;
    float high, low;

    for (i = 0; i < n; i++) {
        printf("Enter RL of station %d (m): ", i + 1);
        scanf("%f", &rl[i]);
        sum += rl[i];
    }

    high = low = rl[0];
    for (i = 1; i < n; i++) {
        if (rl[i] > high) high = rl[i];
        if (rl[i] < low)  low  = rl[i];
    }

    avg = sum / n;

    for (i = 0; i < n; i++)
        if (rl[i] > avg) countAbove++;

    printf("Highest RL = %.2f m\n", high);
    printf("Lowest  RL = %.2f m\n", low);
    printf("Average RL = %.2f m\n", avg);
    printf("Stations above average = %d\n", countAbove);
    return 0;
}

Hand trace for the data set {1245.30,1248.75,1243.10,1250.40,1247.45}\{1245.30, 1248.75, 1243.10, 1250.40, 1247.45\}, n=5n = 5.

Sum:

S=1245.30+1248.75+1243.10+1250.40+1247.45=6235.00 mS = 1245.30 + 1248.75 + 1243.10 + 1250.40 + 1247.45 = 6235.00\ \text{m}

Average:

avg=6235.005=1247.00 m\text{avg} = \frac{6235.00}{5} = 1247.00\ \text{m}

Highest = 1250.401250.40 m, Lowest = 1243.101243.10 m.

Stations above average (>1247.00> 1247.00): 1248.751248.75 and 1250.401250.40 and 1247.451247.45 — that is 33 stations. (Note 1245.301245.30 and 1243.101243.10 are below.)

Final printed output

Highest RL = 1250.40 m
Lowest  RL = 1243.10 m
Average RL = 1247.00 m
Stations above average = 3
control-structuresloopscivil-applications
2long10 marks

(a) Explain the difference between call by value and call by reference in C, with a short example of each. (4 marks)

(b) Write a user-defined C function double power(double base, int exp) that computes baseexp\text{base}^{\text{exp}} for any integer exponent (positive, zero, or negative) without using the library pow() function. Use it in a main() to print 1.541.5^{4} and 232^{-3}. Show the computed values by hand. (6 marks)

(a) Call by value vs call by reference

AspectCall by valueCall by reference
What is passedA copy of the argument's valueThe address of the argument
Effect on callerChanges inside function do not affect originalChanges affect the original variable
MechanismPlain variablesPointers (& and *)

Call by value:

void inc(int x){ x = x + 1; }   // caller's variable unchanged

Call by reference:

void inc(int *x){ *x = *x + 1; } // caller's variable incremented

(b) Power function

#include <stdio.h>

double power(double base, int exp) {
    int negative = (exp < 0);
    int e = negative ? -exp : exp;
    double result = 1.0;
    for (int i = 0; i < e; i++)
        result *= base;
    if (negative)
        result = 1.0 / result;
    return result;
}

int main(void) {
    printf("1.5^4 = %.4f\n", power(1.5, 4));
    printf("2^-3  = %.4f\n", power(2.0, -3));
    return 0;
}

Hand computation

1.54=1.5×1.5×1.5×1.5=2.25×2.25=5.06251.5^4 = 1.5 \times 1.5 \times 1.5 \times 1.5 = 2.25 \times 2.25 = 5.0625.

23=123=18=0.1252^{-3} = \dfrac{1}{2^3} = \dfrac{1}{8} = 0.125.

Output

1.5^4 = 5.0625
2^-3  = 0.1250
functionsrecursionnumerical-methods
3long8 marks

A consulting firm stores data for reinforcement bars. Each bar has: a tag (string up to 10 chars), diameter (in mm), and length (in m). Define a struct named Bar. Write a program that reads data for 3 bars into an array of structures, then computes and prints the total steel mass assuming steel unit mass =7850 kg/m3= 7850\ \text{kg/m}^3.

Use the relation for a circular bar of diameter dd (mm) and length LL (m):

mass=7850×π(d/1000)24×L  (kg).\text{mass} = 7850 \times \frac{\pi (d/1000)^2}{4} \times L\ \ (\text{kg}).

Demonstrate the calculation for one bar with d=20 mmd = 20\ \text{mm}, L=12 mL = 12\ \text{m}.

Program

#include <stdio.h>
#define PI 3.141592653589793
#define RHO 7850.0   /* kg/m^3 */

struct Bar {
    char tag[11];
    double diameter;  /* mm */
    double length;    /* m  */
};

int main(void) {
    struct Bar b[3];
    double totalMass = 0.0;

    for (int i = 0; i < 3; i++) {
        printf("Bar %d tag, diameter(mm), length(m): ", i + 1);
        scanf("%10s %lf %lf", b[i].tag, &b[i].diameter, &b[i].length);
    }

    for (int i = 0; i < 3; i++) {
        double d = b[i].diameter / 1000.0;   /* m */
        double mass = RHO * (PI * d * d / 4.0) * b[i].length;
        printf("%s : %.2f kg\n", b[i].tag, mass);
        totalMass += mass;
    }

    printf("Total steel mass = %.2f kg\n", totalMass);
    return 0;
}

Sample calculation for d=20d = 20 mm =0.020= 0.020 m, L=12L = 12 m:

Cross-sectional area:

A=π(0.020)24=π×0.00044=0.001256644=3.14159×104 m2.A = \frac{\pi (0.020)^2}{4} = \frac{\pi \times 0.0004}{4} = \frac{0.00125664}{4} = 3.14159\times 10^{-4}\ \text{m}^2.

Mass:

mass=7850×3.14159×104×12=7850×3.76991×103=29.59 kg.\text{mass} = 7850 \times 3.14159\times 10^{-4} \times 12 = 7850 \times 3.76991\times 10^{-3} = 29.59\ \text{kg}.

For a single 20 mm bar of 12 m, mass ≈ 29.59 kg. (Equivalently the standard 0.00617d2L0.00617 d^2 L rule gives 0.00617×400×12=29.620.00617 \times 400 \times 12 = 29.62 kg, matching within rounding.)

structuresarrays-of-structurescivil-applications
4long8 marks

(a) What is a pointer? Explain the meaning of the & (address-of) and * (dereference) operators with a small example. (3 marks)

(b) Write a C function void reverse(int *arr, int n) that reverses an integer array in place using pointer notation (no array indexing with [] inside the swap). Trace its effect on the array {4,9,15,22,30}\{4, 9, 15, 22, 30\} showing the array after each swap. (5 marks)

(a) Pointer

A pointer is a variable that stores the memory address of another variable. If int x = 10; then &x gives the address of x, and a pointer int *p = &x; holds that address. The dereference operator *p accesses the value stored at that address (here 10).

int x = 10;
int *p = &x;   // p holds address of x
printf("%d", *p);  // prints 10
*p = 25;       // changes x to 25
  • & gives the address of a variable.
  • * (when applied to a pointer) gives the value at that address.

(b) Reverse function

void reverse(int *arr, int n) {
    int *left = arr;
    int *right = arr + (n - 1);
    while (left < right) {
        int temp = *left;
        *left = *right;
        *right = temp;
        left++;
        right--;
    }
}

Trace on {4,9,15,22,30}\{4, 9, 15, 22, 30\}, n=5n = 5:

Stepleft indexright indexArray after swap
Initial04{4, 9, 15, 22, 30}
Swap 10 ↔ 4{30, 9, 15, 22, 4}
Swap 21 ↔ 3{30, 22, 15, 9, 4}
Stopleft=2, right=2left < right false{30, 22, 15, 9, 4}

The middle element (index 2 = 15) stays in place. Final reversed array: {30, 22, 15, 9, 4}.

pointersarraysmemory
5long9 marks

A field team records daily rainfall (mm) for one month. Write a C program that:

  1. Opens a text file rainfall.txt for reading. The first line contains the number of days dd; the following lines each contain one day's rainfall value.
  2. Reads all values, computes the total and average monthly rainfall, and finds the wettest day (1-based day number with the maximum rainfall).
  3. Appends a one-line summary to a file report.txt.

Include proper file-open error checking. Then, for the sample data d=5d = 5 with values {0.0,12.5,8.0,25.0,4.5}\{0.0, 12.5, 8.0, 25.0, 4.5\} mm, compute the printed summary by hand.

Program

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    FILE *fin = fopen("rainfall.txt", "r");
    if (fin == NULL) {
        printf("Error: cannot open rainfall.txt\n");
        return 1;
    }

    int d;
    fscanf(fin, "%d", &d);

    double value, total = 0.0, maxVal = -1.0;
    int maxDay = 0;
    for (int i = 1; i <= d; i++) {
        fscanf(fin, "%lf", &value);
        total += value;
        if (value > maxVal) { maxVal = value; maxDay = i; }
    }
    fclose(fin);

    double avg = total / d;

    FILE *fout = fopen("report.txt", "a");
    if (fout == NULL) {
        printf("Error: cannot open report.txt\n");
        return 1;
    }
    fprintf(fout, "Total=%.1f mm, Avg=%.2f mm, Wettest day=%d (%.1f mm)\n",
            total, avg, maxDay, maxVal);
    fclose(fout);

    printf("Total=%.1f mm, Avg=%.2f mm, Wettest day=%d (%.1f mm)\n",
           total, avg, maxDay, maxVal);
    return 0;
}

Hand computation for {0.0,12.5,8.0,25.0,4.5}\{0.0, 12.5, 8.0, 25.0, 4.5\}, d=5d = 5:

Total:

T=0.0+12.5+8.0+25.0+4.5=50.0 mm.T = 0.0 + 12.5 + 8.0 + 25.0 + 4.5 = 50.0\ \text{mm}.

Average:

avg=50.05=10.00 mm.\text{avg} = \frac{50.0}{5} = 10.00\ \text{mm}.

Maximum is 25.025.0 mm on day 4.

Printed / appended summary:

Total=50.0 mm, Avg=10.00 mm, Wettest day=4 (25.0 mm)
file-handlingloopscivil-applications
B

Section B: Short Answer Questions

Attempt all questions.

6 questions
6short6 marks

(a) List the four basic data types in C and give the typical size (in bytes) and one example value of each. (3 marks)

(b) Evaluate the following C expressions, assuming int a = 7, b = 2; and show each result with its type: (3 marks)

a / b
a % b
(float)a / b
a > b && b > 0

(a) Basic data types (typical sizes on a 32/64-bit system)

TypeTypical sizeExample value
char1 byte'A'
int4 bytes42
float4 bytes3.14f
double8 bytes2.71828

(b) Expression evaluation with a = 7, b = 2:

ExpressionResultExplanation
a / b3 (int)Integer division truncates 7/2=3.537/2 = 3.5 \to 3
a % b1 (int)Remainder of 7÷27 \div 2 is 11
(float)a / b3.5 (float)a cast to float first, so floating division
a > b && b > 01 (int, true)7>27>2 is true and 2>02>0 is true, so AND is true

In C, relational/logical expressions yield int values 1 (true) or 0 (false).

c-fundamentalsdata-typesoperators
7short5 marks

Write a C program using a switch statement that reads a soil classification code (1, 2, 3, or 4) and prints the corresponding safe bearing capacity range:

  • 1 → Soft clay (50–100 kPa)
  • 2 → Stiff clay (100–300 kPa)
  • 3 → Loose sand (100–200 kPa)
  • 4 → Dense gravel (300–600 kPa)

For any other input, print "Invalid code".

Program

#include <stdio.h>

int main(void) {
    int code;
    printf("Enter soil classification code (1-4): ");
    scanf("%d", &code);

    switch (code) {
        case 1:
            printf("Soft clay: 50-100 kPa\n");
            break;
        case 2:
            printf("Stiff clay: 100-300 kPa\n");
            break;
        case 3:
            printf("Loose sand: 100-200 kPa\n");
            break;
        case 4:
            printf("Dense gravel: 300-600 kPa\n");
            break;
        default:
            printf("Invalid code\n");
    }
    return 0;
}

Example: input 4 prints Dense gravel: 300-600 kPa; input 9 prints Invalid code. The break after each case prevents fall-through to the next case.

control-structuresswitchcivil-applications
8short6 marks

Write a C program that reads two 2×22\times 2 matrices and prints their sum. Then, by hand, add the matrices

A=[3572],B=[1468].A = \begin{bmatrix} 3 & 5 \\ 7 & 2 \end{bmatrix}, \qquad B = \begin{bmatrix} 1 & 4 \\ 6 & 8 \end{bmatrix}.

Program

#include <stdio.h>

int main(void) {
    int A[2][2], B[2][2], C[2][2];
    int i, j;

    printf("Enter matrix A (4 values):\n");
    for (i = 0; i < 2; i++)
        for (j = 0; j < 2; j++)
            scanf("%d", &A[i][j]);

    printf("Enter matrix B (4 values):\n");
    for (i = 0; i < 2; i++)
        for (j = 0; j < 2; j++)
            scanf("%d", &B[i][j]);

    for (i = 0; i < 2; i++)
        for (j = 0; j < 2; j++)
            C[i][j] = A[i][j] + B[i][j];

    printf("Sum:\n");
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++)
            printf("%4d", C[i][j]);
        printf("\n");
    }
    return 0;
}

Hand calculation — element-wise addition:

A+B=[3+15+47+62+8]=[491310].A + B = \begin{bmatrix} 3+1 & 5+4 \\ 7+6 & 2+8 \end{bmatrix} = \begin{bmatrix} 4 & 9 \\ 13 & 10 \end{bmatrix}.

Output

   4   9
  13  10
arrays2d-arraysmatrices
9short5 marks

Without using the strlen() library function, write a C function int myStrlen(char str[]) that returns the number of characters in a string. Explain how a C string is stored in memory and demonstrate the count for the string "Bridge".

Function

int myStrlen(char str[]) {
    int count = 0;
    while (str[count] != '\0')
        count++;
    return count;
}

How a C string is stored

A C string is an array of char terminated by the null character '\0' (ASCII value 0). The functions know where the string ends by scanning until they reach '\0'. The terminator occupies one extra byte that is not counted as a visible character.

For "Bridge" the memory layout is:

Index:  0   1   2   3   4   5    6
Char:  'B' 'r' 'i' 'd' 'g' 'e' '\0'

The loop increments count for indices 0 through 5 (six characters) and stops at index 6 where '\0' is found.

Result: myStrlen("Bridge") = 6 (the '\0' is not counted, though 7 bytes are stored).

stringslibrary-functionsloops
10short4 marks

(a) Differentiate between a #define macro constant and a const variable in C. (2 marks)

(b) Identify and correct the errors in the following program: (2 marks)

#include <stdio.h>
int main()
{
    int radius = 5
    float area;
    area = 3.14 * radius * radius;
    printf("Area = %d", area)
}

(a) #define vs const

#define PI 3.14const float PI = 3.14;
Handled by the preprocessor before compilation; pure text substitutionA real typed variable stored in memory
Has no data type, no memory addressHas a type and (usually) an address
Cannot be inspected by a debugger as a variableType-checked and visible to the debugger

(b) Errors and corrections

The program has three problems:

  1. Missing semicolon after int radius = 5 → should be int radius = 5;
  2. printf uses %d for a float value → should be %f (or cast).
  3. Missing semicolon after the printf(...) statement.

Corrected program:

#include <stdio.h>
int main()
{
    int radius = 5;
    float area;
    area = 3.14 * radius * radius;   /* = 78.50 */
    printf("Area = %f", area);
    return 0;
}

With radius = 5, area =3.14×25=78.50= 3.14 \times 25 = 78.50.

c-fundamentalsprogram-structurepreprocessor
11short4 marks

Write a recursive C function to compute the factorial of a non-negative integer nn. State the base case and the recursive case, and show the call stack (sequence of calls and returns) for n=4n = 4.

Recursive function

long factorial(int n) {
    if (n == 0 || n == 1)   /* base case */
        return 1;
    return n * factorial(n - 1);   /* recursive case */
}
  • Base case: factorial(0) = 1 and factorial(1) = 1 — stops the recursion.
  • Recursive case: n!=n×(n1)!n! = n \times (n-1)!.

Call stack for n=4n = 4

Calls going down (unwinding):

factorial(4) = 4 * factorial(3)
factorial(3) = 3 * factorial(2)
factorial(2) = 2 * factorial(1)
factorial(1) = 1            <- base case reached

Returns coming back up:

factorial(1) -> 1
factorial(2) -> 2 * 1   = 2
factorial(3) -> 3 * 2   = 6
factorial(4) -> 4 * 6   = 24

Result: 4!=244! = 24.

functionsrecursionalgorithms

Frequently asked questions

Where can I find the BE Civil Engineering (IOE, TU) Computer Programming in C (IOE, CT 401) question paper 2080?
The full BE Civil Engineering (IOE, TU) Computer Programming in C (IOE, CT 401) 2080 (regular) question paper is available free on Kekkei. You can read every question online and attempt the paper under timed exam conditions.
Does the Computer Programming in C (IOE, CT 401) 2080 paper come with solutions?
Yes. Every question on this Computer Programming in C (IOE, CT 401) past paper includes a step-by-step solution, plus instant AI feedback when you attempt it on Kekkei.
How many marks is the BE Civil Engineering (IOE, TU) Computer Programming in C (IOE, CT 401) 2080 paper?
The BE Civil Engineering (IOE, TU) Computer Programming in C (IOE, CT 401) 2080 paper carries 80 full marks and is meant to be completed in 180 minutes, across 11 questions.
Is practising this Computer Programming in C (IOE, CT 401) past paper free?
Yes — reading and attempting this Computer Programming in C (IOE, CT 401) past paper on Kekkei is completely free.