Tribhuwan University

Institute of Science and Technology

2080

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.
What is an array? How an array can be passed as function argument? Write a program to arrange an array having 10 integer numbers in ascending order using function.[10]

What is an Array? Passing Array as Function Argument with Program


Definition of Array

An array is a collection of elements of the same data type stored in contiguous memory locations, accessed using a common name and an index (subscript).

Declaration Syntax:

data_type array_name[size];

Example: int marks[10]; — declares an array of 10 integers.

Key Properties:

  • Array index starts from 0 and goes up to size - 1
  • All elements occupy consecutive memory locations
  • Elements are accessed using subscript notation: arr[0], arr[1], ...
  • Size must be a constant expression (fixed at compile time)

Passing Array as Function Argument

When an array is passed to a function, the base address (address of the first element) is passed, not a copy of the entire array. This is called pass by reference.

Three ways to declare a function receiving an array:

  • As a pointer: void func(int *arr)
  • As a sized array: void func(int arr[10])
  • As an unsized array: void func(int arr[])

Important Points:

  • Changes made to array elements inside the function affect the original array
  • The size of the array is usually passed as a separate argument
  • Only the base address is passed, making it memory efficient

Example of function call:

int marks[10];
sort(marks, 10);  // passing array name and size

Program: Arrange 10 Integer Numbers in Ascending Order Using Function

#include <stdio.h>

// Function to sort array in ascending order (Bubble Sort)
void sortAscending(int arr[], int n) {
    int i, j, temp;
    for (i = 0; i < n - 1; i++) {
        for (j = 0; j < n - 1 - i; j++) {
            if (arr[j] > arr[j + 1]) {
                // Swap the elements
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

int main() {
    int arr[10], i;

    // Input 10 integers
    printf("Enter 10 integer numbers:\n");
    for (i = 0; i < 10; i++) {
        scanf("%d", &arr[i]);
    }

    // Call function to sort
    sortAscending(arr, 10);

    // Display sorted array
    printf("Array in ascending order:\n");
    for (i = 0; i < 10; i++) {
        printf("%d ", arr[i]);
    }

    return 0;
}

Sample Output:

Enter 10 integer numbers:
45 12 89 33 7 56 21 98 64 10
Array in ascending order:
7 10 12 21 33 45 56 64 89 98

How the Program Works

  • The sortAscending() function receives the array and its size as arguments
  • It uses Bubble Sort algorithm — repeatedly compares adjacent elements and swaps them if they are in wrong order
  • Since the array is passed by reference (base address), the sorted result is directly reflected in main()
  • Time Complexity: O(n2)O(n^2) in worst case

Conclusion: Arrays provide efficient storage for homogeneous data. When passed to functions, only the base address is transferred, allowing functions to directly modify the original array — making sorting operations simple and memory efficient.

2.
Explain different control structures in C. Write a program in C to perform different arithmetic operations on two integers and display the result based on choice made by user using switch statement.[10]
3.
Differentiate between structure and union? Write a program to read employ id, name, post and salary of employee and display detail of those employee whose post is 'clerk'.[10]
Section B

Short Answers Questions

Attempt any Eight questions.
[8*5=40]
4.
What is recursion? Write a program to find the factorial of a given integer using recursion. [5]
5.
What are conversion specifiers? Explain the basic structure of a C program. [5]
6.
Explain nested if else ladder with suitable example. [5]
7.
Explain structure of for loop. Write a program to reverse a number entered by user. [5]
8.
What is DMA? Write a program to read N numbers and find largest and smallest number using DMA. [5]
9.
Why data file is needed? Write a program to write N numbers in file 'number.txt' and then read it and display only even numbers. [5]
10.
List out different operators in C. Explain Logical and relational operator. [5]
11.
What is pointer? Explain the similarity between array and pointer in brief. [5]
12.
Write short notes on: a) Macro Write short notes on: b) Opening and closing file [2.5+2.5]