Tribhuwan University

Institute of Science and Technology

2079

Bachelor Level / First Year / First Semester / Science

Bachelors in Information Technology (BIT102)

(C Programming)

Full Marks: 60

Pass Marks: 24

Time: 3 Hours

Candidates are required to give their answers in their own words as for as practicable.

The figures in the margin indicate full marks.

Section A

Long Answers Questions

Attempt any TWO questions.
[2*10=20]
1.
List the advantages and disadvantages of global variable. Assume the two data files "numl.txt" and "num2.txt" containing integers. Merge these two files to the third file named " file3.txt". Here, the integers of " file3.t-xt" must be written in sorted order.[10]

Part A: Advantages and Disadvantages of Global Variables

A global variable is a variable declared outside all functions, accessible from any function throughout the program.

Advantages:

  • Universal Accessibility — Any function in the program can access and modify the global variable without passing it as a parameter.
  • Reduced Parameter Passing — No need to pass variables as arguments between multiple functions, simplifying function calls.
  • Data Sharing — Multiple functions can share data easily through global variables.
  • Persistence — Global variables retain their value throughout the entire program execution.

Disadvantages:

  • Uncontrolled Access — Any function can accidentally modify the global variable, making debugging difficult.
  • Namespace Pollution — Too many global variables can cause naming conflicts in large programs.
  • Reduced Modularity — Functions become dependent on global variables, making them harder to reuse in other programs.
  • Security Risk — Data is exposed to all functions, violating the principle of data hiding.
  • Difficult Debugging — Hard to track which function changed the variable's value when errors occur.

Part B: Merge Two Sorted Files into a Third Sorted File

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

int main() {
    FILE *f1, *f2, *f3;
    int a[100], b[100], c[200];
    int i = 0, j = 0, k = 0, n1 = 0, n2 = 0, temp;

    // Read integers from num1.txt
    f1 = fopen("num1.txt", "r");
    if (f1 == NULL) {
        printf("Cannot open num1.txt\n");
        exit(1);
    }
    while (fscanf(f1, "%d", &a[n1]) != EOF) {
        n1++;
    }
    fclose(f1);

    // Read integers from num2.txt
    f2 = fopen("num2.txt", "r");
    if (f2 == NULL) {
        printf("Cannot open num2.txt\n");
        exit(1);
    }
    while (fscanf(f2, "%d", &b[n2]) != EOF) {
        n2++;
    }
    fclose(f2);

    // Copy all elements into array c
    for (i = 0; i < n1; i++)
        c[k++] = a[i];
    for (j = 0; j < n2; j++)
        c[k++] = b[j];

    // Sort array c using Bubble Sort
    for (i = 0; i < k - 1; i++) {
        for (j = 0; j < k - i - 1; j++) {
            if (c[j] > c[j + 1]) {
                temp = c[j];
                c[j] = c[j + 1];
                c[j + 1] = temp;
            }
        }
    }

    // Write sorted integers to file3.txt
    f3 = fopen("file3.txt", "w");
    if (f3 == NULL) {
        printf("Cannot open file3.txt\n");
        exit(1);
    }
    for (i = 0; i < k; i++) {
        fprintf(f3, "%d\n", c[i]);
    }
    fclose(f3);

    printf("Files merged and sorted successfully into file3.txt\n");
    return 0;
}

Logic Explanation:

  • Read all integers from num1.txt and num2.txt into separate arrays.
  • Combine both arrays into a single array c[].
  • Sort the combined array using Bubble Sort in ascending order.
  • Write the sorted integers into file3.txt using fprintf().

Conclusion: Global variables offer easy data sharing but compromise modularity and security. The file merging program demonstrates file handling with fopen(), fscanf(), fprintf(), and fclose() along with sorting logic to produce a sorted output file.

2.
How nested Structure is defined and initialized? Explain with an example.Write a program, using structure, to input records of 10 students. Members include name, roll number, marks obtained in math, C program and English. Display the records of students who have passed in C Program.[4+6]
3.
When do we use for loop and while loop? Compare the while and do while loop with suitable example.Write a program to find average, minimum and maximum age in a class of 20 students.[4+6]
Section B

Short Answers Questions

Attempt any Eight questions.
[8*5=40]
4.
What do you mean by flowchart? Draw a Flowchart to find the largest among three numbers entered by user. [5]
5.
Why array and pointer is similar? Write a program to show the similarity between an array and pointer. [5]
6.
Write a program to find the sum of diagonal elements of n*n matrix. [5]
7.
How keyword is different from Variables? Discuss Basic data types with its ranges. [5]
8.
What is operator associativity? Explain conditional and Logical operators in brief. [5]
9.
Write a program with a function that takes an array as an argument and returns its sum to the main function. [5]
10.
How do you include a file to another file? Illustrate with an example. [5]
11.
Write a program to display the following pattern using the string "NEPAL".
NN
NENE
NEPNEP
NEPANEPA
NEPALNEPAL
[5]
12.
Write short notes on: a) Macro vs Function Write short notes on: b) Break vs Continue [2.5+2.5]