Browse papers
A

Section A: Long Answer Questions

Attempt all questions.

5 questions
1long10 marks

A surveyor records the chainage (in metres) of nn points along a road centre-line. Write a C program that:

(a) reads the number of points nn and then reads nn chainage readings into an array,

(b) computes and prints the total length of the surveyed stretch (difference between the last and first readings),

(c) computes the average spacing between consecutive points, and

(d) reports how many spacings exceed 20m20\,\text{m}.

Assume the readings are entered in increasing order. Explain the role of the loop(s) used.

Program

#include <stdio.h>

int main(void) {
    int n, i, count = 0;
    float ch[100], spacing, total, avg;

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

    printf("Enter %d chainage readings (m):\n", n);
    for (i = 0; i < n; i++)
        scanf("%f", &ch[i]);

    /* (b) total length */
    total = ch[n - 1] - ch[0];

    /* (c) average spacing and (d) count > 20 m */
    for (i = 1; i < n; i++) {
        spacing = ch[i] - ch[i - 1];
        if (spacing > 20.0f)
            count++;
    }
    avg = total / (n - 1);   /* n-1 intervals */

    printf("Total length      = %.2f m\n", total);
    printf("Average spacing   = %.2f m\n", avg);
    printf("Spacings > 20 m   = %d\n", count);
    return 0;
}

Worked example. For n=5n=5 and readings 0,18,45,60,880, 18, 45, 60, 88 m:

  • Total length =880=88 m= 88 - 0 = \mathbf{88\ m}.
  • Intervals =n1=4= n-1 = 4. Spacings: 18,27,15,2818, 27, 15, 28 m.
  • Average spacing =88/4=22 m= 88/4 = \mathbf{22\ m}.
  • Spacings >20>20 m: 27,28227, 28 \Rightarrow \mathbf{2}.

Role of loops. The first for loop is an input loop that fills the array index-by-index. The second for loop is a processing loop that walks adjacent pairs ch[i]-ch[i-1]; it both accumulates the count of large gaps and conceptually drives the spacing analysis. Using n1n-1 in the average reflects that nn points define n1n-1 intervals.

control-structuresloopscivil-application
2long10 marks

Differentiate between call by value and call by reference in C with a clear example of each. Then write a C function gcd() using recursion to find the greatest common divisor of two positive integers, and trace its execution for gcd(48, 36).

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 callerOriginal variable unchangedOriginal variable can be modified
Parameter typeOrdinary variablePointer

Call by value example (swap fails to affect caller):

void swap(int a, int b){ int t=a; a=b; b=t; }   /* caller unchanged */

Call by reference example (swap works):

void swap(int *a, int *b){ int t=*a; *a=*b; *b=t; }  /* call: swap(&x,&y); */

Recursive GCD (Euclid's algorithm)

int gcd(int a, int b) {
    if (b == 0)
        return a;            /* base case */
    return gcd(b, a % b);    /* recursive case */
}

Trace of gcd(48, 36) (using gcd(a,b)=gcd(b,amodb)\gcd(a,b)=\gcd(b, a\bmod b)):

Callaba % bReturns
1483612gcd(36,12)
236120gcd(12,0)
312012 (base case)

The result unwinds back up the call stack, giving gcd(48,36)=12\gcd(48,36)=\mathbf{12}.

functionsrecursionmodular-programming
3long10 marks

In a structural analysis problem the stiffness contributions of three members at a joint are stored in two 3×33\times3 matrices AA and BB. Write a C program to read two 3×33\times3 matrices and print their sum C=A+BC=A+B and their transpose of the sum. Demonstrate the output for:

A=[201132045],B=[120013201]A=\begin{bmatrix}2&0&1\\1&3&2\\0&4&5\end{bmatrix},\qquad B=\begin{bmatrix}1&2&0\\0&1&3\\2&0&1\end{bmatrix}

Program

#include <stdio.h>
#define N 3

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

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

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

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

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

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

Computed output. Element-wise addition Cij=Aij+BijC_{ij}=A_{ij}+B_{ij}:

C=A+B=[321145246]C=A+B=\begin{bmatrix}3&2&1\\1&4&5\\2&4&6\end{bmatrix}

Verification of a few entries: C11=2+1=3C_{11}=2+1=3, C23=2+3=5C_{23}=2+3=5, C33=5+1=6C_{33}=5+1=6.

Transpose CTC^{T} (swap rows and columns, CijT=CjiC^{T}_{ij}=C_{ji}):

CT=[312244156]C^{T}=\begin{bmatrix}3&1&2\\2&4&4\\1&5&6\end{bmatrix}
arraysmatricescivil-application
4long10 marks

Define a C structure named Beam to store the id (int), length in metres (float) and uniform load in kN/m (float) of a simply supported beam. Write a program that reads details of nn beams into an array of structures, computes the maximum bending moment of each beam using Mmax=wL28M_{max}=\dfrac{w L^{2}}{8}, and prints the beam with the largest maximum moment. Show the working for two beams: Beam 101 (L=6m,w=12kN/mL=6\,\text{m}, w=12\,\text{kN/m}) and Beam 102 (L=8m,w=10kN/mL=8\,\text{m}, w=10\,\text{kN/m}).

Program

#include <stdio.h>

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

int main(void) {
    int n, i, maxIndex = 0;
    struct Beam b[50];
    float m, maxM;

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

    for (i = 0; i < n; i++) {
        printf("Beam %d -> id length load: ", i + 1);
        scanf("%d %f %f", &b[i].id, &b[i].length, &b[i].load);
    }

    maxM = (b[0].load * b[0].length * b[0].length) / 8.0f;
    for (i = 1; i < n; i++) {
        m = (b[i].load * b[i].length * b[i].length) / 8.0f;
        if (m > maxM) { maxM = m; maxIndex = i; }
    }

    printf("Beam %d has the largest Mmax = %.2f kN.m\n",
           b[maxIndex].id, maxM);
    return 0;
}

Worked calculation using Mmax=wL28M_{max}=\dfrac{wL^{2}}{8}:

  • Beam 101: Mmax=12×628=12×368=4328=54.00 kN ⁣ ⁣mM_{max}=\dfrac{12\times 6^{2}}{8}=\dfrac{12\times 36}{8}=\dfrac{432}{8}=\mathbf{54.00\ kN\!\cdot\!m}.
  • Beam 102: Mmax=10×828=10×648=6408=80.00 kN ⁣ ⁣mM_{max}=\dfrac{10\times 8^{2}}{8}=\dfrac{10\times 64}{8}=\dfrac{640}{8}=\mathbf{80.00\ kN\!\cdot\!m}.

Since 80.00>54.0080.00 > 54.00, the program prints Beam 102 with the largest maximum moment of 80.00 kN ⁣ ⁣m\mathbf{80.00\ kN\!\cdot\!m}.

structurescivil-applicationarrays-of-structures
5long10 marks

A laboratory stores concrete cube test results in a text file cubes.txt, each line containing a cube id and its crushing load in kN. Write a C program that:

(a) opens the file for reading (handle the case where the file cannot be opened),

(b) reads each record until end-of-file,

(c) converts each crushing load to compressive strength in MPa using a 150mm×150mm150\,\text{mm}\times150\,\text{mm} cube (strength=load×1000150×150\text{strength}=\dfrac{\text{load}\times1000}{150\times150}), and

(d) writes id, load and strength to an output file report.txt.

Show a sample numeric conversion for a load of 562.5kN562.5\,\text{kN}.

Program

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

int main(void) {
    FILE *fin, *fout;
    int id;
    float load, strength;
    int area = 150 * 150;   /* mm^2 */

    fin = fopen("cubes.txt", "r");
    if (fin == NULL) {
        printf("Error: cannot open cubes.txt\n");
        return 1;
    }
    fout = fopen("report.txt", "w");
    if (fout == NULL) {
        printf("Error: cannot create report.txt\n");
        fclose(fin);
        return 1;
    }

    fprintf(fout, "ID\tLoad(kN)\tStrength(MPa)\n");
    while (fscanf(fin, "%d %f", &id, &load) == 2) {
        strength = (load * 1000.0f) / area;   /* N/mm^2 = MPa */
        fprintf(fout, "%d\t%.2f\t\t%.2f\n", id, load, strength);
    }

    fclose(fin);
    fclose(fout);
    printf("Report generated in report.txt\n");
    return 0;
}

Sample conversion for load =562.5kN=562.5\,\text{kN} on a 150×150150\times150 mm cube:

strength=562.5×1000 N150×150 mm2=56250022500=25.00 MPa\text{strength}=\frac{562.5\times1000\ \text{N}}{150\times150\ \text{mm}^2}=\frac{562500}{22500}=\mathbf{25.00\ MPa}

Since 1 N/mm2=1 MPa1\ \text{N/mm}^2 = 1\ \text{MPa}, the strength is 25.00 MPa. The fscanf return value of 2 confirms two fields were read successfully and terminates the loop at end-of-file.

file-handlingcivil-applicationio
B

Section B: Short Answer Questions

Attempt all questions.

6 questions
6short5 marks

Predict and justify the output of the following C program. Explain the effect of integer division and operator precedence.

#include <stdio.h>
int main(void) {
    int a = 7, b = 2;
    float c;
    c = a / b;
    printf("%.2f\n", c);
    c = (float)a / b;
    printf("%.2f\n", c);
    printf("%d\n", a % b + b * 2);
    return 0;
}

Output

3.00
3.50
5

Justification.

  1. c = a / b; — both a and b are int, so 7 / 2 is integer division giving 3 (the fractional part is discarded before assignment). Storing into the float c then prints 3.00.
  2. c = (float)a / b; — the cast promotes a to 7.0, forcing floating-point division: 7.0/2=3.57.0/2 = 3.5, printed as 3.50.
  3. a % b + b * 2 — by precedence, % and * bind tighter than +. So a%b=7%2=1a\%b = 7\%2 = 1, b2=4b*2 = 4, then 1+4=51 + 4 = \mathbf{5}.

Key lesson: type of operands, not the type of the destination, determines whether division is integer or floating point.

fundamentalsdata-typesoperators
7short6 marks

Explain the relationship between arrays and pointers in C. Write a C function float average(float *arr, int n) that returns the average of n float values using pointer arithmetic (no array subscript []), and show how it is called for the array {12.5, 15.0, 9.5, 18.0}.

Array–pointer relationship. The name of an array decays to a pointer to its first element: for float a[4], the expression a is equivalent to &a[0], and a[i] is exactly *(a + i). Pointer arithmetic scales by the element size automatically, so arr + 1 advances by one float.

Function using pointer arithmetic

float average(float *arr, int n) {
    float sum = 0.0f;
    int i;
    for (i = 0; i < n; i++)
        sum += *(arr + i);   /* same as arr[i] */
    return sum / n;
}

Call

float data[] = {12.5, 15.0, 9.5, 18.0};
printf("Average = %.2f\n", average(data, 4));

Result. Sum =12.5+15.0+9.5+18.0=55.0=12.5+15.0+9.5+18.0 = 55.0; average =55.0/4=13.75=55.0/4 = \mathbf{13.75}. The program prints Average = 13.75.

pointersarraysmemory
8short6 marks

Write a C program that classifies a soil sample by its plasticity index PIPI entered by the user using a switch-like decision (you may use if-else if):

  • PI<7PI < 7: Low plasticity
  • 7PI<177 \le PI < 17: Medium plasticity
  • PI17PI \ge 17: High plasticity

State the output for PI=12PI = 12.

Program

#include <stdio.h>

int main(void) {
    float PI;
    printf("Enter plasticity index PI: ");
    scanf("%f", &PI);

    if (PI < 7.0f)
        printf("Low plasticity\n");
    else if (PI < 17.0f)        /* 7 <= PI < 17 */
        printf("Medium plasticity\n");
    else
        printf("High plasticity\n");

    return 0;
}

Note on switch. A switch works only on integer/character constant labels, so it cannot test ranges directly; the natural tool for continuous ranges is the if-else if ladder shown above. (One could switch on an integer band code, e.g. (int)(PI/7), but the ladder is clearer.)

Output for PI=12PI = 12. Since 1212 is not <7<7 but is <17<17, the second branch executes, printing:

Medium plasticity
control-structuresbranchingswitch
9short6 marks

Write a C function to compute the factorial of a non-negative integer iteratively, and use it inside main() to evaluate the number of ways nCr=n!r!(nr)!^{n}C_{r}=\dfrac{n!}{r!\,(n-r)!}. Compute 6C2^{6}C_{2} by hand to verify your program.

Program

#include <stdio.h>

long factorial(int x) {
    long f = 1;
    int i;
    for (i = 2; i <= x; i++)
        f *= i;
    return f;          /* factorial(0)=factorial(1)=1 */
}

int main(void) {
    int n, r;
    long nCr;
    printf("Enter n and r: ");
    scanf("%d %d", &n, &r);

    nCr = factorial(n) / (factorial(r) * factorial(n - r));
    printf("C(%d,%d) = %ld\n", n, r, nCr);
    return 0;
}

Hand verification of 6C2^{6}C_{2}:

6C2=6!2!(62)!=7202×24=72048=15^{6}C_{2}=\frac{6!}{2!\,(6-2)!}=\frac{720}{2\times24}=\frac{720}{48}=\mathbf{15}

where 6!=7206!=720, 2!=22!=2 and 4!=244!=24. The program prints C(6,2) = 15.

functionsloopsnumerical-method
10short5 marks

Without using the <string.h> library, write a C program that counts the number of vowels and the total length of a string entered by the user. Show the output for the input Tribhuvan.

Program

#include <stdio.h>

int main(void) {
    char str[100], ch;
    int i = 0, vowels = 0, len = 0;

    printf("Enter a string: ");
    scanf("%99s", str);     /* reads one word */

    while (str[i] != '\0') {
        ch = str[i];
        if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||
            ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
            vowels++;
        len++;
        i++;
    }

    printf("Length = %d\n", len);
    printf("Vowels = %d\n", vowels);
    return 0;
}

Output for Tribhuvan. The string has characters T r i b h u v a n.

  • Length =9= 9 (loop stops at the '\0' terminator).
  • Vowels: i, u, a \Rightarrow 3.
Length = 9
Vowels = 3
stringsarraysfundamentals
11short6 marks

Define an algorithm and a flowchart. Draw a flowchart and write the algorithm to find the largest of three numbers aa, bb, cc. Trace it for a=14a=14, b=27b=27, c=19c=19.

Definitions. An algorithm is a finite, ordered sequence of unambiguous steps that solves a problem. A flowchart is a graphical representation of an algorithm using standard symbols (oval = start/stop, parallelogram = input/output, rectangle = process, diamond = decision).

Algorithm (largest of three)

Step 1: Start
Step 2: Read a, b, c
Step 3: If a >= b and a >= c, then large = a
Step 4: Else if b >= c, then large = b
Step 5: Else large = c
Step 6: Print large
Step 7: Stop

Flowchart (ASCII)

        ( Start )
            |
     [ Read a, b, c ]
            |
      < a>=b && a>=c? > --yes--> [ large = a ]--+
            | no                                |
      < b>=c? > --------yes--> [ large = b ]-----+
            | no                                |
      [ large = c ]------------------------------+
            |
     [ Print large ]
            |
         ( Stop )

Trace for a=14a=14, b=27b=27, c=19c=19:

  • Is aba\ge b and aca\ge c? 142714\ge27 is false \Rightarrow skip.
  • Is bcb\ge c? 271927\ge19 is true \Rightarrow large = b = 27.
  • Output: 27.

The largest number is 27\mathbf{27}.

fundamentalsalgorithmflowchart

Frequently asked questions

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