Browse papers
A

Section A: Long Answer Questions

Attempt all questions.

5 questions
1long10 marks

Define the four basic data types in C and state the typical storage size (in bytes) and the value range for each on a 32-bit compiler. Distinguish between automatic type conversion (implicit) and type casting (explicit) with a short example of each. Then evaluate the following C expression manually, showing every intermediate step and the final data type and value:

int a = 17, b = 5;
float r;
r = a / b + (float)a / b + a % b;

Four basic (primitive) data types in C (32-bit compiler):

TypeSize (bytes)Typical Range
char1128-128 to 127127 (signed)
int42,147,483,648-2{,}147{,}483{,}648 to 2,147,483,6472{,}147{,}483{,}647
float4±3.4×1038\approx \pm 3.4\times10^{38} (6-7 significant digits)
double8±1.7×10308\approx \pm 1.7\times10^{308} (15-16 significant digits)

Implicit conversion (type promotion): The compiler automatically converts a lower-rank type to a higher-rank type in a mixed expression. Example:

int n = 3; float f = n + 2.5;  // n promoted to float -> f = 5.5

Explicit conversion (type casting): The programmer forces a conversion using the cast operator (type). Example:

int total = 7, count = 2; float avg = (float)total / count; // avg = 3.5

Step-by-step evaluation of the expression with a = 17, b = 5:

  1. a / b -> integer division: 17/5=317 / 5 = 3 (fractional part truncated). Type: int, value 33.
  2. (float)a / b -> a cast to 17.0, then 17.0 / 5 promotes b to float: 17.0/5=3.417.0 / 5 = 3.4. Type: float, value 3.43.4.
  3. a % b -> modulus (remainder): 17mod5=217 \bmod 5 = 2. Type: int, value 22.
  4. Sum: 3+3.4+23 + 3.4 + 2. The int operands are promoted to float: 3.0+3.4+2.0=8.43.0 + 3.4 + 2.0 = 8.4.
  5. Assigned to float r, so no truncation occurs.

Final result: r = 8.4 (type float).

c-fundamentalsdata-typesoperators
2long10 marks

Explain the difference between call by value and call by reference in C. Write a C program using a user-defined function to compute the factored design load WW on a beam given dead load DD and live load LL using the load combination W=1.2D+1.6LW = 1.2D + 1.6L (in kN). The function must return the factored load. Demonstrate, with a dry run, the output of your program for D=25 kND = 25\ \text{kN} and L=40 kNL = 40\ \text{kN}.

Call by value vs call by reference:

  • Call by value: A copy of the actual argument is passed to the function. Changes made to the parameter inside the function do not affect the original variable. Used when the caller's data must be protected.
  • Call by reference: The address of the actual argument is passed (via pointers). The function can modify the original variable directly. Used to return multiple values or modify caller data.

C program (call by value, returning the factored load):

#include <stdio.h>

float factoredLoad(float D, float L) {
    return 1.2f * D + 1.6f * L;   // ACI/IS load combination
}

int main(void) {
    float dead = 25.0f, live = 40.0f;
    float W = factoredLoad(dead, live);
    printf("Factored design load W = %.2f kN\n", W);
    return 0;
}

Dry run for D=25D = 25, L=40L = 40:

  1. factoredLoad(25.0, 40.0) is called; D = 25.0, L = 40.0 (copies).
  2. 1.2×25=30.01.2 \times 25 = 30.0.
  3. 1.6×40=64.01.6 \times 40 = 64.0.
  4. Return 30.0+64.0=94.030.0 + 64.0 = 94.0.

Program output:

Factored design load W = 94.00 kN

Final factored design load: W=94.00 kNW = 94.00\ \text{kN}.

functionsrecursioncivil-applications
3long10 marks

A civil engineer records the compressive strength (in MPa) of 6 concrete cube samples in a 1-D array: {28.5, 31.0, 27.2, 33.4, 29.8, 30.1}. Write a complete C program that uses arrays and loops to: (a) compute and print the average strength, (b) find and print the maximum strength, and (c) count how many samples meet the design requirement of at least 30 MPa. Then compute each of these three results by hand to verify the program output.

C program:

#include <stdio.h>

int main(void) {
    float s[6] = {28.5f, 31.0f, 27.2f, 33.4f, 29.8f, 30.1f};
    float sum = 0.0f, max = s[0], avg;
    int i, count = 0;

    for (i = 0; i < 6; i++) {
        sum += s[i];
        if (s[i] > max) max = s[i];
        if (s[i] >= 30.0f) count++;
    }
    avg = sum / 6;

    printf("Average strength = %.2f MPa\n", avg);
    printf("Maximum strength = %.2f MPa\n", max);
    printf("Samples >= 30 MPa = %d\n", count);
    return 0;
}

Hand verification:

(a) Sum =28.5+31.0+27.2+33.4+29.8+30.1=180.0= 28.5 + 31.0 + 27.2 + 33.4 + 29.8 + 30.1 = 180.0 MPa. Average =180.0/6=30.00= 180.0 / 6 = 30.00 MPa.

(b) Comparing values, the largest is 33.433.4 MPa.

(c) Samples 30\geq 30: 31.031.0, 33.433.4, 30.130.1 -> 3 samples. (Note 29.8<3029.8 < 30 is excluded.)

Program output:

Average strength = 30.00 MPa
Maximum strength = 33.40 MPa
Samples >= 30 MPa = 3

Final answers: average =30.00= 30.00 MPa, maximum =33.40= 33.40 MPa, count =3= 3.

arraysloopscivil-applications
4long10 marks

Define a structure named Survey to store the data of a survey station: a station name (string), and its coordinates x, y, z (all float, in metres). Write a C program that (a) reads data for 3 stations into an array of structures, (b) writes the records to a text file stations.txt, and (c) reads the file back and prints each record. Explain the role of fopen, fprintf, fscanf, and fclose in your program.

Structure definition and program:

#include <stdio.h>

struct Survey {
    char name[20];
    float x, y, z;   // metres
};

int main(void) {
    struct Survey st[3];
    int i;
    FILE *fp;

    /* (a) read input */
    for (i = 0; i < 3; i++) {
        printf("Enter name x y z for station %d: ", i + 1);
        scanf("%s %f %f %f", st[i].name, &st[i].x, &st[i].y, &st[i].z);
    }

    /* (b) write to file */
    fp = fopen("stations.txt", "w");
    if (fp == NULL) { printf("File error\n"); return 1; }
    for (i = 0; i < 3; i++)
        fprintf(fp, "%s %.2f %.2f %.2f\n", st[i].name, st[i].x, st[i].y, st[i].z);
    fclose(fp);

    /* (c) read back and print */
    struct Survey r;
    fp = fopen("stations.txt", "r");
    if (fp == NULL) { printf("File error\n"); return 1; }
    printf("\nStation records:\n");
    while (fscanf(fp, "%s %f %f %f", r.name, &r.x, &r.y, &r.z) == 4)
        printf("%s (%.2f, %.2f, %.2f)\n", r.name, r.x, r.y, r.z);
    fclose(fp);
    return 0;
}

Role of file functions:

  • fopen(name, mode) opens the file and returns a FILE* pointer; mode "w" creates/overwrites for writing, "r" opens for reading. Returns NULL on failure.
  • fprintf(fp, ...) writes formatted data to the file (like printf but to a stream).
  • fscanf(fp, ...) reads formatted data from the file (like scanf but from a stream); returns the count of items successfully read.
  • fclose(fp) flushes buffers and closes the file, releasing the resource.

Sample run (input: A 100 200 1500, B 150 250 1502, C 200 300 1498) produces output:

Station records:
A (100.00, 200.00, 1500.00)
B (150.00, 250.00, 1502.00)
C (200.00, 300.00, 1498.00)
structuresfile-handlingcivil-applications
5long10 marks

Explain the relationship between pointers and arrays in C. Given the declaration int arr[5] = {12, 24, 36, 48, 60}; and int *p = arr;, evaluate each of the following and state the value (assume arr starts at address 2000 and sizeof(int) = 4):

  1. *p
  2. *(p + 2)
  3. *(arr + 3)
  4. p + 1 (as an address)
  5. *(p + 4) - *p

Also write a C function int sumArray(int *a, int n) that returns the sum of n integers using pointer arithmetic, and dry-run it for the given array.

Pointers and arrays relationship: The name of an array decays to a pointer to its first element. Thus arr is equivalent to &arr[0], and arr[i] is identical to *(arr + i). Pointer arithmetic scales by the element size: p + i advances by i * sizeof(int) bytes.

Evaluations (arr at 2000, sizeof(int) = 4):

ExpressionReasoningValue
*pvalue at arr[0]1212
*(p + 2)value at arr[2]3636
*(arr + 3)value at arr[3]4848
p + 12000+1×42000 + 1 \times 4address 20042004
*(p + 4) - *p601260 - 124848

Function using pointer arithmetic:

int sumArray(int *a, int n) {
    int sum = 0, i;
    for (i = 0; i < n; i++)
        sum += *(a + i);   // same as a[i]
    return sum;
}

Dry run for arr = {12, 24, 36, 48, 60}, n = 5:

i*(a+i)running sum
01212
12436
23672
348120
460180

Final returned sum: 12+24+36+48+60=18012 + 24 + 36 + 48 + 60 = 180.

pointersarraysmemory
B

Section B: Short Answer Questions

Attempt all questions.

6 questions
6short5 marks

Differentiate between while and do-while loops. Write a C program using a loop to print the sum of the first nn natural numbers and verify the output for n=10n = 10 using the formula n(n+1)2\dfrac{n(n+1)}{2}.

while vs do-while:

  • while is entry-controlled: the condition is tested first, so the body may execute zero times.
  • do-while is exit-controlled: the body runs once before the condition is tested, so it executes at least once.

C program:

#include <stdio.h>
int main(void) {
    int n = 10, i = 1, sum = 0;
    while (i <= n) {
        sum += i;
        i++;
    }
    printf("Sum of first %d natural numbers = %d\n", n, sum);
    return 0;
}

Verification for n=10n = 10: Formula gives 10×112=1102=55\dfrac{10 \times 11}{2} = \dfrac{110}{2} = 55.

Program output:

Sum of first 10 natural numbers = 55

Final answer: sum =55= 55 (matches the formula).

control-structuresloops
7short5 marks

Write a recursive C function to compute n!n! (factorial). Show the recursion call stack (unwinding) for n=5n = 5 and state the final value.

Recursive factorial function:

long factorial(int n) {
    if (n <= 1) return 1;        // base case
    return n * factorial(n - 1); // recursive case
}

Call stack for n=5n = 5 (winding down to base case, then unwinding):

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

Unwinding (returning values):

  • factorial(2)=2×1=2factorial(2) = 2 \times 1 = 2
  • factorial(3)=3×2=6factorial(3) = 3 \times 2 = 6
  • factorial(4)=4×6=24factorial(4) = 4 \times 6 = 24
  • factorial(5)=5×24=120factorial(5) = 5 \times 24 = 120

Final value: 5!=1205! = 120.

functionsrecursion
8short4 marks

Write a C program to add two 2×22 \times 2 matrices AA and BB and store the result in CC. Given A=[3572]A = \begin{bmatrix} 3 & 5 \\ 7 & 2 \end{bmatrix} and B=[1468]B = \begin{bmatrix} 1 & 4 \\ 6 & 8 \end{bmatrix}, compute CC by hand.

C program (2x2 matrix addition):

#include <stdio.h>
int main(void) {
    int A[2][2] = {{3,5},{7,2}};
    int B[2][2] = {{1,4},{6,8}};
    int C[2][2], i, j;
    for (i = 0; i < 2; i++)
        for (j = 0; j < 2; j++)
            C[i][j] = A[i][j] + B[i][j];
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++) printf("%d ", C[i][j]);
        printf("\n");
    }
    return 0;
}

Hand computation (Cij=Aij+BijC_{ij} = A_{ij} + B_{ij}):

  • C11=3+1=4C_{11} = 3 + 1 = 4
  • C12=5+4=9C_{12} = 5 + 4 = 9
  • C21=7+6=13C_{21} = 7 + 6 = 13
  • C22=2+8=10C_{22} = 2 + 8 = 10
C=[491310]C = \begin{bmatrix} 4 & 9 \\ 13 & 10 \end{bmatrix}

Program output:

4 9 
13 10 
arraysmatrices
9short4 marks

Explain the purpose of strlen(), strcpy(), and strcmp() from <string.h>. For the strings char a[] = "BRIDGE"; and char b[] = "BEAM";, state the return value of strlen(a), and explain what strcmp(a, b) returns (positive, negative, or zero) and why.

String library functions:

  • strlen(s) returns the number of characters in s excluding the terminating \0.
  • strcpy(dest, src) copies the string src (including \0) into dest.
  • strcmp(s1, s2) compares two strings character-by-character by ASCII value; returns 00 if equal, a negative value if s1 < s2, and a positive value if s1 > s2.

For a = "BRIDGE": strlen(a) counts B-R-I-D-G-E = 6.

strcmp(a, b) with a = "BRIDGE", b = "BEAM": Compare char by char:

  • Index 0: 'B' vs 'B' -> equal, continue.
  • Index 1: 'R' (ASCII 82) vs 'E' (ASCII 69) -> differ. Since 82>6982 > 69, a > b.

strcmp(a, b) returns a positive value (specifically 8269=1382 - 69 = 13 in many implementations), because at the first differing position 'R' has a higher ASCII code than 'E'.

Final answers: strlen(a) = 6; strcmp(a, b) returns a positive value (13).

stringslibrary-functions
10short4 marks

Write a C program that classifies a steel beam's deflection status. Given a measured mid-span deflection δ\delta (mm) and a span LL (mm), the allowable deflection limit is L/360L/360. Print SAFE if δL/360\delta \leq L/360, otherwise UNSAFE. Test it by hand for L=7200 mmL = 7200\ \text{mm} and δ=18 mm\delta = 18\ \text{mm}.

C program:

#include <stdio.h>
int main(void) {
    float L = 7200.0f, delta = 18.0f;
    float limit = L / 360.0f;
    if (delta <= limit)
        printf("SAFE\n");
    else
        printf("UNSAFE\n");
    return 0;
}

Hand test for L=7200L = 7200 mm, δ=18\delta = 18 mm:

  1. Allowable limit =L/360=7200/360=20.0= L/360 = 7200 / 360 = 20.0 mm.
  2. Compare: δ=1820.0\delta = 18 \leq 20.0 -> condition true.
  3. Therefore the program prints SAFE.

Program output:

SAFE

Final answer: limit =20.0= 20.0 mm, δ=18\delta = 18 mm 20.0\leq 20.0 mm, so the beam is SAFE.

control-structuresconditionals
11short8 marks

Write short notes on any two of the following:

(a) Difference between structure and union in C, with memory-size illustration for a type containing one int and one float.

(b) Use of pointers to a structure (the -> operator) with a small C example computing the cross-sectional area of a rectangular column from a struct Column { float width, depth; };.

(c) break vs continue statements with a loop example showing different outputs.

(a) Structure vs Union:

Featurestructunion
MemorySum of all members' sizesSize of the largest member
StorageAll members stored separatelyAll members share one memory location
AccessAll members usable simultaneouslyOnly one member valid at a time

For a type containing one int (4 bytes) and one float (4 bytes): a struct needs 4+4=84 + 4 = 8 bytes, while a union needs only max(4,4)=4\max(4,4) = 4 bytes. (Sizes may grow due to padding.)

(b) Pointer to structure (-> operator):

The -> operator dereferences a structure pointer and accesses a member: ptr->member is equivalent to (*ptr).member.

#include <stdio.h>
struct Column { float width, depth; }; // mm
int main(void) {
    struct Column c = {300.0f, 450.0f};
    struct Column *p = &c;
    float area = p->width * p->depth;   // mm^2
    printf("Area = %.2f mm^2\n", area);
    return 0;
}

For width = 300, depth = 450: area =300×450=135000 mm2= 300 \times 450 = 135000\ \text{mm}^2. Output: Area = 135000.00 mm^2.

(c) break vs continue:

  • break terminates the loop immediately.
  • continue skips the rest of the current iteration and proceeds to the next iteration.
for (int i = 1; i <= 5; i++) {
    if (i == 3) break;      // exits when i == 3
    printf("%d ", i);
}
// Output: 1 2

for (int i = 1; i <= 5; i++) {
    if (i == 3) continue;   // skips i == 3 only
    printf("%d ", i);
}
// Output: 1 2 4 5

Summary: break prints 1 2 (stops at 3); continue prints 1 2 4 5 (skips 3).

pointersstructurescivil-applications

Frequently asked questions

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