BIT102 · TU past paper
C Programming 2077 question paper
The complete TU 2077 exam paper for C Programming (BIT102), all 12 questions with solved model answers written to the mark scheme.
Tap a question to open its answer.
- 110 marksRecursive functionsHideAnswer
Explain the role of function in programming. How function is declared, defined and called in C?Write a grogram to find factorial of 'n' using recursive function.[5+5]
--- (a) Role of Functions in Programming A function is a self-contained, named block of code that performs a specific task and can be called (invoked) from anywhere in the program. Role Description ------------------- Modularity Breaks a...
- 210 marksIdentifiers and keywordsHideAnswer
What is an Identifier and keyword? Explain the rules for naming valid identifiers in C with example.Write a C program to find sum of digits of a 'n' digit number.[5+5]
An identifier is a name given to various program elements such as variables, functions, arrays, structures, and other user-defined items. Identifiers are used to uniquely identify a program element during execution. Example: --- A keywor...
- 310 marksDifferences between structure and unionHideAnswer
How structure is different from union? Write a program to store and display basic information (roll, name, address, email, and phone) of students of using a structure.[10]
Structure vs Union in C
Difference Between Structure and Union
Feature Structure Union Keyword structunionMemory Allocation Each member gets its own separate memory All members share the same memory location Total Size Sum of sizes of all members Size of the largest member only Data Storage All members can store values simultaneously Only one member can hold a value at a time Access All members can be accessed at any time Only the last written member gives valid data Usage When all data fields are needed together When only one field is needed at a time (saves memory) Example of size difference:
struct S { int a; float b; char c; }; // Size = 4 + 4 + 1 = 9 bytes (approx, with padding) union U { int a; float b; char c; }; // Size = 4 bytes (size of largest member: int or float)
C Program to Store and Display Student Information Using Structure
#include <stdio.h> #include <string.h> // Define the structure for student information struct Student { int roll; char name[50]; char address[100]; char email[50]; char phone[15]; }; int main() { int i, n; printf("Enter the number of students: "); scanf("%d", &n); // Declare an array of structures struct Student s[n]; // Input student information for (i = 0; i < n; i++) { printf("\n--- Enter details for Student %d ---\n", i + 1); printf("Roll Number : "); scanf("%d", &s[i].roll); printf("Name : "); // Clear input buffer before reading string getchar(); fgets(s[i].name, sizeof(s[i].name), stdin); s[i].name[strcspn(s[i].name, "\n")] = '\0'; // Remove newline printf("Address : "); fgets(s[i].address, sizeof(s[i].address), stdin); s[i].address[strcspn(s[i].address, "\n")] = '\0'; printf("Email : "); fgets(s[i].email, sizeof(s[i].email), stdin); s[i].email[strcspn(s[i].email, "\n")] = '\0'; printf("Phone : "); fgets(s[i].phone, sizeof(s[i].phone), stdin); s[i].phone[strcspn(s[i].phone, "\n")] = '\0'; } // Display student information printf("\n\n========== Student Information ==========\n"); for (i = 0; i < n; i++) { printf("\nStudent %d:\n", i + 1); printf(" Roll Number : %d\n", s[i].roll); printf(" Name : %s\n", s[i].name); printf(" Address : %s\n", s[i].address); printf(" Email : %s\n", s[i].email); printf(" Phone : %s\n", s[i].phone); } return 0; }
Sample Output
Enter the number of students: 2 --- Enter details for Student 1 --- Roll Number : 101 Name : Ram Sharma Address : Kathmandu Email : [email protected] Phone : 9841000001 --- Enter details for Student 2 --- Roll Number : 102 Name : Sita Thapa Address : Pokhara Email : [email protected] Phone : 9841000002 ========== Student Information ========== Student 1: Roll Number : 101 Name : Ram Sharma Address : Kathmandu Email : [email protected] Phone : 9841000001 Student 2: Roll Number : 102 Name : Sita Thapa Address : Pokhara Email : [email protected] Phone : 9841000002
Key Points to Remember
struct Studentdefines a template/blueprint for student data.- The dot operator (
.) is used to access structure members. - An array of structures (
struct Student s[n]) allows storing multiple students. - Structure is preferred here because all fields are needed simultaneously.
- 45 marksBasic structure of C programHideAnswer
What is the basic structure of a C program? Explain each part. [5]
Basic Structure of a C Program
Basic Structure of a C Program
A C program is organized into several distinct sections. Below is the general template:
/* Documentation Section */ // Program description, author, date /* Link Section */ #include <stdio.h> #include <stdlib.h> /* Definition Section */ #define PI 3.14159 /* Global Declaration Section */ int globalVar = 10; /* Main Function */ int main() { /* Local Declaration */ int a, b; /* Executable Statements */ printf("Hello, World!\n"); return 0; } /* User-Defined Functions */ void myFunction() { // function body }
Explanation of Each Part
1. Documentation Section
- Contains comments about the program such as program name, author, date, and purpose.
- Written using
/* ... */(multi-line) or//(single-line). - Ignored by the compiler; meant for human readability.
2. Link Section (Preprocessor Directives)
- Contains header file inclusions using
#include. - Tells the compiler to include standard library files before compilation.
- Example:
#include <stdio.h>includes standard input/output functions likeprintf()andscanf().
3. Definition Section
- Contains macro definitions using
#define. - Defines symbolic constants or macros used throughout the program.
- Example:
#define PI 3.14159
4. Global Declaration Section
- Declares global variables and function prototypes.
- Global variables are accessible by all functions in the program.
- Example:
int count = 0;
5. Main Function (
main())- The entry point of every C program; execution always begins here.
- Contains two parts:
- Local Declaration: Variables declared inside
main(). - Executable Statements: The actual logic, calculations, input/output operations.
- Local Declaration: Variables declared inside
- Returns an integer value (typically
0to indicate successful execution).
6. User-Defined Functions
- Functions written by the programmer to perform specific tasks.
- Defined after
main()(or declared before using prototypes). - Promotes modularity and code reusability.
Summary Table
Section Purpose Documentation Comments and description Link Include header files Definition Define constants/macros Global Declaration Declare global variables main()Program execution starts here User-Defined Functions Custom reusable functions - 55 marksMulti-dimensional arraysHideAnswer
What do you mean by multi-dimensional array? Write program logic to add and display the sum of two m x n matrices. [5]
Multi-Dimensional Array
Definition
A multi-dimensional array is an array that has more than one dimension (index). It can be thought of as an array of arrays. The most common form is the two-dimensional (2D) array, which represents data in rows and columns (like a matrix or table).
General Syntax (C language):
data_type array_name[size1][size2]...[sizeN];For a 2D array (matrix of m rows and n columns):
int A[m][n];Elements are accessed using two indices:
A[i][j]whereiis the row index andjis the column index.
Program Logic to Add Two m x n Matrices and Display the Sum
Algorithm / Logic
- Declare three m x n matrices:
A,B, andC(for the result). - Input all elements of matrix
A. - Input all elements of matrix
B. - For each element at position
[i][j], computeC[i][j] = A[i][j] + B[i][j]. - Display the resultant matrix
C.
C Program
#include <stdio.h> int main() { int m, n; int A[10][10], B[10][10], C[10][10]; int i, j; /* Step 1: Input dimensions */ printf("Enter number of rows (m): "); scanf("%d", &m); printf("Enter number of columns (n): "); scanf("%d", &n); /* Step 2: Input elements of Matrix A */ printf("\nEnter elements of Matrix A (%d x %d):\n", m, n); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { printf("A[%d][%d] = ", i, j); scanf("%d", &A[i][j]); } } /* Step 3: Input elements of Matrix B */ printf("\nEnter elements of Matrix B (%d x %d):\n", m, n); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { printf("B[%d][%d] = ", i, j); scanf("%d", &B[i][j]); } } /* Step 4: Add the two matrices */ for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { C[i][j] = A[i][j] + B[i][j]; } } /* Step 5: Display the resultant matrix C */ printf("\nSum Matrix C = A + B:\n"); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { printf("%d\t", C[i][j]); } printf("\n"); } return 0; }
Sample Output
Enter number of rows (m): 2 Enter number of columns (n): 2 Enter elements of Matrix A (2 x 2): A[0][0] = 1 A[0][1] = 2 A[1][0] = 3 A[1][1] = 4 Enter elements of Matrix B (2 x 2): B[0][0] = 5 B[0][1] = 6 B[1][0] = 7 B[1][1] = 8 Sum Matrix C = A + B: 6 8 10 12
Key Points
Concept Detail Storage Elements stored in row-major order in memory Access A[i][j]accesses element at rowi, columnjCondition Both matrices must have the same dimensions (m x n) for addition Time Complexity O(m x n) for addition - Declare three m x n matrices:
- 65 marksCompilation and execution processHideAnswer
Find errors (If any) in the following program and rewrite it.
#define (studio,h) float main < > int m [ ] [ ] = 1,2,3,4; for (i = 4, I>=0 ; i--) for (j=0, j <4; j++); m[i] [j] = a[i][j] + b[i][j]; return 0. 0; }[5]
--- Erroneous Code Error Description ------------------------------------- 1 define (studio,h) Wrong preprocessor directive; should be include <stdio.h 2 float main < main must return int, not float; < should be () 3 int m[][] 2D array d...
- 75 marksPattern generation using arraysHideAnswer
Write a program to generate the following pattern of Integers.
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5[5]
Program to Generate Integer Pattern
Understanding the Pattern
Looking at the pattern carefully:
1 1 2 1 1 2 1 2 3 1 1 2 3 1 2 3 1 2 3 4 1 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 5 1 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5Pattern Logic:
- Row
icontainsigroups - Each group in row
iprints numbers from1toi - Except the last group which prints
1toi(for last row) or ends with the row number
C Program
#include <stdio.h> int main() { int n = 5; // Number of rows int i, j, k; for (i = 1; i <= n; i++) { // Print i groups of (1 to i) for (j = 1; j <= i; j++) { for (k = 1; k <= i; k++) { printf("%d ", k); } } printf("\n"); } return 0; }
Output
1 1 2 1 2 1 2 3 1 2 3 1 2 3 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
Alternative Pattern (Pyramid Style)
#include <stdio.h> int main() { int n = 5; int i, j, k; for (i = 1; i <= n; i++) { // Each row: groups from 1..1, 1..2, ..., 1..i for (j = 1; j <= i; j++) { for (k = 1; k <= j; k++) { printf("%d ", k); } } printf("\n"); } return 0; }Output:
1 1 1 2 1 1 2 1 2 3 1 1 2 1 2 3 1 2 3 4 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Key Concepts Used
Concept Description Outer loop Controls the row number ( ifrom 1 to n)Middle loop Controls number of groups per row ( jfrom 1 to i)Inner loop Prints numbers 1 to i (or 1 to j) in each group Nested loops Three levels of nesting required Note: The exact pattern depends on interpretation of the question. Both versions use the same three-nested-loop logic which is the core concept being tested.
- Row
- 85 marksString manipulation and palindrome checkinHideAnswer
Write a C program to check whether a given string is palindrome or not. [5]
A palindrome is a string that reads the same forwards and backwards. Example: "madam", "racecar", "level" --- --- Step Description ------------------- 1 Read the string from the user 2 Find the length using strlen() 3 Compare str[0] with...
- 95 marksDouble pointersHideAnswer
What is pointer? Illustrate the use of a double pointer with suitable example. [5]
A pointer is a variable that stores the memory address of another variable rather than storing a direct value. Syntax: Example: - &x gives the address of variable x - p gives the value at the address stored in p (dereferencing) --- A dou...
- 105 marksFile opening modesHideAnswer
Why file is used in programming? Explain different file opening modes. [5]
Files are used in programming for the following reasons: 1. Permanent/Persistent Storage: Data stored in variables and arrays is lost when the program terminates. Files allow data to be stored permanently on secondary storage (disk). 2. ...
- 115 marksMacros and macro vs functionHideAnswer
Write short notes on: a) Macros Write short notes on: b) Null Pointers [2.5+2.5]
A macro is a preprocessor directive in C that allows the programmer to define symbolic names or constants and to create inline code substitutions before the actual compilation begins. Macros are processed by the C preprocessor using the ...
- 125 marksBreak and continue statementsHideAnswer
How break statement is different from continue statement? Explain with suitable example. [5]
Feature break continue --------- Purpose Terminates the entire loop immediately Skips only the current iteration Control flow Exits the loop body completely Goes back to the loop condition check Remaining iterations All remaining iterati...