Browse papers
A

Section A: Long Answer Questions

Attempt all questions.

5 questions
1long10 marks

A civil engineering survey records the daily concrete strength gain (in MPa) of a curing cylinder over a number of days. Write a C program that:

(a) Reads an integer nn (number of days) from the user, then reads nn daily strength values (floating point).

(b) Computes and prints the total, the average, the maximum, and the minimum strength using appropriate loops.

(c) Counts how many days the strength was greater than or equal to the design strength of 25 MPa.

Explain the difference between an entry-controlled and an exit-controlled loop, and state which loop construct you used in part (b) and why.

Program

#include <stdio.h>

int main(void) {
    int n, i, daysAbove = 0;
    float strength, total = 0.0f, avg;
    float maxS, minS;
    const float DESIGN = 25.0f;

    printf("Enter number of days: ");
    scanf("%d", &n);

    for (i = 1; i <= n; i++) {
        printf("Enter strength for day %d (MPa): ", i);
        scanf("%f", &strength);

        total += strength;

        if (i == 1) {            /* initialise max/min with first reading */
            maxS = minS = strength;
        } else {
            if (strength > maxS) maxS = strength;
            if (strength < minS) minS = strength;
        }

        if (strength >= DESIGN) daysAbove++;
    }

    avg = total / n;

    printf("\nTotal strength   = %.2f MPa\n", total);
    printf("Average strength = %.2f MPa\n", avg);
    printf("Maximum strength = %.2f MPa\n", maxS);
    printf("Minimum strength = %.2f MPa\n", minS);
    printf("Days >= 25 MPa   = %d\n", daysAbove);

    return 0;
}

Sample run (for n=5n=5 with readings 18.0, 22.5, 25.0, 27.5, 30.0 MPa):

  • Total =18.0+22.5+25.0+27.5+30.0=123.0= 18.0 + 22.5 + 25.0 + 27.5 + 30.0 = 123.0 MPa
  • Average =123.0/5=24.60= 123.0 / 5 = 24.60 MPa
  • Maximum =30.00= 30.00 MPa, Minimum =18.00= 18.00 MPa
  • Days 25\geq 25 MPa =3= 3

Entry-controlled vs exit-controlled loop:

AspectEntry-controlledExit-controlled
Test positionCondition checked before bodyCondition checked after body
Examplesfor, whiledo...while
Minimum executions0 (body may never run)1 (body runs at least once)

I used a for loop in part (b) because the number of iterations nn is known in advance; for keeps the initialisation, condition, and update neatly in one place, which reduces the chance of forgetting the counter update.

control-structuresloopscivil-application
2long10 marks

(a) Differentiate between call by value and call by reference in C with a clear example of each.

(b) Write a C program that uses a user-defined function power(base, exp) (using recursion) to compute baseexpbase^{exp} for integer exponents. Use it to compute the volume of a cube of side 6 m as 636^3.

(c) Show the recursion trace (stack of calls) for power(6, 3).

(a) Call by value vs call by reference

  • Call by value: a copy of the argument is passed; changes inside the function do not affect the caller's variable.
  • Call by reference: the address is passed (via pointers); changes inside the function do affect the caller's variable.
/* call by value: swap does NOT work */
void swapVal(int a, int b) { int t = a; a = b; b = t; }

/* call by reference: swap DOES work */
void swapRef(int *a, int *b) { int t = *a; *a = *b; *b = t; }

After swapVal(x, y) the caller's x, y are unchanged; after swapRef(&x, &y) they are swapped.

(b) Recursive power and cube volume

#include <stdio.h>

long power(int base, int exp) {
    if (exp == 0) return 1;            /* base case */
    return base * power(base, exp - 1); /* recursive case */
}

int main(void) {
    int side = 6;
    long volume = power(side, 3);
    printf("Volume of cube = %ld cubic metres\n", volume);
    return 0;
}

(c) Recursion trace for power(6, 3)

power(6,3) = 6 * power(6,2)
           = 6 * (6 * power(6,1))
           = 6 * (6 * (6 * power(6,0)))
           = 6 * (6 * (6 * 1))     <- base case returns 1
           = 6 * (6 * 6)
           = 6 * 36
           = 216

Unwinding: power(6,0)=1, power(6,1)=6, power(6,2)=36, power(6,3)=216.

Final answer: Volume of the cube =63=216 m3= 6^3 = \mathbf{216\ m^3}.

functionsrecursionmodular-programming
3long8 marks

A structural engineer stores nodal loads in two 2×32 \times 3 matrices AA and BB (in kN). Write a C program that reads two 2×32 \times 3 matrices and prints their sum C=A+BC = A + B. Then, by hand, compute CC for:

A=[12759411],B=[3862101]A = \begin{bmatrix} 12 & 7 & 5 \\ 9 & 4 & 11 \end{bmatrix}, \quad B = \begin{bmatrix} 3 & 8 & 6 \\ 2 & 10 & 1 \end{bmatrix}

Program

#include <stdio.h>
#define R 2
#define C 3

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

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

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

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

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

Hand computation (element-wise addition):

  • C11=12+3=15C_{11} = 12 + 3 = 15
  • C12=7+8=15C_{12} = 7 + 8 = 15
  • C13=5+6=11C_{13} = 5 + 6 = 11
  • C21=9+2=11C_{21} = 9 + 2 = 11
  • C22=4+10=14C_{22} = 4 + 10 = 14
  • C23=11+1=12C_{23} = 11 + 1 = 12
C=[151511111412] kN\mathbf{C = \begin{bmatrix} 15 & 15 & 11 \\ 11 & 14 & 12 \end{bmatrix}\ \text{kN}}
arraystwo-dimensional-arraymatrix
4long8 marks

Define a C structure named Beam to store: a beam ID (integer), length in metres (float), and uniformly distributed load (UDL) ww in kN/m (float). Write a program that reads data for 3 beams into an array of structures, then for each beam computes and prints the maximum bending moment for a simply supported beam under UDL, given by Mmax=wL28M_{max} = \dfrac{w L^2}{8}.

Program

#include <stdio.h>

struct Beam {
    int id;
    float length;   /* metres */
    float udl;      /* kN/m   */
};

int main(void) {
    struct Beam beams[3];
    int i;
    float Mmax;

    for (i = 0; i < 3; i++) {
        printf("Beam %d - enter id, length(m), UDL(kN/m): ", i + 1);
        scanf("%d %f %f", &beams[i].id, &beams[i].length, &beams[i].udl);
    }

    printf("\nID    L(m)    w(kN/m)   Mmax(kN.m)\n");
    for (i = 0; i < 3; i++) {
        Mmax = (beams[i].udl * beams[i].length * beams[i].length) / 8.0f;
        printf("%-4d  %-6.2f  %-7.2f   %.2f\n",
               beams[i].id, beams[i].length, beams[i].udl, Mmax);
    }
    return 0;
}

Worked example for a beam with L=4 mL = 4\ \text{m}, w=10 kN/mw = 10\ \text{kN/m}:

Mmax=wL28=10×428=10×168=1608=20 kNmM_{max} = \frac{w L^2}{8} = \frac{10 \times 4^2}{8} = \frac{10 \times 16}{8} = \frac{160}{8} = \mathbf{20\ kN\cdot m}

For a beam with L=6 mL = 6\ \text{m}, w=12 kN/mw = 12\ \text{kN/m}:

Mmax=12×368=4328=54 kNmM_{max} = \frac{12 \times 36}{8} = \frac{432}{8} = \mathbf{54\ kN\cdot m}

The structure groups related, differently-typed data under one logical unit, and the array of structures lets the same processing loop run for every beam.

structurescivil-applicationdata-modelling
5long8 marks

A site office logs rainfall data. Write a C program that:

(a) Opens a file rainfall.txt for writing, writes 5 rainfall readings (in mm) entered by the user, then closes it.

(b) Re-opens the same file for reading, reads back all values, and computes the total and average rainfall.

Explain the purpose of fopen, fprintf, fscanf, and fclose, and what happens if fopen fails.

Program

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

int main(void) {
    FILE *fp;
    int i;
    float value, total = 0.0f, avg;

    /* (a) write phase */
    fp = fopen("rainfall.txt", "w");
    if (fp == NULL) {
        printf("Error: cannot open file for writing.\n");
        return 1;
    }
    for (i = 1; i <= 5; i++) {
        printf("Enter rainfall reading %d (mm): ", i);
        scanf("%f", &value);
        fprintf(fp, "%f\n", value);
    }
    fclose(fp);

    /* (b) read phase */
    fp = fopen("rainfall.txt", "r");
    if (fp == NULL) {
        printf("Error: cannot open file for reading.\n");
        return 1;
    }
    for (i = 0; i < 5; i++) {
        fscanf(fp, "%f", &value);
        total += value;
    }
    fclose(fp);

    avg = total / 5.0f;
    printf("Total rainfall   = %.2f mm\n", total);
    printf("Average rainfall = %.2f mm\n", avg);
    return 0;
}

Worked example for readings 8.0, 12.0, 5.0, 20.0, 15.0 mm:

  • Total =8.0+12.0+5.0+20.0+15.0=60.0= 8.0 + 12.0 + 5.0 + 20.0 + 15.0 = 60.0 mm
  • Average =60.0/5=12.00 mm= 60.0 / 5 = \mathbf{12.00\ mm}

Function roles:

  • fopen(name, mode) — opens a file in the given mode ("w", "r", etc.) and returns a FILE * pointer (or NULL on failure).
  • fprintf(fp, ...) — writes formatted data to the file.
  • fscanf(fp, ...) — reads formatted data from the file.
  • fclose(fp) — flushes buffers and releases the file resource.

If fopen fails it returns NULL (e.g., disk full, permission denied, path missing). The program must check for NULL before using the pointer; otherwise dereferencing a NULL FILE * causes a runtime crash.

file-handlingdata-loggingcivil-application
B

Section B: Short Answer Questions

Attempt all questions.

6 questions
6short6 marks

(a) List the four basic data types in C and give a typical size (in bytes) and one civil-engineering use for each.

(b) Evaluate the following C expression step by step, stating the result and its type, assuming int a = 7, b = 2;:

a / b + 5.0 * a % b\texttt{a / b + 5.0 * a \% b}

(a) Basic data types

TypeTypical sizeCivil use
char1 byteStoring a soil-class label (e.g. 'A')
int4 bytesCounting number of bars/storeys
float4 bytesConcrete strength in MPa
double8 bytesHigh-precision survey coordinates

(b) Expression evaluation of a / b + 5.0 * a % b with a = 7, b = 2:

Precedence: *, /, % (left-to-right) before +.

  1. a / b =7/2=3= 7 / 2 = 3 (integer division, result int 33).
  2. 5.0 * a =5.0×7=35.0= 5.0 \times 7 = 35.0 (double).
  3. 35.0 % bthe % operator is not valid on a double. In C, modulus requires integer operands, so 5.0 * a % b would cause a compile-time error.

If the intended expression were a / b + 5 * a % b (all int):

  • a / b = 3
  • 5 * a = 35, then 35 % 2 = 1
  • 3 + 1 = 4

Result (integer version): 4\mathbf{4}, type int. (The original mixes double with %, which is illegal — a key teaching point.)

c-fundamentalsdata-typesoperators
7short6 marks

Write a C program using a switch statement that classifies a concrete grade based on a single character input: 'A' prints "M20", 'B' prints "M25", 'C' prints "M30", and any other input prints "Invalid grade". Explain why a break statement is needed after each case.

Program

#include <stdio.h>

int main(void) {
    char grade;
    printf("Enter grade code (A/B/C): ");
    scanf(" %c", &grade);

    switch (grade) {
        case 'A':
            printf("M20\n");
            break;
        case 'B':
            printf("M25\n");
            break;
        case 'C':
            printf("M30\n");
            break;
        default:
            printf("Invalid grade\n");
    }
    return 0;
}

Sample: input 'B' prints M25; input 'X' prints Invalid grade.

Why break is needed: Without break, control falls through to the next case after a match, executing subsequent cases until a break or the end of the switch is reached. For example, without breaks, input 'A' would print M20, then M25, then M30. The break exits the switch immediately after the matching case so only the intended output is produced.

control-structuresswitch-casedecision-making
8short6 marks

(a) What is a pointer? Explain the & (address-of) and * (dereference) operators.

(b) For the following code, state the printed output and explain why:

int x = 42;
int *p = &x;
*p = *p + 8;
printf("%d\n", x);

(a) Pointer

A pointer is a variable that stores the memory address of another variable rather than a direct value.

  • The & (address-of) operator returns the memory address of a variable, e.g. &x gives the location of x.
  • The * (dereference / indirection) operator accesses the value stored at the address a pointer holds, e.g. *p reads or writes the variable that p points to.

(b) Output trace

int x = 42;        // x holds 42
int *p = &x;       // p now points to x
*p = *p + 8;       // *p reads 42, adds 8 -> writes 50 into x
printf("%d\n", x); // prints x

Since p points to x, writing through *p changes x itself: 42+8=5042 + 8 = 50.

Printed output: 50\mathbf{50}.

pointersmemoryaddress-arithmetic
9short6 marks

Write a C program that searches a 1-D array of nn integers for a key value using linear search and prints its position (1-based) or "Not found". Then dry-run the search for key = 17 on the array {4,9,17,23,8}\{4, 9, 17, 23, 8\} and show the comparisons made.

Program

#include <stdio.h>

int main(void) {
    int arr[100], n, i, key, pos = -1;

    printf("Enter n: ");
    scanf("%d", &n);
    printf("Enter %d values: ", n);
    for (i = 0; i < n; i++)
        scanf("%d", &arr[i]);

    printf("Enter key to search: ");
    scanf("%d", &key);

    for (i = 0; i < n; i++) {
        if (arr[i] == key) {
            pos = i + 1;   /* 1-based position */
            break;
        }
    }

    if (pos != -1)
        printf("Found at position %d\n", pos);
    else
        printf("Not found\n");
    return 0;
}

Dry run for key = 17, array {4,9,17,23,8}\{4, 9, 17, 23, 8\}:

StepIndex iiarr[i]Compare with 17Result
1044=174 = 17?No
2199=179 = 17?No
321717=1717 = 17?Yes

Match found at index 22, so 1-based position =2+1=3= 2 + 1 = 3.

Output: Found at position 3\mathbf{3} (after 3 comparisons).

arrayssearchingalgorithms
10short6 marks

Write a user-defined C function float area_circle(float r) that returns the cross-sectional area of a circular column of radius rr. Call it from main to compute the area of a column of radius 0.300.30 m. Use π=3.1416\pi = 3.1416 and show the hand calculation.

Program

#include <stdio.h>
#define PI 3.1416f

float area_circle(float r) {
    return PI * r * r;
}

int main(void) {
    float r = 0.30f;
    float A = area_circle(r);
    printf("Cross-sectional area = %.4f square metres\n", A);
    return 0;
}

Hand calculation for r=0.30r = 0.30 m:

A=πr2=3.1416×(0.30)2=3.1416×0.09=0.282744 m2A = \pi r^2 = 3.1416 \times (0.30)^2 = 3.1416 \times 0.09 = 0.282744\ \text{m}^2

Rounded to four decimal places:

Cross-sectional area =0.2827 m2= \mathbf{0.2827\ m^2}.

functionscivil-applicationgeometry
11short6 marks

(a) Explain the steps of the C program execution cycle: editing, preprocessing, compiling, linking, and execution.

(b) Identify and correct the three errors in the program below:

#include <stdio.h>
int main() {
    int n = 5
    printf("Square = %d", n * n)
    return 0
}

(a) C program execution cycle

  1. Editing — the source code is typed and saved in a .c file using an editor/IDE.
  2. Preprocessing — the preprocessor handles directives such as #include and #define, producing an expanded source file.
  3. Compiling — the compiler translates the preprocessed source into object code (.o/.obj), checking syntax.
  4. Linking — the linker combines object code with library functions (e.g. printf from the standard library) to create an executable.
  5. Execution — the operating system loads and runs the executable; output is produced.

Flow: source.c → preprocessor → compiler → linker → executable → run.

(b) Error correction

The three errors are missing semicolons at the end of statements.

#include <stdio.h>
int main() {
    int n = 5;                       /* error 1: added ; */
    printf("Square = %d", n * n);    /* error 2: added ; */
    return 0;                        /* error 3: added ; */
}

With the corrections the program compiles and prints: Square = 25 (since 5×5=255 \times 5 = 25).

c-fundamentalsprogram-structurecompilation

Frequently asked questions

Where can I find the BE Civil Engineering (IOE, TU) Computer Programming in C (IOE, CT 401) question paper 2078?
The full BE Civil Engineering (IOE, TU) Computer Programming in C (IOE, CT 401) 2078 (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) 2078 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) 2078 paper?
The BE Civil Engineering (IOE, TU) Computer Programming in C (IOE, CT 401) 2078 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.