C Programming & Data Structures

Part A
(2 Marks)
1 What is algorithm?
An Algorithm is a finite set of instructions to be followed in a step-by-step manner to accomplish a particular task.
2 What are the different generations of computers?
There are five generations of computers:
  • First Generation: Vacuum Tube Based
  • Second Generation: Transistor Based
  • Third Generation: Integrated Circuit Based
  • Fourth Generation: Microprocessor Based
  • Fifth Generation: Artificial Intelligence Based
3 What are keywords in ‘C’?
C keywords are reserved words by the compiler that have been assigned a fixed meaning. They cannot be used as variable names.
Examples: auto, double, int, struct, break, else, long, switch.
4 Define variables in ‘C’.
A variable in C is a name given to a memory location where data is stored. It allows the programmer to store values that can change during program execution. Each variable has a data type, which decides what kind of data it can hold (like int, float, char, etc.). Variables make programs flexible because they store values and can be used in expressions and calculations.
5 Define a string in ‘C’. How do you declare it?
A String is a sequence of characters terminated with a null character '\0'. The C String is stored as an array of characters.
It is declared as:
char string_name[size];
6 What is a function in ‘C’?
A function is a set of statements which perform some specific tasks when called. It is the basic building block of a C program that provides modularity and code reusability.
7 What is a self-referential structure?
Self-Referential structures are those structures that have one or more pointers which point to the same type of structure, as their member.
8 What is a pointer to a function?
Function pointers point to the functions. They are different from the rest of the pointers in the sense that instead of pointing to the data, they point to the code.
9 Differentiate between a structure and a union.
Feature Structure (struct) Union (union)
Size Equal to or greater than the sum of all members. Equal to the size of the largest member.
Storage Stores distinct values for all members simultaneously. Stores value for only one member at a time.
10 What is file? Define file handling in C.
A file is a collection of data stored on secondary memory. File handling is the process of handling file operations such as create, open, read, write, and close on a file using different functions such as fopen(), fwrite(), fread(), fseek(), and fprintf().
Part B
(5 Marks)
11 Describe the different categories of software.
1. System Software

System software directly operates the computer hardware and provides basic functionality to users and other software.

  • Operating System: Manages resources (memory, CPU). Examples: Linux, Windows.
  • Language Processor: Converts source code to machine code. Examples: Compiler, Assembler.
  • Device Driver: Controls specific devices like printers or modems.
  • Utility Software: Maintenance tools like Antivirus and disk cleaners.
2. Application Software

Software designed to perform specific tasks for end-users.

  • General Purpose: Not limited to one function. Examples: MS-Word, Excel.
  • Customized Software: Tailored for specific organizations. Example: Railway reservation systems.
12 Explain the structure of a ‘C’ program with proper syntax and an example.
A C program consists of 6 main sections:
  1. Documentation: Comments describing the program.
  2. Preprocessor Section: Header files (e.g., #include).
  3. Definition: Constants (e.g., #define).
  4. Global Declaration: Global variables and functions.
  5. Main() Function: The entry point of execution.
  6. Sub Programs: User-defined functions.
// Documentation Section /* * file: sum.c * author: you * description: program to find sum. */ // Link (Preprocessor Section) #include <stdio.h> // Definition #define X 20 // Global Declaration int sum(int y); // Main() Function int main(void) { int y = 55; printf("Sum: %d", sum(y)); return 0; } // Subprogram int sum(int y) { return y + X; }
13 Write a program to check if a string is a palindrome or not without using function.

A palindrome reads the same backwards as forwards (e.g., "MADAM").

#include <stdio.h> #include <string.h> int main() { char str[100]; int i, length = 0, flag = 0; printf("Enter a string: "); scanf("%s", str); // Calculate length manually while (str[length] != '\0') { length++; } // Compare characters from start and end for (i = 0; i < length / 2; i++) { if (str[i] != str[length - i - 1]) { flag = 1; // Flag raised if mismatch found break; } } if (flag == 0) { printf("%s is a Palindrome\n", str); } else { printf("%s is Not a Palindrome\n", str); } return 0; }
14 Write a program to create employee payroll using nested structure.

This program demonstrates a structure (Salary) inside another structure (Employee).

#include <stdio.h> // Child Structure for Salary Details struct Salary { int basic; int hra; int da; }; // Parent Structure for Employee struct Employee { int id; char name[20]; struct Salary pay; // Nested structure variable }; int main() { struct Employee emp; int gross_salary; printf("Enter Employee ID: "); scanf("%d", &emp.id); printf("Enter Employee Name: "); scanf("%s", emp.name); printf("Enter Basic Pay: "); scanf("%d", &emp.pay.basic); // Accessing nested member printf("Enter HRA: "); scanf("%d", &emp.pay.hra); printf("Enter DA: "); scanf("%d", &emp.pay.da); // Calculate Gross Salary gross_salary = emp.pay.basic + emp.pay.hra + emp.pay.da; // Display Payroll Info printf("\n--- Employee Payroll ---\n"); printf("ID: %d\n", emp.id); printf("Name: %s\n", emp.name); printf("Gross Salary: %d\n", gross_salary); return 0; }
15 Discuss the various storage classes in C with an example for each.
Storage Classes
  • auto: Default for local variables. Scope is within the block.
  • extern: Declares a global variable defined elsewhere. Visible across files.
  • static: Preserves value between function calls. Lifetime is entire program execution.
  • register: Requests storage in CPU register for faster access.
#include <stdio.h> // Declare an extern variable extern int externVar; int externVar = 10; void staticDemo() { static int staticVar = 0; // Static variable retains its value across function calls staticVar++; printf("Static variable value: %d\n", staticVar); } int main() { // Example of auto storage class auto int autoVar = 5; // Example of register storage class register int registerVar = 7; staticDemo(); // Prints 1 staticDemo(); // Prints 2 return 0; }
Part C
(10 Marks)
Note: The answers provided below serve as a structured summary. For 10-mark questions, please expand on the context, elaborate on definitions to ensure the answer is comprehensive enough for the full marks.
16 Explain the function of various components of a computer with its block diagram.

The computer organization consists of Input, CPU (Control Unit, ALU, Internal Memory), Output, and Storage.

Data Path
Control Path
Input Unit Keyboard, Mouse
CPU
Control Unit
ALU
Internal Memory
Main Memory (RAM)
Secondary Storage HDD / SSD
Output Unit Monitor, Printer
Functions of Various Components

1. Input Unit

  • This unit accepts data and instructions from the outside world.
  • It converts these instructions and data into a form (binary form) that the computer can understand.
  • It supplies the converted data to the computer system for further processing.
  • Examples: Keyboards, Mouse, Joystick, OCR, MICR, etc.

2. Central Processing Unit (CPU)

The CPU is the brain of the computer. It performs all calculations, decisions, and controls/coordinates all units. It is subdivided into:

(i) Control Unit:

  • It instructs the computer how to carry out program instructions.
  • It directs the flow of data between memory and the Arithmetic Logical Unit (ALU).
  • It controls the flow of data from the input unit to the storage unit and from the storage unit to the output unit.
  • It fetches instructions from memory, decodes them, and sets up instruction execution.

(ii) Arithmetic and Logical Unit (ALU):

  • It performs all arithmetic operations (addition, subtraction, multiplication).
  • It performs logical operations (comparisons like less than, greater than).
  • It executes the calculations and takes decisions based on instructions provided.

(iii) Memory Unit:

  • It stores data temporarily and monitors external requests. It holds data for processing.
  • Primary Storage (Main Memory/RAM): Used to hold the program currently being executed, data received from the input unit, and intermediate results. It is fast but volatile.
  • Secondary Storage (Auxiliary Storage): Used to store several programs, documents, and databases permanently. Examples include Hard disks, Floppy disks, and Tapes.

3. Output Unit

  • Devices used to get the response or result of a process from the computer.
  • It provides the information and results of a computation to the outside world.
  • It converts the binary data produced by the computer into a form that users can understand (human-readable form).
  • Examples: Visual Display Units (VDU/Monitor), Printers, and Plotters.
(Source: 1_Introduction.pdf, Pages 8-11)
17 Explain decision making and branching statements with examples.

‘C’ language provides conditional statements to execute code blocks based on whether a condition is true or false.

1. The if Statement

The simplest form. The block executes only if the condition is true.

if(condition) { // True statements }
2. The if...else Statement

Executes one block if true, and a different block if false.

if(i < j) { big = j; } else { big = i; }
3. Nested if...else Statement

An if...else statement inside another if...else statement.

if(condition1) { if(condition2) { // executed if both true } }
4. The if...else Ladder

Used to check a series of conditions sequentially.

if (mark > 90) grade='S'; else if (mark > 80) grade='A'; else if (mark > 70) grade='B'; else grade='F';
5. The switch Statement

A multiway branch statement acting as a substitute for long if...else ladders. It dispatches execution based on the value of a variable.

switch(op) { case '+': c = a + b; break; case '-': c = a - b; break; default: printf("Invalid"); }
18 Discuss declaration, initialization and representation of array of strings. Write a program to sort an array of integers {67,98,23,54,9}.
Array of Strings
  • Definition: In C, an array of strings is a two-dimensional array of character types. Each string is terminated with a null character \0.
  • Declaration Syntax: char name[rows][columns];
  • Initialization:
    char arr[3][15] = {"sivashankari", "sivaranjeni", "kiruthika"};
  • Representation: Stored in contiguous memory. arr[0] accesses the first string.

Program to Sort Integers {67, 98, 23, 54, 9}:

#include <stdio.h> void bubbleSort(int a[], int n) { int i, j, temp; for (i = 1; i <= n; i++) { for(j = 0; j <= n - 1 - i; j++) { if(a[j] > a[j+1]) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } } int main() { int a[] = {67, 98, 23, 54, 9}; int n = 5; int i; printf("\nOriginal array: "); for(i = 0; i < n; i++) { printf("%d ", a[i]); } // Calling the function bubbleSort(a, n); printf("\nSorted array: "); for(i = 0; i < n; i++) { printf("%d ", a[i]); } return 0; }