0% found this document useful (0 votes)
1 views19 pages

ITPS SP Solved

Introduction of problem solving

Uploaded by

mk8972653191
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views19 pages

ITPS SP Solved

Introduction of problem solving

Uploaded by

mk8972653191
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

End Semester Examination

Sample Paper (Solved)

Semester : 1st

Subject : Introduction to Problem Solving

Compiled by Subhayu

Q.No. Statement CO mapping

Section A

5 x 2 = 10 marks

es
1 What is the purpose of header files in C? Give few examples of header
at CO2
files.
pd
Ans : Header files contain function prototypes, global variable
declarations, and data type definitions, enabling the compiler to check
code consistency across multiple source files.
U

1. Code Reusability: By including header files, you can share


common functions, constants, and data structures between
U

different source files, avoiding code duplication.


2. Modularity: They help in organizing code into logical units, where
C

the interface (declared in headers) is separated from the


implementation (in source files).
3. Predefined Libraries: Standard library header files provide
essential functionality like input/output, memory management,
and string manipulation. For example, <stdio.h>, <stdlib.h>,
<math.h>.

Examples of common header files:

● <stdio.h> — for input/output functions.


● <stdlib.h> — for memory allocation, process control, and
conversions.
● <math.h> — for mathematical functions.
● <string.h> — for string manipulation functions.
2 Define a one-dimensional array and provide an example of its CO2
declaration in C.

Ans : A one-dimensional array in C is a collection of elements of the


same data type, stored in contiguous memory locations, and accessed
using a single index.

Syntax: data_type array_name[array_size];

Example:

int numbers[5];

In this example, numbers is an integer array that can hold 5 integer


values, indexed from 0 to 4.

es
3 What is the difference between a structure and a union in C? CO3
at
Ans : The difference between a structure and a union in C is shown
below -
pd
U
U
C

4 What does the malloc function do in dynamic memory allocation? CO1

Ans : The malloc function in C is used to allocate a specified amount of


memory at runtime on the heap and returns a pointer to the beginning of
the allocated memory block.

Syntax:

void* malloc(size_t size);


● It allocates size bytes of memory and returns a pointer to the
allocated memory.
● If the allocation fails, it returns NULL.
● The memory is not initialized, so it contains garbage values
initially.

Example:

int* ptr = (int*) malloc(10 * sizeof(int)); // Allocates memory for 10


integers

In this example, malloc allocates enough memory to store 10 integers,


and ptr points to the allocated block.

5 If the memory address of a variable P is 2004, determine the output of CO1


the following code :

es
#include<stdio.h>

int main() {
at
pd
int P=200;

printf(“%d”,P);
U

printf(“%d”,&P);
U

return 0;
C

Ans : The first printf prints the value of P (which is 200).

The second printf prints the memory address of P (which is 2004).

Section B

4 x 5 = 20 marks

6 Describe the process of type conversion in C with suitable examples. CO3


Ans : Type conversion in C refers to the process of converting one data
type to another. There are two main types of type conversion:

1. Implicit Type Conversion (Automatic Conversion):

● Definition: The compiler automatically converts a lower data type


to a higher data type when needed.
● Example: If you assign an integer to a float variable, the integer is
automatically converted to a float.

Example:

int a = 10;

float b = a; // Implicit conversion from int to float

printf("%f", b); // Output: 10.000000

es
In this case, the integer a is implicitly converted to a float when assigned
to b. at
2. Explicit Type Conversion (Type Casting):
pd
● Definition: The programmer manually converts a variable from
one type to another using type casting.
U

● Syntax: (target_type) variable_name


● Example: If you want to convert a float to an integer, you use
explicit type casting.
U

Example:
C

float x = 9.7;

int y = (int) x; // Explicit conversion from float to int

printf("%d", y); // Output: 9

Here, x is explicitly cast to an integer, truncating the decimal part and


resulting in the value 9.

7 Explain the concept of recursion in functions with an example. CO4

Ans : Recursion in Functions


Recursion is a process where a function calls itself to solve a problem. It
consists of two main parts:

1. Base Case: A condition that terminates the recursion.


2. Recursive Case: A call to the same function with a modified
argument, moving the problem closer to the base case.

Example: Factorial of a Number

Factorial of n is the product of all positive integers from 1 to n. For n = 0,


the factorial is 1.

Recursive function for factorial:

#include <stdio.h>

int factorial(int n) {

es
if (n == 0) // Base case

return 1;
at
pd
else // Recursive case

return n * factorial(n - 1);


U

}
U

int main() {
C

int num = 5;

printf("Factorial of %d is %d\n", num, factorial(num));

return 0;

8 Describe the process of dynamic memory allocation using calloc and CO2
realloc.

Ans :

1. calloc (Contiguous Allocation)


Purpose: Allocates memory for an array of specified elements and
initializes all the memory to zero.

Syntax:
void* calloc(size_t num_elements, size_t size_of_element);

Example:
int* arr = (int*) calloc(5, sizeof(int)); // Allocates memory for 5 integers,
initializes to 0

● Here, calloc allocates space for 5 integers, and initializes all


elements to 0.

2. realloc (Reallocation)

es
Purpose: Resizes previously allocated memory block to a new size.

Syntax: void* realloc(void* ptr, size_t new_size);


at
Example:
pd
int* arr = (int*) malloc(5 * sizeof(int)); // Initially allocate memory for 5
integers
U

arr = (int*) realloc(arr, 10 * sizeof(int)); // Resize the array to hold 10


integers
U

● realloc adjusts the size of the previously allocated memory. If the


new size is larger, additional memory is added; if smaller, the
C

excess is freed.

9 Discuss the advantages of using structures in C programming with CO5


examples.

Ans : Advantages of Using Structures in C Programming

1. Organizing Related Data: Structures allow you to group different


data types under a single name, making it easier to represent
complex data.

Example:
struct Student {
char name[50];

int age;

float marks;

};

This groups a student's name, age, and marks together into a single unit,
rather than having them as separate variables.

2. Improved Code Readability:


○ By using structures, you can give meaningful names to
groups of related data, improving code clarity.

Example:
struct Employee {

es
int id;

char name[50];
at
pd
float salary;

};
U

struct Employee emp1;


U

emp1.id = 101;
C

emp1.salary = 50000;

This is clearer than using individual variables like emp_id, emp_name,


etc., since emp1 directly represents an employee.

3. Memory Efficiency: Structures allow for better memory


organization. Related data is stored contiguously, which can
optimize memory access.
4. Ease of Maintenance and Scalability: When adding new data
fields or changing the structure of data, it’s easier to manage
changes in one place (the structure definition) than in multiple
locations where the variables are used.

Example:
struct Car {
char make[30];

int year;

float price;

};

If we later need to add a color field, you only need to modify the structure
definition.

5. Passing Complex Data: Structures can be used to pass multiple


related data to functions, improving modularity and avoiding the
need to pass many individual variables.

Example:
void display(struct Student s) {

es
printf("Name: %s\n", s.name);

printf("Age: %d\n", s.age);


at
pd
printf("Marks: %.2f\n", s.marks);

}
U

Section C
U

3 x 10 = 30 marks
C

10 What is the advantage of using switch case? Using switch case, develop CO2
a menu driven program to input 2 numbers and produce the following
outputs :

(a) If user chooses 1, it gives the sum of the numbers

(b) If user chooses 2, it gives their difference

(c) If user chooses 3, it displays the greater number between the two.

(d) If user chooses 4, it swaps the numbers.

(e) Otherwise it prompts the user to enter a valid number.


Ans : Advantages of Using switch-case

1. Readability and Clarity:


○ The switch-case structure is more readable and cleaner
than multiple if-else statements when you have a set of
conditions based on a single variable (like menu
options).
2. Efficiency:
○ For a large number of possible cases, switch-case is
often more efficient than if-else because it can use jump
tables to directly go to the appropriate case, especially
when there are many choices.
3. Easier Maintenance:
○ It's easier to modify or add new cases in a switch-case
block without affecting other conditions, unlike in if-else
chains.

es
Below is a menu-driven program that takes two numbers as input and
performs various operations based on the user's choice using a
at
switch-case statement:
pd
#include <stdio.h>

int main() {
U

int num1, num2, choice;


U

// Input two numbers from the user


C

printf("Enter first number: ");

scanf("%d", &num1);

printf("Enter second number: ");

scanf("%d", &num2);

// Display menu

printf("\nMenu:\n");

printf("1. Sum of the numbers\n");


printf("2. Difference of the numbers\n");

printf("3. Greater number between the two\n");

printf("4. Swap the numbers\n");

printf("Enter your choice: ");

scanf("%d", &choice);

// Switch-case to perform the corresponding operation

switch(choice) {

case 1:

es
printf("Sum: %d\n", num1 + num2);

break; at
case 2:
pd
printf("Difference: %d\n", num1 - num2);
U

break;

case 3:
U

if (num1 > num2)


C

printf("Greater number: %d\n", num1);

else

printf("Greater number: %d\n", num2);

break;

case 4:

// Swapping the numbers

int temp = num1;


num1 = num2;

num2 = temp;

printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);

break;

default:

printf("Invalid choice! Please enter a valid number.\n");

break;

return 0;

es
}

11
at
What do you mean by multidimensional arrays? Write a program to CO3
show addition of two matrices using the concept of 2D arrays.
pd
Ans : A multidimensional array is an array of arrays, where each element
can be accessed using more than one index. The most common type is
U

the 2D array, which can be thought of as a table (rows and columns). In


general, a multidimensional array in C is declared by specifying the
U

number of elements in each dimension.


C

For example, a 2D array is declared as:

data_type array_name[rows][columns];

Example: Addition of Two Matrices Using 2D Arrays

#include <stdio.h>

int main() {

int m, n;

// Input the number of rows and columns for the matrices


printf("Enter the number of rows: ");

scanf("%d", &m);

printf("Enter the number of columns: ");

scanf("%d", &n);

// Declare two 2D arrays (matrices)

int matrix1[m][n], matrix2[m][n], sum[m][n];

// Input elements for the first matrix

printf("\nEnter elements of the first matrix:\n");

for (int i = 0; i < m; i++) {

es
for (int j = 0; j < n; j++) {

printf("Enter element at position [%d][%d]: ", i, j);


at
scanf("%d", &matrix1[i][j]);
pd
}
U

// Input elements for the second matrix


U

printf("\nEnter elements of the second matrix:\n");


C

for (int i = 0; i < m; i++) {

for (int j = 0; j < n; j++) {

printf("Enter element at position [%d][%d]: ", i, j);

scanf("%d", &matrix2[i][j]);

// Add the two matrices and store the result in the sum matrix
for (int i = 0; i < m; i++) {

for (int j = 0; j < n; j++) {

sum[i][j] = matrix1[i][j] + matrix2[i][j];

// Display the sum matrix

printf("\nSum of the two matrices is:\n");

for (int i = 0; i < m; i++) {

for (int j = 0; j < n; j++) {

es
printf("%d ", sum[i][j]);

} at
printf("\n");
pd
}
U

return 0;

}
U
C

Optional Question of Section C

12 Write a program that demonstrates pointer arithmetic operations CO4


(increment, decrement, addition, subtraction) on an array of integers.
The program should prompt the user to enter the array size and
elements, then display the results of the pointer arithmetic operations.
Also give the flowchart.

Ans : Program to Demonstrate Pointer Arithmetic on an Array of Integers

#include <stdio.h>
int main() {

int n;

// Input the number of elements in the array

printf("Enter the size of the array: ");

scanf("%d", &n);

// Declare an array of integers

int arr[n];

es
int *ptr = arr; // Pointer pointing to the first element of the array

at
// Input the elements of the array
pd
printf("Enter %d elements for the array:\n", n);
U

for (int i = 0; i < n; i++) {

printf("Element %d: ", i + 1);


U

scanf("%d", &arr[i]);
C

// Display the array using pointer arithmetic

printf("\nArray elements using pointer arithmetic:\n");

for (int i = 0; i < n; i++) {

printf("Element %d: %d (ptr + %d) = %d\n", i + 1, *ptr, i, *(ptr + i));

}
// Perform pointer arithmetic operations

printf("\nPointer Arithmetic Results:\n");

// Increment the pointer

ptr++; // Move the pointer to the next element

printf("After incrementing, ptr points to value: %d\n", *ptr);

// Decrement the pointer

es
ptr--; // Move the pointer back to the previous element

printf("After decrementing, ptr points to value: %d\n", *ptr);


at
pd
// Addition of pointer and integer (ptr + index)
U

int index = 2; // Example index to add

printf("Element at index %d using ptr + index: %d\n", index, *(ptr +


U

index));
C

// Subtraction of pointers (ptr2 - ptr1 gives the number of elements


between them)

int *ptr2 = &arr[n-1]; // Pointer to the last element

printf("Number of elements between ptr and ptr2: %ld\n", ptr2 - ptr);

return 0;

}
es
at
pd
U
U

Flowchart for the above code


C

OR

13 Define a structure to store book information (title, author, price). Write a CO4
program that creates an array of such structures, prompts the user to
enter the data, and then displays the stored information. Support your
code with a suitable algorithm.

Ans : Program to Store and Display Book Information

#include <stdio.h>
// Define a structure to store book information

struct Book {

char title[100];

char author[100];

float price;

};

int main() {

int n;

es
// Prompt the user to enter the number of books
at
printf("Enter the number of books: ");
pd
scanf("%d", &n);
U

// Declare an array of structures to hold 'n' books


U

struct Book books[n];


C

// Input data for each book

for (int i = 0; i < n; i++) {

printf("\nEnter information for book %d:\n", i + 1);

printf("Enter title: ");

getchar(); // Clear newline character left by previous scanf

fgets(books[i].title, sizeof(books[i].title), stdin);


printf("Enter author: ");

fgets(books[i].author, sizeof(books[i].author), stdin);

printf("Enter price: ");

scanf("%f", &books[i].price);

// Display the stored information

es
printf("\nStored Book Information:\n");

for (int i = 0; i < n; i++) { at


printf("\nBook %d\n", i + 1);
pd
printf("Title: %s", books[i].title);
U

printf("Author: %s", books[i].author);

printf("Price: %.2f\n", books[i].price);


U

}
C

return 0;

Algorithm:

1. Start
2. Input the number of books n.
3. Define a structure Book with title, author, and price.
4. Create an array books[] of size n to store the book information.
5. For i = 0 to n-1, repeat the following:
○ Prompt the user to input the title, author, and price of the
book.
○ Store the data in books[i].title, books[i].author, and
books[i].price.
6. For i = 0 to n-1, repeat the following:
○ Display the title, author, and price of the book stored in
books[i].
7. End

es
at
pd
U
U
C

You might also like