Programming Fundamentals, Past Papers, UOS Main Campus

Programming Fundamentals Past Paper 2014 – BSIT 1st Semester UOS (CMP-140)

ProgramBSIT
Semester1st
Year2014
Course codeCMP-140
AnswersSolved ✓

University of Sargodha Programming Fundamentals (CMP-140) final-term paper for BS IT 1st semester, 2014 exam, session 2012-16 (60 marks, 2:30 hours). The paper has been retyped and solved answers for all 12 short questions and complete C++ programs for every long question are given below.

Solved answers

Q1 – Short answers (12 × 2)

i. Counter-controlled vs sentinel-controlled repetition: Counter-controlled repetition runs a known number of times using a counter (for i = 1 to 10). Sentinel-controlled repetition runs an unknown number of times until a special value (sentinel, e.g. −1) is entered.

ii. Define break statement. break immediately exits the nearest loop or switch and transfers control to the statement after it.

iii. Purpose of a C compiler: It translates C source code into machine code (object/executable file) and reports syntax errors.

iv. Purpose of getch(): Reads one character from the keyboard without showing it and without Enter (conio.h); often used to hold the output screen.

v. Why is main() important? Program execution starts from main(); without it the program cannot be linked/run, and its return value tells the OS how the program ended.

vi. Prefix vs postfix operators: ++x increments first and then uses the value; x++ uses the current value and then increments. If x = 5, y = ++x gives y = 6; y = x++ gives y = 5 (x becomes 6 in both).

vii. Evaluate 85-(20+7*20*(5-18)+(20-2)/3)12: The printed expression is missing an operator before 12; taking it as *12: 7*20*(−13) = −1820, (20−2)/3 = 6, inner = 20 − 1820 + 6 = −1794, × 12 = −21528, so 85 − (−21528) = 21613.

viii. Syntax errors in the code: n is not declared; while (k<7); has a stray semicolon (empty loop); 'Sargodha' must be a string "Sargodha"; the program also needs #include <iostream> and using namespace std; and main should return 0.

ix. What is a fatal error? An error that stops the program from compiling or makes it terminate abnormally at run time (e.g. division by zero, missing file).

x. for loop vs while loop: A for loop keeps initialization, condition and update in one line and is used when the number of repetitions is known; a while loop has only the condition and is used when repetition depends on a condition.

xi. Function prototype vs definition: A prototype declares the name, return type and parameters (int sum(int, int);) so the compiler can check calls; the definition contains the body that does the work.

xii. Output of the pointer code: first is 12
second is 20 – my points first to first (set to 12) and then to second (set to 20).

Q2 – Number pattern with nested loops (9)

#include <iostream>
using namespace std;
int main() {
    for (int i = 1; i <= 5; i++) {
        for (int s = 1; s <= 5 - i; s++) cout << " ";
        for (int k = 1; k <= 2 * i - 1; k++) cout << i;
        cout << endl;
    }
    return 0;
}

Q3 – Recursive factorial (9)

#include <iostream>
using namespace std;
long fact(int n) {
    if (n <= 1) return 1;
    return n * fact(n - 1);
}
int main() {
    int n;
    cout << "Enter a number: ";
    cin >> n;
    cout << "Factorial = " << fact(n) << endl;
    return 0;
}

Q4 – Four-function calculator using switch (9)

#include <iostream>
using namespace std;
int main() {
    double a, b; char op;
    cout << "Enter operand operator operand: ";
    cin >> a >> op >> b;
    switch (op) {
        case '+': cout << a + b; break;
        case '-': cout << a - b; break;
        case '*': cout << a * b; break;
        case '/': if (b != 0) cout << a / b; else cout << "Division by zero"; break;
        default: cout << "Invalid operator";
    }
    return 0;
}

Q5 – Copy inputfile.txt to output.txt and display (9)

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
    ifstream in("inputfile.txt");
    ofstream out("output.txt");
    if (!in) { cout << "Cannot open inputfile.txt"; return 1; }
    string line;
    while (getline(in, line)) {
        out << line << endl;   // write to output.txt
        cout << line << endl;  // display on screen
    }
    in.close(); out.close();
    return 0;
}

Q6 – Structure PERSON for tax (9)

#include <iostream>
using namespace std;
struct PERSON {
    double income, taxRate, tax;
};
int main() {
    PERSON p[5];
    for (int i = 0; i < 5; i++) {
        cout << "Income and tax rate (%) of person " << i + 1 << ": ";
        cin >> p[i].income >> p[i].taxRate;
        p[i].tax = p[i].income * p[i].taxRate / 100;
    }
    for (int i = 0; i < 5; i++)
        cout << "Person " << i + 1 << " tax payable = " << p[i].tax << endl;
    return 0;
}

Q7 – Bubble sort with swap by reference (9)

#include <iostream>
using namespace std;
void swapVals(int &x, int &y) { int t = x; x = y; y = t; }
void bubbleSort(int a[], int n) {
    for (int i = 0; i < n - 1; i++)
        for (int j = 0; j < n - 1 - i; j++)
            if (a[j] > a[j + 1]) swapVals(a[j], a[j + 1]);
}
int main() {
    int a[] = {5, 2, 9, 1, 7}, n = 5;
    bubbleSort(a, n);
    for (int i = 0; i < n; i++) cout << a[i] << " ";   // 1 2 5 7 9
    return 0;
}
Save this paper for offline studyDownload the question paper, or save the paper together with all solved answers as a PDF.
↓ Download Paper (JPG)

About Saif R

An educator and technology enthusiast passionate about empowering students through quality education and digital learning. Dedicated to making academic resources and opportunities accessible to everyone.