ITPS SP Solved
ITPS SP Solved
Semester : 1st
Compiled by Subhayu
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
Example:
int numbers[5];
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
Syntax:
Example:
es
#include<stdio.h>
int main() {
at
pd
int P=200;
printf(“%d”,P);
U
printf(“%d”,&P);
U
return 0;
C
Section B
4 x 5 = 20 marks
Example:
int a = 10;
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
Example:
C
float x = 9.7;
#include <stdio.h>
int factorial(int n) {
es
if (n == 0) // Base case
return 1;
at
pd
else // Recursive case
}
U
int main() {
C
int num = 5;
return 0;
8 Describe the process of dynamic memory allocation using calloc and CO2
realloc.
Ans :
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
2. realloc (Reallocation)
es
Purpose: Resizes previously allocated memory block to a new size.
excess is freed.
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.
Example:
struct Employee {
es
int id;
char name[50];
at
pd
float salary;
};
U
emp1.id = 101;
C
emp1.salary = 50000;
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.
Example:
void display(struct Student s) {
es
printf("Name: %s\n", s.name);
}
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 :
(c) If user chooses 3, it displays the greater number between the two.
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
scanf("%d", &num1);
scanf("%d", &num2);
// Display menu
printf("\nMenu:\n");
scanf("%d", &choice);
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
else
break;
case 4:
num2 = temp;
break;
default:
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
data_type array_name[rows][columns];
#include <stdio.h>
int main() {
int m, n;
scanf("%d", &m);
scanf("%d", &n);
es
for (int j = 0; j < n; 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++) {
es
printf("%d ", sum[i][j]);
} at
printf("\n");
pd
}
U
return 0;
}
U
C
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
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
scanf("%d", &arr[i]);
C
}
// Perform pointer arithmetic operations
es
ptr--; // Move the pointer back to the previous element
index));
C
return 0;
}
es
at
pd
U
U
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.
#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
scanf("%f", &books[i].price);
es
printf("\nStored Book Information:\n");
}
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