Pps - Summer 2024
Pps - Summer 2024
/Seat No_______________
Z
(b) Illustrate an Algorithm to check whether the given year is leap year or not. 04
AA
(c) Define Flow chart, List the rules for designing a Flow chart and Rephrase the 07
merits and demerits of flow chart.
two variables.
U
Q.3 (a) Contrast the Entry Control loop and Exit Control loop. 03
(b) Construct a C program to find the largest number amongst three integers 04
R
entered.
(c) Construct a C program to check if entered integer is a prime number or note. 07
OR
G
Q.3 (a) Debug the following codes and show the output for this: 03
For ( i=0 ; i<=50 ; i++ );
{
JA
Z
Q.5 (a) Write True / False against following statements: 03
AA
1) Pointer is a variable which holds address of location where value of
other variable is stored.
2) Pointer holds the address in form of float or integer.
3) The address of a variable can be stored in two or more pointers.
(b) List the categories of User Defined Functions and Discuss any one of these. 04
AW
(c) Construct a C program which used a user define function that receives an 07
integer and returns square and cube of it and prints in main function.
OR
Q.5 (a) What are the arithmetic operators that are permitted on pointers? 03
(b) Show the use of Malloc, Calloc, Free and Realloc in context of Dynamic 04
memory allocation.
T
*************
U
R
G
JA
PPS-SUMMER-2024
Z
AA
AW
T
U
Input Devices:
R
• These devices are used to provide data and control signals to the computer.
• Examples: Keyboard, Mouse, Scanner, etc.
G
• The CPU is the brain of the computer, responsible for executing instructions.
• Control Unit (CU): It directs the operation of the processor and coordinates the
execution of instructions by controlling the flow of data between the CPU and other
components.
• Arithmetic and Logic Unit (ALU): Performs arithmetic operations (addition,
subtraction, etc.) and logical operations (comparisons, decision-making).
• Registers: Small, fast storage locations inside the CPU used to store intermediate
data and instructions.
JAGRUT AWAAZ 1
Z
format.
• Examples: Monitor, Printer, Speakers, etc.
AA
(b) Illustrate an Algorithm to check whether the given year is leap
year or not.
AW
Ans.
T
U
R
G
JA
JAGRUT AWAAZ 2
Z
represented by oval symbols labeled "Start" and "End."
2. Flow Direction: The flow of the chart should be from top to bottom or left to
AA
right.
3. Use Standard Symbols: Only use standard flowchart symbols for specific
purposes:
o Oval: Start/End
AW
o Rectangle: Process or action step
o Diamond: Decision point
o Parallelogram: Input/Output
T
4. Single Entry and Exit: Each flowchart symbol should have only one entry and one
exit, except decision symbols which can have two exits.
R
5. Clarity and Simplicity: Keep the flowchart simple and easy to understand. Avoid
complex and overlapping lines.
G
6. Consistent Level of Detail: Maintain a consistent level of detail. Each step should
be simple and represent a meaningful action.
JA
7. Use Connector Symbols: For complex flowcharts, use connector symbols (circle)
to indicate jumps within the flowchart.
8. Label Decisions and Arrows: Label decision branches (Yes/No, True/False) and
arrow directions when necessary for clarity.
Merits of Flowcharts:
1. Clarity of Process: Flowcharts visually represent a process, making it easier to
understand and follow.
JAGRUT AWAAZ 3
Z
breaking them into simpler steps.
AA
Demerits of Flowcharts:
1. Time-Consuming: Designing a detailed flowchart for complex processes can be
time-consuming. AW
2. Complexity in Large Systems: For large and complicated systems, flowcharts can
become overly complex and hard to follow.
3. Not Suitable for All Types of Problems: Flowcharts may not be suitable for
highly detailed or algorithmic problems where pseudocode or other forms of
documentation are more efficient.
T
5. Limited Representation: Flowcharts may not capture detailed logic and can
oversimplify certain aspects of processes.
R
G
Here is the list of rules and regulations to follow when defining identifiers in C:
1. The identifier cannot begin with a digit/ numeric digit.
2. The initial letter/ first element of an identifier should always either be an underscore
or an alphabet.
3. The letters that follow the first could be a character, a number, or an underscore in
the identifier.
JAGRUT AWAAZ 4
Z
use an in-built keyword as an identifier as well.
10. And lastly, identifiers must be short yet descriptive and also simple to understand
AA
(b) Illustrate the basic structure of a C Programs and highlight usual
sections of main function.
AW
Ans.
#include <stdio.h> // Preprocessor directive
int main() {
// Local variable declarations
R
G
// Return statement
return 0;
}
Usual Sections of the main Function:
JAGRUT AWAAZ 5
Z
o Variables and functions declared here are accessible throughout the program,
including the main function and other functions.
AA
o Example: int global_var = 10;
3. Function Definition (main): AW
o This is the starting point of execution. The basic syntax of the main function is:
int main() {
// Code
}
T
o The main function must return an integer (typically 0 for successful execution).
4. Local Variable Declarations:
U
o Local variables are declared within the main function and are only accessible within
R
this function.
o Example:
G
int a = 5;
5. Statements and Expressions (Logic):
JA
o This is the core part of the program where the logic, computations, and statements
are executed.
o Example:
printf("Sum is: %d\n", a + b);
6. Return Statement:
JAGRUT AWAAZ 6
Z
#include <stdio.h>
AA
int main() {
int ndays, y, m, d; // Declare variables for number of
days, years, months, and days
AW
// Prompt user for input number of days and store in
'ndays'
printf("Input no. of days: ");
scanf("%d", &ndays);
T
U
y = (int) ndays/365;
ndays = ndays-(365*y);
G
m = (int)ndays/30;
d = (int)ndays-(m*30);
JA
return 0;
JAGRUT AWAAZ 7
OR
(c) Construct a C program to swap the values of two integers with
use of only two variables.
Ans.
// C Program to Swap Two Numbers using a
Z
// Temporary Variable
#include <stdio.h>
AA
int main() {
int a = 5, b = 10, temp;
AW
// Swapping values of a and b
temp = a;
a = b;
T
b = temp;
U
R
}
JA
Q.3 (a) Contrast the Entry Control loop and Exit Control loop
Ans.
Entry Controlled Loop Entry Controlled Loop
Condition is evaluated before body of loop Condition is evaluated after body of loop is
is executed. executed.
JAGRUT AWAAZ 8
Z
Ans.
AA
#include <stdio.h> // Include standard input-output
library AW
int main() {
int num1, num2, num3; // Declare three integer
variables
int largest; // Variable to hold the largest
T
number
U
Z
execution
AA
}
#include <stdbool.h>
U
#include <stdio.h>
R
int main() {
G
int n = 29;
JA
int cnt = 0;
JAGRUT AWAAZ 10
Z
cnt++;
AA
}
// else it is prime
U
else
printf("%d is prime", n);
R
}
G
return 0;
JA
OR
Q.3 (a) Debug the following codes and show the output for this: For (
i=0 ; i<=50 ; i++ ); { printf( “\n %d” , i ) }
*consider header files and declaration of variable is done.
JAGRUT AWAAZ 11
Z
4. Missing semicolon after printf: The printf() statement is missing a semicolon (;) at
the end.
AA
Explanation of Fixes:
• Changed For to for: The correct C syntax for loops.
• Removed semicolon after for loop: The loop should have no semicolon after the
AW
parentheses, so the following block {} can be executed.
• Fixed the quotes in printf(): Replaced the curly quotes with straight quotes.
• Added missing semicolon after printf(): Every C statement needs to end with a
semicolon.
T
Ans.
A nested if-else statement is an if-else statement that is placed inside another if-else
R
statement. This is useful when you need to make multiple decisions based on different
conditions.
G
int main() {
int num;
Z
AA
// Input a number from the user
printf("Enter an integer: ");
scanf("%d", &num);
AW
// Outer if-else statement
if (num >= 0) {
// If the number is positive or zero, check
T
further
U
if (num % 2 == 0) {
printf("The number %d is even and
R
positive.\n", num);
} else {
G
}
} else {
// If the number is negative
printf("The number %d is negative.\n", num);
}
JAGRUT AWAAZ 13
Z
be checked.
AA
• Improves readability when logically related conditions are grouped together.
11 12 13 14 15…..
U
Ans.
R
#include <stdio.h>
G
int main() {
JA
return 0;
Z
}
AA
Q.4 (a) List the types of Array. AW
Ans.
Arrays in C are classified into different types based on the number of dimensions. Here
are the most common types of arrays:
1. One-Dimensional Array (1D Array):
A one-dimensional array stores a list of elements in a single row or a linear
T
o
form.
U
o Example:
int arr[5] = {10, 20, 30, 40, 50};
R
and columns.
o Example:
int arr[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
JAGRUT AWAAZ 15
Z
{1, 2, 3, 4},
AA
{5, 6, 7, 8},
{9, 10, 11, 12}
},
AW
{
{13, 14, 15, 16},
{17, 18, 19, 20},
{21, 22, 23, 24}
T
}
U
};
R
• Elements are accessed using three indices, like arr[1][2][3] (second block, third row,
G
fourth column).
(b) Summarize the methods for initialization of One-Dimensional
JA
array.
Ans.
In C, a one-dimensional array can be initialized in several ways. Here are the common
methods for initializing a one-dimensional array:
JAGRUT AWAAZ 16
2. Partial Initialization:
Z
• If you provide fewer values than the size of the array, the remaining elements are
initialized to 0.
AA
• Syntax:
data_type array_name[array_size] = {value1, value2};
AW
• Example:
int arr[5] = {10, 20}; // The remaining elements will be 0
• Example:
G
int arr[] = {10, 20, 30, 40, 50}; // Size is automatically set to 5
JA
Z
Ans.
#include <stdio.h>
AA
#define SIZE 3 // Define the size of the matrix
AW
void multiplyMatrices(int firstMatrix[SIZE][SIZE], int
secondMatrix[SIZE][SIZE], int result[SIZE][SIZE]) {
// Initialize the result matrix to 0
for (int i = 0; i < SIZE; i++) {
T
result[i][j] = 0;
R
}
}
G
JAGRUT AWAAZ 18
Z
}
AA
printf("\n");
}
}
AW
int main() {
int firstMatrix[SIZE][SIZE], secondMatrix[SIZE][SIZE],
result[SIZE][SIZE];
T
U
}
}
JAGRUT AWAAZ 19
Z
// Display the result
AA
printf("Result of matrix multiplication:\n");
printMatrix(result); AW
return 0;
}
OR
T
Q.4 (a) List the application of gets getchar and puts functions.
U
Ans.
R
1. gets Function:
• Purpose: Reads a string from standard input (stdin) until a newline character or EOF
G
is encountered.
Syntax:
JA
JAGRUT AWAAZ 20
Z
• Input Validation: Often employed in loops to validate or filter character
input.
AA
3. puts Function:
• Purpose: Writes a string to standard output (stdout) followed by a newline
character.
AW
• Syntax:
int puts(const char *str);
Applications:
• Displaying Strings: Commonly used for displaying messages, prompts, or any
T
• Simple Output: Ideal for printing simple strings without the need for format
specifiers.
R
on screen of a String.
JA
Ans.
1. Declaration of a String
To declare a string in C, you typically use a character array or a pointer to a character.
Here are two common methods:
a. Using a Character Array:
char str[100]; // Declares a string with a maximum size of 99 characters (plus
a null terminator)
JAGRUT AWAAZ 21
Z
You can also assign a string value after declaration using the strcpy function
AA
char str2[100];
strcpy(str2, "Hello, World!"); // Copy the string to the character array
3. Printing a String on the Screen
AW
To print a string on the screen, you can use the printf or puts function.
a. Using printf:
printf("%s\n", str1); // Prints the string using format specifier %s
b. Using puts:
puts(str1); // Prints the string followed by a newline character
T
U
• Team name:
G
Ans.
#include <stdio.h>
int main() {
// Create an instance of the Cricketer structure
struct Cricketer player;
Z
AA
// Input details for the cricketer
printf("Enter the name of the player: ");
fgets(player.name, sizeof(player.name), stdin); //
AW
Reading name (with spaces)
JAGRUT AWAAZ 23
return 0;
}
Z
1) Pointer is a variable which holds address of location where value of
AA
other variable is stored.
Ans. True
Explanation: A pointer is a variable that stores the memory address of another
variable.
AW
2) Pointer holds the address in form of float or integer.
Ans. False
Explanation: A pointer holds an address in a specific format (typically a memory
address), and its type is defined based on the type of variable it points to (e.g., int*, float*,
T
char*). The address itself is generally represented as an integer type (usually an unsigned
long integer), but pointers themselves are not classified as float or integer types.
U
Ans. True
Explanation: Multiple pointers can hold the address of the same variable, allowing them
G
(b) List the categories of User Defined Functions and Discuss any one
of these.
Ans.
Categories of User-Defined Functions
1. Based on Return Type:
o Functions with Return Type: Functions that return a value (e.g., int, float,
char).
JAGRUT AWAAZ 24
Z
o Custom Functions: Functions defined by the user to perform specific tasks.
AA
Discussion on Functions with Return Type
Functions with Return Type are the most common category of user-defined functions.
AW
These functions perform a specific task and return a value back to the caller. The return
type of the function defines the data type of the value that the function will return.
Structure of a Function with Return Type
The structure of a function with a return type typically includes:
• Return Type: The data type of the value being returned.
T
• Function Body: The block of code that defines what the function does.
Example of a Function with Return Type
G
Here's a simple example that illustrates how to define and use a function that calculates the
square of an integer:
JA
#include <stdio.h>
JAGRUT AWAAZ 25
int main() {
int number, result;
Z
// Call the square function and store the result
AA
result = square(number);
return 0;
}
T
U
main function.
G
Ans.
#include <stdio.h>
JA
JAGRUT AWAAZ 26
int main() {
int number, square, cube;
Z
// Call the function to calculate square and cube
AA
calculateSquareAndCube(number, &square, &cube);
return 0;
T
}
U
OR
R
Q.5 (a) What are the arithmetic operators that are permitted on
G
pointers?
Ans.
JA
JAGRUT AWAAZ 27
Z
• You can subtract an integer value from a pointer, which moves the pointer
backward by that number of elements.
AA
• You can also subtract one pointer from another if they point to elements of
the same array, which results in the difference in terms of the number of
elements between the two pointers.
AW
• Example:
ptr = arr + 3; // Now ptr points to the fourth element (40)
ptr = ptr - 1; // Now ptr points to the third element (30)
3. Increment (++):
T
• The increment operator moves the pointer to the next element in the array.
U
• Example:
int *ptr = arr; // ptr points to 10
R
4. Decrement (--):
• The decrement operator moves the pointer to the previous element in the
JA
array.
• Example:
ptr--; // Now ptr points back to 10
(b) Show the use of Malloc, Calloc, Free and Realloc in context of
Dynamic memory allocation.
Ans.
JAGRUT AWAAZ 28
Z
Example:
AA
#include <stdio.h>
#include <stdlib.h>
AW
int main() {
int *arr;
int n = 5;
T
if (arr == NULL) {
G
JAGRUT AWAAZ 29
return 0;
}
2. calloc
Z
The calloc function allocates memory for an array of specified elements and initializes
AA
all bits to zero. It is useful when you want to ensure that the allocated memory starts with
a known value.
Syntax:
void* calloc(size_t num, size_t size);
AW
Example:
#include <stdio.h>
#include <stdlib.h>
T
int main() {
U
int *arr;
R
int n = 5;
G
if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
JAGRUT AWAAZ 30
Z
AA
return 0;
}
3. free
AW
The free function is used to deallocate memory that was previously allocated using
malloc, calloc, or realloc. This prevents memory leaks by releasing the memory back to the
system.
Syntax:
T
4. realloc
JA
The realloc function is used to change the size of previously allocated memory. It can
increase or decrease the size of the memory block. If the new size is larger, the additional
memory is uninitialized. If it is smaller, the excess memory is freed.
Syntax:
void* realloc(void* ptr, size_t new_size);
Example:
#include <stdio.h>
JAGRUT AWAAZ 31
int main() {
int *arr;
int n = 5;
Z
AA
if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
AW
}
}
R
n = 10;
arr = (int*)realloc(arr, n * sizeof(int));
JA
if (arr == NULL) {
printf("Memory reallocation failed!\n");
return 1;
}
JAGRUT AWAAZ 32
Z
}
AA
// Free the allocated memory
free(arr);
AW
return 0;
}
T
Ans.
File handling in C is crucial for reading from and writing to files, enabling persistent data
R
storage. C provides several standard library functions for file operations, which are defined
in the stdio.h header file.
G
1. fopen
JA
JAGRUT AWAAZ 33
Z
Example:
FILE *file = fopen("example.txt", "r");
AA
if (file == NULL) {
perror("Error opening file");
}
AW
2. fclose
Purpose: Closes an opened file.
Syntax:
T
Parameters:
stream: A pointer to the file to be closed.
R
Example:
G
if (fclose(file) != 0) {
perror("Error closing file");
JA
3. fprintf
Purpose: Writes formatted data to a file.
Syntax:
int fprintf(FILE* stream, const char* format, ...);
JAGRUT AWAAZ 34
4. fscanf
Purpose: Reads formatted input from a file.
Z
Syntax:
AA
int fscanf(FILE* stream, const char* format, ...);
Parameters:
• stream: Pointer to the file from which data will be read.
AW
• format: A format string similar to scanf.
Example:
int number;
fscanf(file, "%d", &number);
T
U
5. fgets
Purpose: Reads a string from a file.
R
Syntax:
G
• str: Pointer to the buffer where the read string will be stored.
• n: The maximum number of characters to read (including the null terminator).
• stream: Pointer to the file from which to read.
Example:
char buffer[100];
fgets(buffer, sizeof(buffer), file);
JAGRUT AWAAZ 35
Z
Example:
fputs("This is a line.\n", file);
AA
7. fseek
Purpose: Moves the file pointer to a specified location in the file.
Syntax:
AW
int fseek(FILE* stream, long offset, int whence);
Parameters:
• stream: Pointer to the file.
T
SEEK_END).
Example:
R
8. ftell
Purpose: Returns the current position of the file pointer.
JA
Syntax:
long ftell(FILE* stream);
Parameters:
• stream: Pointer to the file.
Example:
long pos = ftell(file);
JAGRUT AWAAZ 36
Z
AA
AW
T
U
R
G
JA
JAGRUT AWAAZ 37