PYQ With Solution-5
PYQ With Solution-5
PPS (ESC-103)
End Semester Paper December 2019
Part-B
2. (a) Draw a flowchart for finding out the greatest between three numbers. (5 mark)
(b) What is a function? What is the difference between call by value and call by reference method
of calling a function? Explain with proper example. (10 mark)
Definition:
Data types in C are classifications that specify the type of data a variable can hold, defining
its size, range, and operations that can be performed on it. They help ensure proper memory
allocation and operations.
Detailed Answer:
In C, data types are broadly categorized into three types:
2. Define Pointer.
Definition:
A pointer is a variable in C that stores the memory address of another variable. It enables
dynamic memory allocation and efficient handling of arrays, strings, and functions.
Detailed Answer:
Pointers are declared using the * operator. For example:
int x = 10;
int *p = &x;
Here, p is a pointer storing the address of x. The & operator retrieves the address of a variable,
and the * operator (dereferencing) accesses the value at that address. Pointers are widely used
for:
Definition:
Sorting is the process of arranging data in a specific order (ascending or descending) to
enable efficient searching and analysis.
Detailed Answer:
Two common sorting techniques are:
1. Bubble Sort: Repeatedly compares adjacent elements and swaps them if they are in
the wrong order.
o Time Complexity: O(n2)O(n^2)O(n2)
o Space Complexity: O(1)O(1)O(1)
2. Insertion Sort: Builds the sorted list one element at a time by comparing and
inserting elements into their correct position.
o Time Complexity: O(n2)O(n^2)O(n2)
o Space Complexity: O(1)O(1)O(1)
Definition:
A string in C is a one-dimensional array of characters terminated by a null character \0.
Strings are used to store and manipulate text.
Strings are declared using the char keyword. Syntax:
char str[size];
For example:
Here, "Hello" is stored as {'H', 'e', 'l', 'l', 'o', '\0'}. Common string functions
include:
Algorithm Flowchart
A step-by-step procedure for solving a
A graphical representation of an algorithm.
problem.
Algorithm Flowchart
Uses symbols like ovals, rectangles, and
Written in natural language or pseudocode.
arrows.
Focuses on the logic of the solution. Focuses on the process flow.
Example: Steps to calculate factorial. Example: Flowchart to find factorial.
No visual representation. Easy to visualize and understand.
Definition:
A function prototype is a declaration of a function that specifies its name, return type, and
parameters but not its body.
Detailed Answer:
The prototype informs the compiler about the function details before its actual definition.
Syntax:
return_type function_name(parameter_list);
Example:
Here, add is a function that takes two integers and returns an integer. Prototypes are crucial
for type-checking during compilation.
Code:
main()
{
int x = 5, *p;
p = &x;
printf("%d", *p);
getch();
}
Answer:
Detailed Answer:
Syntax:
char str[50];
gets(str); // Reads input into str
puts(str); // Prints str with a newline
Example:
char name[20];
gets(name); // Input: "Alice"
puts(name); // Output: "Alice"
Note: gets() is unsafe due to buffer overflow risks and is replaced by fgets() in modern C
standards.
9. Define recursion.
Definition:
Recursion is a programming technique where a function calls itself to solve smaller instances
of a problem.
A recursive function has a base case (termination condition) and a recursive step. Syntax:
return_type function_name(parameters) {
if (base_condition)
return base_case_result;
else
return recursive_call;
}
Example (Factorial):
int factorial(int n) {
if (n == 0) return 1;
else return n * factorial(n - 1);
}
10. Write the syntax of fopen and fclose commands in C file system.
Definition:
fopen: Opens a file in a specified mode.
fclose: Closes the opened file and releases resources.
Detailed Answer:
Syntax:
"r": Read
"w": Write
"a": Append
Example:
Explanation:
The memory hierarchy in a computer system is organized into layers to balance speed, cost,
and capacity. The hierarchy ranges from the fastest and most expensive memory (CPU
registers) to the slowest but most affordable and voluminous storage (secondary storage).
Diagram:
1. (b) Explain all types of loops available in C with their syntax. Write a program to
generate a table of a number “n” being entered by the user. (10 marks)
Explanation of Loops in C:
2. while Loop: Used when the condition is evaluated before executing the loop body.
o Syntax:
while(condition) {
// Statements
}
3. do-while Loop: Executes at least once since the condition is checked after execution.
o Syntax:
do {
// Statements
} while(condition);
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Table of %d:\n", n);
for (int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", n, i, n * i);
}
return 0;
}
2. (a) Draw a flowchart for finding out the greatest among three numbers. (5 marks)
Flowchart:
Definition of Function:
A function is a reusable block of code that performs a specific task. Functions simplify code,
allow modularity, and improve readability.
Examples:
Call by Value:
void func(int a) {
a = 10;
}
int main() {
int x = 5;
func(x); // x is still 5
}
Call by Reference:
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
3. (b) WAP to calculate the length of a string without using inbuilt functions. (10 marks)
#include <stdio.h>
int stringLength(char str[]) {
int count = 0;
while (str[count] != '\0') {
count++;
}
return count;
}
int main() {
char str[100];
printf("Enter a string: ");
gets(str);
printf("Length of the string is %d\n", stringLength(str));
return 0;
}
#include <stdio.h>
int main() {
int arr[100], n, search, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Enter the element to search: ");
scanf("%d", &search);
for (i = 0; i < n; i++) {
if (arr[i] == search) {
printf("Element found at index %d\n", i);
return 0;
}
}
printf("Element not found.\n");
return 0;
}
4. (b) Define array. WAP to find the sum of two matrices in a third matrix. (10 marks)
Definition:
An array is a collection of elements of the same data type stored in contiguous memory
locations.
Program:
#include <stdio.h>
int main() {
int A[3][3], B[3][3], C[3][3], i, j;
printf("Enter elements of matrix A:\n");
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
scanf("%d", &A[i][j]);
printf("Enter elements of matrix B:\n");
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
scanf("%d", &B[i][j]);
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
C[i][j] = A[i][j] + B[i][j];
printf("Sum of matrices:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++)
printf("%d ", C[i][j]);
printf("\n");
}
return 0;
}
#include <stdio.h>
int main() {
FILE *source, *dest;
char ch;
source = fopen("source.txt", "r");
dest = fopen("dest.txt", "w");
if (source == NULL || dest == NULL) {
printf("Error opening file.\n");
return 1;
}
while ((ch = fgetc(source)) != EOF) {
fputc(ch, dest);
}
fclose(source);
fclose(dest);
printf("File copied successfully.\n");
return 0;
}
5. (b) What is an operating system? Write functions and explain memory management.
(10 marks)
Definition:
An operating system (OS) is software that acts as an interface between the user and the
hardware, managing resources and providing services.
Functions:
6. Short Notes:
Compiler: Translates the entire source code to machine code before execution.
Example: C compiler.
Interpreter: Translates and executes code line by line. Example: Python interpreter.
(b) Strings:
Strings are arrays of characters terminated by \0. They allow representation and manipulation
of text in C. Example: "Hello".