PYQ with Solution-2
PYQ with Solution-2
PYQ with Solution-2
PPS (ESC-103)
End Semester Paper April 2022
Part-B
3. What is Recursion? How it is different from iteration? WAP to print the Fibonacci series
up to n terms using recursion. (15 mark)
5. (a) Differentiate between array and structure. Explain array of structure with the help of a
program. (10 mark)
(b) WAP to count the number of vowels in a string without using inbuilt functions. (5
mark)
6. Define flowchart. Draw a flowchart to compute factorial for a given number N, where N
is a non-negative integer. Write an algorithm for the same problem. (15 mark)
Solutions
Solutions for Part-A
1. Define linked list
A linked list is a data structure that consists of a sequence of elements, where each
element, called a node, contains data and a reference (or a link) to the next node in the
sequence. Unlike arrays, linked lists do not store elements in contiguous memory
locations. This structure allows for efficient insertions and deletions since they don't
require shifting elements, unlike arrays. Linked lists are mainly used when the size of the
data structure needs to vary dynamically or when memory utilization is crucial. There are
several types of linked lists, such as singly linked lists (where each node points to the next
node only), doubly linked lists (where nodes have links to both the previous and next
nodes), and circular linked lists (where the last node points back to the first node).
3. What is “\0”?
The \0 character, also known as the null character, is used in C and C++ to mark the end
of a string. It has an ASCII value of zero and is essential in languages that use null-
terminated strings. The null character lets functions like strlen, strcpy, and others
recognize where a string terminates, thus preventing memory from being accessed beyond
the intended string boundaries. Without \0, the program may continue reading adjacent
memory, leading to errors.
Question 1
In C, loops allow for repetitive execution of a block of code. There are three primary types of
loops: for, while, and do-while.
1. For Loop: Used when the number of iterations is known. It has three parts within the
loop: initialization, condition, and increment/decrement. Syntax:
Example:
2. While Loop: Checks the condition before executing the loop. It’s useful when the
number of iterations isn’t predetermined. Syntax:
while(condition) {
// Statements to execute
}
Example:
int i = 0;
while(i < 5) {
printf("%d ", i);
i++;
}
3. Do-While Loop: Executes the loop body first and checks the condition afterward,
ensuring the loop runs at least once. Syntax:
do {
// Statements to execute
} while(condition);
Example:
int i = 0;
do {
printf("%d ", i);
i++;
} while(i < 5);
(b) Write a program to calculate the area and circumference of a circle using call by
reference. (5 marks)
Here’s a C program that calculates the area and circumference of a circle by passing values
using pointers.
#include <stdio.h>
#define PI 3.14159
int main() {
float radius, area, circumference;
printf("Enter radius of circle: ");
scanf("%f", &radius);
calculateCircle(radius, &area, &circumference);
printf("Area: %.2f\nCircumference: %.2f\n", area, circumference);
return 0;
}
Question 2
An Operating System (OS) is system software that manages hardware and software resources
and provides services for computer programs. Functions include:
(b) Write a program to print the upper and lower triangular matrix along with the sum
of their elements. (10 marks)
#include <stdio.h>
Question 3
What is Recursion? How is it different from iteration? Write a program to print the
Fibonacci series up to n terms using recursion. (15 marks)
#include <stdio.h>
int fibonacci(int n) {
if(n <= 1)
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
Question 4
(a) Write a program to copy the elements of one file to another. (5 marks)
#include <stdio.h>
fclose(source);
fclose(dest);
}
int main() {
copyFile("source.txt", "destination.txt");
return 0;
}
(b) Explain each step for sorting the given array using selection sort. (10 marks)
Selection sort works by repeatedly finding the minimum element from the unsorted portion
and moving it to the sorted portion. For example, given [5, 10, 15, 20, 18, 12, 8, 1,
30]:
1. Find the minimum in the array [1] and swap with the first element.
2. Continue to find the minimum in the unsorted portion and move to the sorted portion.
3. Repeat until the array is fully sorted.
Question 5
(a) Differentiate between array and structure. Explain array of structures with a
program. (10 marks)
#include <stdio.h>
#include <string.h>
struct Student {
int id;
char name[20];
float grade;
};
int main() {
struct Student students[3];
students[0].id = 1;
strcpy(students[0].name, "Alice");
students[0].grade = 85.5;
(b) Write a program to count the number of vowels in a string without using inbuilt
functions. (5 marks)
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
gets(str);
printf("Number of vowels: %d\n", countVowels(str));
return 0;
}
Question 6
Define flowchart. Draw a flowchart to compute factorial for a given number N, where N
is a non-negative integer. Write an algorithm for the same problem. (15 marks)
Below is the flowchart to compute the factorial of a given non-negative integer NNN:
+-----------------+
| Start |
+-----------------+
|
v
+-----------------+
| Input N |
+-----------------+
|
v
+-----------------+
| fact <- 1 |
+-----------------+
|
v
+----------------------------+
| i <- 1 |
| While (i <= N) do |
| fact <- fact * i |
| i <- i + 1 |
+----------------------------+
|
v
+-----------------+
| Output fact |
+-----------------+
|
v
+-----------------+
| End |
+-----------------+
#include <stdio.h>
int main() {
int N, fact = 1;
This flowchart and algorithm are ideal for calculating factorials in iterative programs,
providing clear visual and step-by-step guides for understanding factorial computation.