Here, We provide Programming For Problem Solving GTU Paper Solution Winter 2021. Read the Full PPS GTU Paper Solution Winter 2021 given below.
PPS GTU Old Paper Winter 2021 [Marks : 70] : Click Here
Question: 1
(a) Define the following terms:
1) Application Software 2) System Software 3) Algorithm 4) Flowchart.
Application Software: Application software is a type of computer software designed to perform specific tasks for end-users. Examples of application software include word processors, spreadsheets, web browsers, and video games.
System Software: System software is a type of computer software that manages and controls the hardware and other software resources of a computer. Examples of system software include operating systems, device drivers, and utilities.
Algorithm: An algorithm is a set of instructions or steps that are followed in a specific order to solve a problem or perform a specific task. It is a step-by-step procedure for solving a problem or performing a task.
Flowchart: A flowchart is a diagram that represents a process or workflow. It uses a variety of symbols and shapes to represent different parts of the process, such as inputs, outputs, decisions, and actions.
(b) Draw the flow chart to find the largest of the given three numbers –
A, B, and C
(c) Explain different types of operators used in c language with their
precedence and associativity.
C language has several types of operators that are used to perform various operations on variables and expressions. These operators can be classified into the following categories:
- Arithmetic Operators: These operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, and division. For example, +, -, *, / are arithmetic operators.
- Relational Operators: These operators are used to compare two values and determine the relationship between them. For example, ==, !=, >, <, >=, <= are relational operators.
- Logical Operators: These operators are used to evaluate logical expressions and return a Boolean value (true or false). For example, &&, ||, ! are logical operators.
- Assignment Operators: These operators are used to assign a value to a variable. For example, =, +=, -=, *=, /= are assignment operators.
- Bitwise Operators: These operators are used to perform bit-level operations on variables. For example, &, |, ^, ~, <<, >> are bitwise operators.
- Miscellaneous Operators: These operators are used for various purposes such as incrementing and decrementing a variable, taking the address of a variable, and dereferencing a pointer. For example, ++, –, &, *, sizeof are miscellaneous operators.
The precedence and associativity order of operators in C language are as follows:
Highest precedence:
postfix increment (++) and decrement (–)
unary plus (+) and minus (-)
bitwise NOT (~)
logical NOT (!)
address-of (&) and indirection (*)
sizeof
type cast
unary conditional operator (?:)
High Precedence:
exponentiation (**)
multiplicative (*, /, %)
additive (+, -)
shift (<<, >>)
relational (<, >, <=, >=, !=, ==)
equality (==, !=)
bitwise AND (&)
bitwise XOR (^)
bitwise OR (|)
logical AND (&&)
logical OR (||)
Low Precedence:
ternary operator (?:)
assignment (=, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=)
the comma (,)
Associativity:
Left-to-right associativity for the assignment operator, ternary operator, and comma operator.
Right-to-left associativity for all other operators.
Question: 2
(a) Discuss the use of break and continue statements in C with examples.
- The break statement is used to immediately exit a loop or a switch statement. When a break statement is encountered inside a loop or a switch, the execution of the loop or switch is immediately terminated, and the control is transferred to the statement immediately following the loop or switch.
Example:
- for (int i = 0; i < 10; i++) {
- if (i == 5) {
- break;
- }
- printf(“%d\n”, i);
- }
In this example, the loop will iterate from 0 to 4 and print the numbers, but when the value of i becomes 5, the break statement will be executed, and the loop will be terminated, so the number 5 will not be printed.
2. The continue statement is used to skip the current iteration of a loop and continue with the next iteration. When a continue statement is encountered inside a loop, the current iteration of the loop is skipped and the control is transferred to the next iteration.
- int i = 0;
- while (i < 10) {
- i++;
- if (i % 2 == 0) {
- continue;
- }
- printf(“%d\n”, i);
- }
In this example, the loop will iterate from 1 to 10 and print the odd numbers only, because the continue statement will be executed when the value of i is even, so the current iteration will be skipped and the control will be transferred to the next iteration.
(b) Compare and contrast while and do while loop with example.
Both while and do-while loops are used to repeatedly execute a block of code in C programming. However, there are some key differences between the two types of loops:
The while loop: The while loop repeatedly executes a block of code as long as a certain condition is true. The condition is evaluated at the beginning of each iteration before the code inside the loop is executed. If the condition is false, the loop is terminated and the control is transferred to the statement following the loop.
Example:
- int i = 0;
- while (i < 10) {
- printf(“%d\n”, i);
- i++;
- }
- In this example, the loop will iterate from 0 to 9 and print the numbers, and it will stop when the condition i < 10 is false.
- The do-while loop: The do-while loop also repeatedly executes a block of code as long as a certain condition is true. However, the condition is evaluated at the end of each iteration, after the code inside the loop is executed. This means that the code inside the loop is always executed at least once, regardless of the initial value of the condition.
- Example:
- int i = 0;
- do {
- printf(“%d\n”, i);
- i++;
- } while (i < 10);
- In this example, the loop will also iterate from 0 to 9 and print the numbers, and it will stop when the condition i < 10 is false.
the main difference between the while loop and the do-while loop is the point at which the condition is evaluated. The while loop evaluates the condition before executing the code inside the loop, while the do-while loop evaluates the condition after executing the code inside the loop
(c) Write a C program to print the following pattern.
1
2 2
3 3 3
- #include <stdio.h>
- int main() {
- int i, j;
- for (i = 1; i <= 3; i++) {
- for (j = 1; j <= i; j++) {
- printf(“%d “, i);
- }
- printf(“\n”);
- }
- return 0;
- }
- The output of the program will be
- 1
- 2 2
- 3 3 3
(c) Write a C program to print following pattern.
1
2 3
4 5 6
- #include <stdio.h>
- int main() {
- int i, j, k = 1;
- for (i = 1; i <= 3; i++) {
- for (j = 1; j <= i; j++) {
- printf(“%d “, k);
- k++;
- }
- printf(“\n”);
- }
- return 0;
- }
- The output of the program will be
- 1
- 2 3
- 4 5 6
Question: 3
(a) Write a program to check whether entered character is a vowel or not?
- #include <stdio.h>
- int main() {
- char ch;
- printf(“Enter a character: “);
- scanf(“%c”, &ch);
- if (ch == ‘a’ || ch == ‘e’ || ch == ‘i’ || ch == ‘o’ || ch == ‘u’ || ch == ‘A’ || ch == ‘E’ || ch == ‘I’ || ch == ‘O’ || ch == ‘U’) {
- printf(“The character is a vowel.\n”);
- } else {
- printf(“The character is not a vowel.\n”);
- }
- return 0;
- }
(b) Explain getch(), getchar(), gets(), puts()Â
- getch(): It is used to read a single character from the keyboard without waiting for the user to press the enter key. It is defined in the <conio.h> header file. getch() function is used to read a single character from the keyboard, but it does not echo the character to the screen.
- getchar(): It is used to read a single character from the keyboard, including the enter key. It is defined in the <stdio.h> header file. getchar() function waits for the user to press the enter key after the input.
- gets(): It is used to read a string (sequence of characters) from the keyboard. It is defined in the <stdio.h> header file. gets() function reads the string entered by the user including whitespace characters and newline characters.
- puts(): It is used to output a string (sequence of characters) to the screen. It is defined in the <stdio.h> header file. puts() function writes the string to the standard output device and appends a newline character at the end of the string.
(c) Develop a menu-based program to perform addition, multiplication,
subtraction and division using a user-defined function.
- #include <stdio.h>
- // function to perform addition
- double add(double a, double b) {
- return a + b;
- }
- // function to perform multiplication
- double multiply(double a, double b) {
- return a * b;
- }
- // function to perform subtraction
- double subtract(double a, double b) {
- return a – b;
- }
- // function to perform division
- double divide(double a, double b) {
- return a / b;
- }
- int main() {
- double a, b;
- int choice;
- // menu
- printf(“Menu:\n”);
- printf(“1. Addition\n”);
- printf(“2. Multiplication\n”);
- printf(“3. Subtraction\n”);
- printf(“4. Division\n”);
- printf(“Enter your choice: “);
- scanf(“%d”, &choice);
- printf(“Enter two numbers: “);
- scanf(“%lf %lf”, &a, &b);
- // perform an operation based on user choice
- switch(choice) {
- case 1:
- printf(“%.2lf + %.2lf = %.2lf\n”, a, b, add(a, b));
- break;
- case 2:
- printf(“%.2lf * %.2lf = %.2lf\n”, a, b, multiply(a, b));
- break;
- case 3:
- printf(“%.2lf – %.2lf = %.2lf\n”, a, b, subtract(a, b));
- break;
- case 4:
- printf(“%.2lf / %.2lf = %.2lf\n”, a, b, divide(a, b));
- break;
- default:
- printf(“Invalid choice\n”);
- }
- return 0;
- }
Question: 3
(a) Write an algorithm for finding odd and even numbers from given two numbers.
Here is an algorithm for finding all odd and even numbers between two given numbers:
- Begin
- Input the first number (let’s call it “start”) and the second number (let’s call it “end”)
- Initialize two variables, one for storing odd numbers and another for storing even numbers
- Set a loop variable “i” to the value of “start”
- While “i” is less than or equal to “end” do the following:
a) Check if “i” is odd or even:
if “i” is odd, add “i” to the list of odd numbers
if “i” is even, add “i” to the list of even numbers
b) increment “i” by 1
- Print the list of odd numbers
- Print the list of even numbers
- End
(b) Write a program to check whether the entered number is prime or not with the help of the user-defined function check-prime().
#include <stdio.h>
int check_prime(int num) {
int i;
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
return 0;
}
}
return 1;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (check_prime(num) == 1) {
printf("%d is a prime number\n", num);
} else {
printf("%d is not a prime number\n", num);
}
return 0;
}
(c) Write a program to find out the largest of an array.
#include <stdio.h>
int main() {
int n, i;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array: ");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int largest = arr[0];
for (i = 1; i < n; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
}
printf("The largest element in the array is: %d\n", largest);
return 0;
}
Question: 4
(a) What is structure? Explain with an example how to declare a structure and how to initialize it.
A Structure is a user-defined data type that groups together different types of data (such as integers, strings, etc.) under a single name. A structure can be used to represent a real-world object, such as a student, where the object has different properties (name, age, etc.).
Here is an example of how to declare a structure in C:
struct student {
char name[50];
int age;
char major[50];
};
You can also initialize the members individually like this:
struct student s1;
strcpy(s1.name, "John Doe");
s1.age = 21;
strcpy(s1.major, "Computer Science");
(b) Explain the following string manipulation function. strcat( ), strcpy( ) ,strcmp( ) and strlen( )
strcat( ): The strcat( ) function is used to concatenate (or append) one string to the end of another string.
strcpy( ): The strcpy( ) function is used to copy one string to another string.
strcmp( ): The strcmp( ) function is used to compare two strings. It compares the ASCII values of each character in the two strings and returns an integer value indicating the result of the comparison. If the first string is lexicographically greater than the second, it returns a positive number, if the first string is lexicographically less than the second, it returns a negative number and if the two strings are equal then it returns 0.
strlen( ): The strlen( ) function is used to find the length of a string. It returns the number of characters in the string, not including the null terminator.
Example:
char str1[20] = "Hello, ";
char str2[] = "world!";
strcat(str1, str2);
printf("%s\n", str1); // Output: "Hello, world!"
char str3[20];
strcpy(str3, str1);
printf("%s\n", str3); // Output: "Hello, "
int result = strcmp(str1, str2);
printf("%d\n", result); // Output: -1
int length = strlen(str1);
printf("%d\n", length); // Output: 7
(c) Write a program in c for multiply two matrices A and B of dimensions pXq and qXr respectively and store the result in third matrix C
#include <stdio.h>
int main() {
int p, q, r;
printf("Enter the dimensions of matrix A (p x q): ");
scanf("%d %d", &p, &q);
printf("Enter the dimensions of matrix B (q x r): ");
scanf("%d %d", &q, &r);
int A[p][q], B[q][r], C[p][r];
// Input matrix A
printf("Enter the elements of matrix A:\n");
for (int i = 0; i < p; i++) {
for (int j = 0; j < q; j++) {
scanf("%d", &A[i][j]);
}
}
// Input matrix B
printf("Enter the elements of matrix B:\n");
for (int i = 0; i < q; i++) {
for (int j = 0; j < r; j++) {
scanf("%d", &B[i][j]);
}
}
// Multiply matrices A and B and store result in C
for (int i = 0; i < p; i++) {
for (int j = 0; j < r; j++) {
C[i][j] = 0;
for (int k = 0; k < q; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
// Output matrix C
printf("The result of multiplying A and B is:\n");
for (int i = 0; i < p; i++) {
for (int j = 0; j < r; j++) {
printf("%d ", C[i][j]);
}
printf("\n");
}
return 0;
}
Question: 4
(a) Demonstrate the declaration and initialization of a two-dimensional array with a suitable example.
Here is an example of declaring and initializing a 2-dimensional array in C:
#include <stdio.h>
int main() {
// Declare a 2-dimensional array with 3 rows and 4 columns
int my_array[3][4];
// Initialize the array with some values
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
my_array[i][j] = i * j;
}
}
// Print the array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
printf("%d ", my_array[i][j]);
}
printf("\n");
}
return 0;
}
Output:
0 0 0 0
0 1 2 3
0 2 4 6
(b) Explain nested if else ladder with a suitable example.
A nested if-else ladder is a type of control flow statement in which an “if-else” statement is placed inside another “if-else” statement. This creates a multi-level decision-making structure, where the outcome of one “if-else” statement affects the execution of the next one.
Here is an example of a nested if-else ladder:
int age = 25;
char gender = 'M';
if (age >= 18) {
if (gender == 'M') {
printf("You are a male adult.\n");
}
else if (gender == 'F') {
printf("You are a female adult.\n");
}
else {
printf("Invalid gender input.\n");
}
}
else {
if (gender == 'M') {
printf("You are a male minor.\n");
}
else if (gender == 'F') {
printf("You are a female minor.\n");
}
else {
printf("Invalid gender input.\n");
}
}
(c) Write a program in c using structure to enter rollno, marks of the three
subject for 3 student and find total obtained by each student
#include <stdio.h>
struct student {
int roll_no;
int sub1;
int sub2;
int sub3;
int total;
};
int main() {
struct student s[3];
int i;
for (i = 0; i < 3; i++) {
printf("Enter the roll number of student %d: ", i+1);
scanf("%d", &s[i].roll_no);
printf("Enter the marks of 3 subjects for student %d: ", i+1);
scanf("%d%d%d", &s[i].sub1, &s[i].sub2, &s[i].sub3);
s[i].total = s[i].sub1 + s[i].sub2 + s[i].sub3; //calculate total marks
}
printf("\nRoll No\tSub1\tSub2\tSub3\tTotal\n");
for (i = 0; i < 3; i++) {
printf("%d\t%d\t%d\t%d\t%d\n", s[i].roll_no, s[i].sub1, s[i].sub2, s[i].sub3, s[i].total);
}
return 0;
}
Question: 5
(a) What do you mean by recursive function? What care must be taken while writing a program with a recursive function?
A recursive function is a function that calls itself in order to solve a problem. It breaks the problem down into smaller and smaller sub-problems until it reaches a point where the problem can be solved without recursion.
Here are a few things to keep in mind when writing a program with a recursive function:
- Base case: A recursive function must have a base case, which is a condition that stops the recursion. If the base case is not defined, the function will continue to call itself indefinitely, resulting in an infinite loop.
- Recursive case: The recursive case is the part of the function that calls itself. It must be written in such a way that it will eventually reach the base case.
- Simplifying the problem: The recursive case must simplify the problem in some way. If the problem is not simplified, the function will not make progress toward the base case.
(b) Explain fopen() and its mode with example.Â
fopen() is a standard C library function that is used to open a file. It takes two arguments: the name of the file and the mode in which the file should be opened. The return value is a pointer to a FILE structure, which is used to read from or write to the file.
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("example.txt", "r"); // open example.txt in read mode
if (fp == NULL) {
printf("Error opening file\n");
return 1;
}
// read from file using fp
// ...
fclose(fp); // close the file
return 0;
}
The available modes for fopen() are:
“r”: Open the file for reading. If the file does not exist or cannot be read, fopen() returns NULL.
“w”: Open the file for writing. If the file does not exist, it will be created. If the file already exists, its contents will be truncated (deleted).
“a”: Open the file for writing in append mode. If the file does not exist, it will be created. If the file already exists, new data will be written to the end of the file.
“r+”: Open the file for reading and writing. The file must exist.
“w+”: Open the file for reading and writing. If the file does not exist, it will be created. If the file already exists, its contents will be truncated (deleted).
“a+”: Open the file for reading and writing in append mode. If the file does not exist, it will be created. If the file already exists, new data will be written to the end of the file.
(c) Describe different categories of user-defined functions.Â
In C programming, user-defined functions can be broadly categorized into the following types:
- Normal Functions: These are the basic functions that are defined by the user. They perform a specific task and return a value. For example, a function to calculate the area of a circle, a function to find the factorial of a number, etc.
- Recursive Functions: These functions call themselves in order to solve a problem. They are used to solve problems that can be broken down into smaller sub-problems, which are similar to the original problem.
- Function Overloading: In C++, function overloading is possible, which means that multiple functions can have the same name, but different parameter lists. This feature allows for more flexibility and code reusability.
- Inline Functions: These are functions which are expanded in line when they are called, rather than being called through the normal function call mechanism. They are useful for small functions, as they can improve performance by reducing function call overhead.
- Static Functions: These functions have a scope limited to the file in which they are defined. They are not accessible from outside the file, making them useful for implementing hidden functionality that is only used within the file.
- Variadic Functions: These functions can take a variable number of arguments. They are defined using the … notation and are usually used to implement functions that can handle a variable number of arguments like printf, scanf.
- Pointer to Functions: These are functions that take one or more pointer arguments, and/or return a pointer. They are used to pass or return function pointers, which can be used to implement callback functions and other advanced functionality.
Question: 5
(a) What is a pointer? Explain how pointers are declared and initialized
A pointer is a variable that stores the memory address of another variable. In C, a pointer is a variable that holds the memory address of another variable of a specific data type.
Pointers allow you to indirectly access and manipulate variables, which can be useful for a variety of tasks, such as dynamic memory allocation and passing variables to functions.
int x = 10;
int *ptr; // declare a pointer to an integer
ptr = &x; // initialize the pointer with the address of variable x
You can also declare and initialize pointer in single line like this:
int x = 10;
int *ptr = &x;
You can also use the malloc() function to dynamically allocate memory for a pointer and assign its address to the pointer. Here is an example:
int *ptr = (int *) malloc(sizeof(int));
(b) Compare malloc() and calloc() functions for dynamic memory
allocation.
Both malloc() and calloc() are C library functions that are used for dynamic memory allocation. They are used to allocate memory on the heap, which can be freed using the free() function.
Here is a comparison of malloc() and calloc():
malloc(): malloc() is used to allocate a block of memory on the heap. The function takes a single argument, which is the size of the memory block to be allocated. The memory block is not initialized and contains garbage values.
int *ptr = (int *) malloc(sizeof(int) * 10);
calloc(): calloc() is used to allocate a block of memory on the heap and initialize it to zero. The function takes two arguments: the number of blocks to be allocated and the size of each block.
int *ptr = (int *) calloc(10, sizeof(int));
- Initialization: malloc() only allocates memory and does not initialize it, whereas calloc() allocates and initializes the memory to zero.
- Arguments: malloc() takes a single argument, the size of the memory block to be allocated, while calloc() takes two arguments, the number of blocks and the size of each block.
- Memory Allocated: malloc() returns a pointer to the first byte of the allocated memory block, whereas calloc() returns a pointer to the first byte of the first block of the allocated memory.
- Performance: calloc() is generally slower than malloc() as it initializes the memory to zero, but is useful when it is necessary to have zero-initialized memory.
(c) Develop a program in C to check whether the entered number is prime or not by creating a user-defined function named check_prime().
#include <stdio.h>
int check_prime(int num) {
if (num <= 1) {
return 0;
}
for (int i = 2; i < num; i++) {
if (num % i == 0) {
return 0;
}
}
return 1;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (check_prime(num)) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}
return 0;
}
The program starts by defining a user-defined function check_prime(), which takes an integer as a parameter. It checks if the number is less than or equal to 1, it returns 0, as it’s not a prime number. Then it uses a for loop to check if the number is divisible by any integer between 2 and itself, if it is divisible, it returns 0, indicating it’s not a prime number. If the number is not divisible by any integer between 2 and itself, the function returns 1, indicating that a number is a prime number.
Read More : PPS GTU Paper Solution Summer 2022
Read More : BEE GTU Paper Solution Winter 2021
“Do you have the answer to any of the questions provided on our website? If so, please let us know by providing the question number and your answer in the space provided below. We appreciate your contributions to helping other students succeed.