Here, We provide PPS GTU Paper Solution Summer 2022. Read the Full Programming For Problem Solving gtu paper solution given below.
PPS GTU Old Paper Summer 2022 [Marks : 70] : Click Here
(a) Answer the following questions
Define: Compiler
Justify, 2 and ‘2’ both are not same in C language.
What is the role of sizeof operator in C language?
Definition of Compiler
A compiler is a software program that takes source code written in a high-level programming language and converts it into machine code that can be executed by a computer.
Justification:
In the C programming language, the number 2 is an integer constant, while the string “2” is a character string constant. These are not the same and cannot be used interchangeably in C. For example, the integer constant 2 can be used in mathematical operations, such as addition and multiplication, while the string “2” cannot. The string “2” must be converted to an integer value before it can be used in such operations.
Role of sizeof Operator:
The sizeof operator in C is a unary operator that returns the size, in bytes, of a data type or a variable. It can be used to determine the memory requirements of a program, to allocate dynamic memory, and to determine the size of arrays and structures. The sizeof operator is particularly useful when writing code that is platform-independent, as it can be used to determine the size of data types that may vary depending on the hardware architecture.
(b) Answer the following questions
Ternary operator can be nested. (True/False)
What do you mean by enumerated data type in C language?
What is the difference between char *p and char p[]?
What do you mean by function prototype?
Ternary operator can be nested. (True/False)
True. Ternary operator can be nested in C language.
What do you mean by enumerated data type in C language?
Enumerated data type in C language is a user-defined data type that consists of a set of named values. It provides a way to assign a name to a constant integer value, making the code more readable and easier to maintain.
What is the difference between char *p and char p[]?
char *p
is a pointer to a character, whereas char p[]
is an array of characters. The difference is that a pointer is a variable that holds the memory address of another variable, while an array is a contiguous block of memory that stores multiple values of the same type.
What do you mean by function prototype?
A function prototype in C language is a declaration of a function that specifies the function’s name, return type, and the types of its arguments. It provides information to the compiler about the function’s signature, allowing the compiler to check the type compatibility of calls to the function and to generate the necessary code for calling the function. Function prototypes are typically declared at the top of a source file before they are used.
(c) Answer the following questions
Define Algorithm.
What do you mean by recursion?
What do you mean by program and program control?
What is a pointer?
What is the role of getc( ) and getw() file functions?
Programming needs logic building. Justify
Define Interpreter.
An algorithm is a set of well-defined steps that specifies a procedure for solving a particular problem. An algorithm must be clear, effective, and unambiguous, and should be finite and terminate after a finite number of steps.
Recursion is a programming technique where a function calls itself to solve a problem. The function breaks down a complex problem into smaller sub-problems, and then solves those sub-problems by calling itself. The recursion continues until the sub-problems can be solved directly, or until a base case is reached.
Program and program control refer to the instructions that a computer follows to perform a task. A program is a set of instructions that tell the computer what to do, and program control refers to the flow of execution of these instructions. The program control determines the order in which the instructions are executed and the conditions under which certain instructions are executed.
A pointer is a variable in C programming that stores the memory address of another variable. Pointers are used to manipulate data stored in memory and to pass arguments to functions by reference. They are also used to create dynamic data structures, such as linked lists and trees.
The getc()
function is used to read a single character from a file, while the getw()
function is used to read a word (a binary integer) from a file. These functions are typically used to read data from a file in a sequential manner.
Programming needs logic building because a program is a set of instructions that solve a problem. To write effective and efficient code, a programmer must understand the problem they are trying to solve and use a logical and organized approach to write the code. A well-built logic in programming helps in debugging the code, reducing the chance of errors, and making the code more maintainable.
An interpreter is a program that executes instructions written in a high-level programming language, such as Python or JavaScript. The interpreter reads the source code, converts it into machine code, and executes it on the fly. Unlike compilers, which convert source code into machine code before the program is executed, interpreters execute code line by line, making it possible to run the program without precompilation.
(a) Write the outputs of the following expression:
i) 50 % 2 / 3 + 2
ii) 21 / (int) 2.5 + 3
iii) (1 > 2 ) || ( 2 < 3) && 5 < 1
i) 50 % 2 / 3 + 2
The output of the expression is 2
.
ii) 21 / (int) 2.5 + 3
The output of the expression is 13
.
iii) (1 > 2 ) || ( 2 < 3) && 5 < 1
The output of the expression is 0
.
(b) Write a program to print Fibonacci series.e.g.0, 1, 1, 2, 3, 5, 8, 13.
#include <stdio.h>
int main() {
int i, n, t1 = 0, t2 = 1, next_term;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 1; i <= n; i++) {
printf("%d, ", t1);
next_term = t1 + t2;
t1 = t2;
t2 = next_term;
}
return 0;
}
(c) Explain the working of various bit-wise operators with the example.
In C language, the following bit-wise operators are used to perform operations on individual bits of an integer value:
Bitwise AND (&
): This operator performs a bit-wise AND operation on each pair of corresponding bits in the operands. The result is 1 if both bits are 1, and 0 otherwise. For example, consider the expression 5 & 3
. The binary representation of 5 is 0000 0101
and the binary representation of 3 is 0000 0011
. The result of the bit-wise AND operation is 0000 0001
, which is equal to 1.
Bitwise OR (|
): This operator performs a bit-wise OR operation on each pair of corresponding bits in the operands. The result is 1 if either or both bits are 1, and 0 otherwise. For example, consider the expression 5 | 3
. The binary representation of 5 is 0000 0101
and the binary representation of 3 is 0000 0011
. The result of the bit-wise OR operation is 0000 0111
, which is equal to 7.
Bitwise XOR (^
): This operator performs a bit-wise exclusive OR operation on each pair of corresponding bits in the operands. The result is 1 if exactly one of the bits is 1, and 0 otherwise. For example, consider the expression 5 ^ 3
. The binary representation of 5 is 0000 0101
and the binary representation of 3 is 0000 0011
. The result of the bit-wise XOR operation is 0000 0110
, which is equal to 6.
Bitwise NOT (~
): This operator inverts the value of each bit in the operand. For example, consider the expression ~5
. The binary representation of 5 is 0000 0101
. The result of the bit-wise NOT operation is 1111 1010
, which is equal to -6
in two’s complement representation.
Left Shift (<<
): This operator shifts the bits of the first operand to the left by the number of positions specified by the second operand. For example, consider the expression 5 << 1
. The binary representation of 5 is 0000 0101
. The result of the left shift operation is 0000 1010
, which is equal to 10.
Right Shift (>>
): This operator shifts the bits of the first operand to the right by the number of positions specified by the second operand. For example, consider the expression 5 >> 1
. The binary representation of 5 is 0000 0101
. The result of the right shift operation is 0000 0010
, which is equal to 2.
[OR] (c) List out all various string functions and describe them with syntax and
example.
Here are some of the most commonly used string functions, along with their syntax and examples:
strlen
: This function returns the length of a null-terminated string. The syntax is as follows:size_t strlen(const char *s);
. For example,strlen("Hello, world!")
returns12
.strcpy
: This function copies a null-terminated string from one location to another. The syntax is as follows:char *strcpy(char *dest, const char *src);
. For example, the following code copies the string “Hello, world!” to the char arraydest
:
char dest[100];
strcpy(dest, "Hello, world!");
strncpy
: This function copies at mostn
characters from a null-terminated string from one location to another. The syntax is as follows:char *strncpy(char *dest, const char *src, size_t n);
. For example, the following code copies the first 5 characters of the string “Hello, world!” to the char arraydest
:
char dest[100];
strncpy(dest, "Hello, world!", 5);
strcat
: This function concatenates two null-terminated strings. The syntax is as follows:char *strcat(char *dest, const char *src);
. For example, the following code concatenates the strings “Hello” and “, world!” and stores the result in the char arraydest
:
cCopy codechar dest[100] = "Hello";
strcat(dest, ", world!");
strncat
: This function concatenates at mostn
characters of a null-terminated string to another null-terminated string. The syntax is as follows:char *strncat(char *dest, const char *src, size_t n);
. For example, the following code concatenates the first 5 characters of the string “Hello, world!” to the string “Hello” and stores the result in the char arraydest
:
cCopy codechar dest[100] = "Hello";
strncat(dest, ", world!", 5);
(a) Explain ternary (?:) operator in detail with the example.
The ternary operator (also known as the conditional operator) in C is a shorthand way of writing an if
–else
statement.
The syntax for the ternary operator is as follows: condition ? expression1 : expression2
.
If condition
is true, the ternary operator evaluates to expression1
, otherwise, it evaluates to expression2
.
Here is an example of using the ternary operator to return the larger of two values:
#include <stdio.h> int main() { int a = 5, b = 10; int max = (a > b) ? a : b; printf("The maximum value is: %d\n", max); return 0; }
(b) Discuss the need of break and continue statements with example.
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.
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.
(c) Design a flowchart for checking whether a given number is palindrome or not.
(a) What is meant by array of pointers? Explain it with example.
An array of pointers is a data structure in C where each element of the array is a pointer. The elements of the array can point to variables, functions, or any other data type.
Here is an example of declaring and initializing an array of pointers to integers:
#include <stdio.h>
int main() {
int a = 10, b = 20, c = 30;
int *ptr[3];
ptr[0] = &a;
ptr[1] = &b;
ptr[2] = &c;
printf("Value of a: %d\n", *ptr[0]);
printf("Value of b: %d\n", *ptr[1]);
printf("Value of c: %d\n", *ptr[2]);
return 0;
}
In this example, ptr
is declared as an array of 3 pointers to integers. The addresses of the variables a
, b
, and c
are then stored in the elements of the array ptr
. The values of a
, b
, and c
can be accessed through the pointers stored in the array by dereferencing them using the *
operator.
(b) Write a program to print the triangle shown below.
A
A B
A B C
A B C D
#include <stdio.h>
int main()
{
int i, j, n = 5;
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) {
printf("%c ", 'A' + j - 1);
}
printf("\n");
}
return 0;
}
(c) Draw block diagram of computer system and explain the functions of each
component in detail.
(a) Explain type casting with example.
Type casting refers to converting a variable from one data type to another data type explicitly. This can be achieved using type casting operators. The type casting operator is written in the form of (type_name) expression.
For example, consider the following code:
float f = 10.5;
int i;
i = (int) f;
Here, the value of f
is 10.5 which is a float data type. We want to store it in an integer variable i
. In order to do so, we use type casting operator (int)
to convert f
from float to integer. The result will be i = 10
, the fractional part of the float value will be truncated.
(b) Compare and contrast goto statement and switch statement with example.
Feature | goto statement | switch statement |
---|---|---|
Purpose | Jump to a specific label in the program | Choose one of several branches based on the value of an expression |
Syntax | goto label; ... label: statement; | switch (expression) { case value1: statement1; break; case value2: statement2; break; ... default: statement; } |
Example | #include <stdio.h> int main() { int i = 0; loop: printf("%d\n", i); i++; if (i < 10) { goto loop; } return 0; } | #include <stdio.h> int main() { int option = 2; switch (option) { case 1: printf("Option 1 selected\n"); break; case 2: printf("Option 2 selected\n"); break; case 3: printf("Option 3 selected\n"); break; default: printf("Invalid option\n"); } return 0; } |
Pros | Can be used to escape from nested loops | Easy to read and understand, can handle multiple cases efficiently |
Cons | Can lead to unstructured and hard-to-debug code | Only supports integers and characters as case values, cannot handle ranges or complex expressions |
(c) Write a recursive program to find factorial of a given number.
#include <stdio.h>
int factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial(n - 1);
}
int main() {
int num, result;
printf("Enter a positive integer: ");
scanf("%d", &num);
result = factorial(num);
printf("Factorial of %d is %d\n", num, result);
return 0;
}
(a) Differentiate function and macro.
Feature | Function | Macro |
---|---|---|
Definition | A self-contained block of code that performs a specific task and returns a value | A preprocessor directive that performs a simple text substitution |
Syntax | return_type function_name(arguments) { statements; } | #define macro_name replacement_text |
Execution | Function calls are executed at runtime | Macros are expanded by the preprocessor before the program is compiled |
Arguments | Functions can accept arguments and operate on them | Macros can accept arguments but have no type checking or error checking |
Return value | Functions can return a value of any type, or void if there is no return value | Macros do not return a value, they are replaced by their replacement text in the code |
Scope | Functions have their own local scope, and the variables declared within a function are destroyed when the function returns | Macros do not have a scope, and their replacement text is visible to the entire program |
Overhead | Functions have some overhead due to function call and return, and local variable allocation | Macros are expanded in place and have no overhead, but can result in larger and less readable code if used excessively |
(b) Explain while loop 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.
(c) Explain one dimensional array, two dimensional array and multi-dimensional
array with their syntax and example.
In C programming language, arrays are used to store collections of elements of the same data type.
One Dimensional Array:
A one-dimensional array is a linear arrangement of elements, where each element can be accessed by a single index. The syntax for declaring a one-dimensional array is:
data_type array_name[array_size];
For example, the following code declares an array arr
of size 5, which can store 5 integers:
int arr[5];
Elements of the array can be accessed using the array name and an index, like arr[0]
, arr[1]
, etc.
Two Dimensional Array:
A two-dimensional array is an array of arrays, where each element is accessed using two indices. The syntax for declaring a two-dimensional array is:
data_type array_name[row_size][column_size];
For example, the following code declares a 2D array matrix
of size 3×3, which can store 9 elements:
int matrix[3][3];
Elements of the 2D array can be accessed using the array name and two indices, like matrix[0][0]
, matrix[0][1]
, etc.
Multi-Dimensional Array:
A multi-dimensional array is an array of arrays, where each element is accessed using multiple indices. The syntax for declaring a multi-dimensional array is similar to a two-dimensional array:
data_type array_name[dimension1_size][dimension2_size]...[dimensionN_size];
For example, the following code declares a 3D array cube
of size 3x3x3, which can store 27 elements:
int cube[3][3][3];
Elements of the multi-dimensional array can be accessed using the array name and multiple indices, like cube[0][0][0]
, cube[0][0][1]
, etc.
(a) Differentiate call by value and call by reference.
Call by Value | Call by Reference |
---|---|
A copy of the actual argument is made and passed to the function. | A reference to the actual argument is passed to the function. |
Changes made to the argument within the function do not affect the original argument outside the function. | Changes made to the argument within the function affect the original argument outside the function. |
The argument must be a simple data type, such as an integer or a character. | The argument must be a pointer to a simple data type, such as an integer or a character. |
Syntax: function_name(argument) | Syntax: function_name(&argument) |
Example: void swap(int a, int b) { int temp = a; a = b; b = temp; } | Example: void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } |
(b) Explain structure within structure with example.
In C programming language, a structure within a structure is a concept of nested structures, where one structure can contain another structure as its member. This allows you to create complex data structures that can represent real-world objects and their attributes.
Here is an example to demonstrate structure within structure:
#include<stdio.h>
struct date
{
int day;
int month;
int year;
};
struct student
{
char name[30];
int roll_no;
struct date dob;
};
int main()
{
struct student s1;
printf("Enter name: ");
scanf("%s", s1.name);
printf("Enter roll number: ");
scanf("%d", &s1.roll_no);
printf("Enter date of birth (dd mm yyyy): ");
scanf("%d %d %d", &s1.dob.day, &s1.dob.month, &s1.dob.year);
printf("\nDisplaying Information:\n");
printf("Name: %s\n", s1.name);
printf("Roll number: %d\n", s1.roll_no);
printf("Date of birth: %d/%d/%d\n", s1.dob.day, s1.dob.month, s1.dob.year);
return 0;
}
(c) Explain error handling in file system with example.
Error handling in file systems is the process of dealing with errors that occur while reading or writing to a file. This is important because file operations can fail due to various reasons such as incorrect file path, insufficient memory, lack of permissions, etc.
In C, error handling in file systems is usually done using the functions ferror
and feof
. The ferror
function is used to check for errors in file operations and returns non-zero if an error occurs. The feof
function is used to check for end-of-file condition and returns non-zero if the end-of-file has been reached.
Here is an example to demonstrate error handling in file system:
#include<stdio.h>
int main()
{
FILE *fp;
char filename[] = "sample.txt";
char ch;
fp = fopen(filename, "r");
if(fp == NULL)
{
printf("Error in opening the file");
return 1;
}
while((ch = getc(fp)) != EOF)
{
putchar(ch);
}
if(ferror(fp))
{
printf("Error in reading the file");
return 1;
}
fclose(fp);
return 0;
}
In this example, the file sample.txt
is opened for reading using the fopen
function. If the file is not found or there is an error in opening the file, fp
will be set to NULL
and an error message is displayed. The file is then read character by character using the getc
function until the end-of-file is reached. If there is an error in reading the file, the ferror
function returns non-zero and an error message is displayed. Finally, the file is closed using the fclose
function.
(a) Define Union in ‘C’ with example.
A union in C is a data structure that allows you to store different data types in the same memory location. Unlike a structure, where each member has its own memory location, all members of a union share the same memory location. The size of a union is equal to the size of its largest member.
#include<stdio.h>
union number
{
int x;
float y;
char z;
};
int main()
{
union number num;
num.x = 10;
printf("The value of x is: %d\n", num.x);
num.y = 10.5;
printf("The value of y is: %f\n", num.y);
num.z = 'A';
printf("The value of z is: %c\n", num.z);
return 0;
}
(b) Define dynamic memory allocation. Explain malloc() and calloc().
Dynamic memory allocation is a technique in C programming that allows you to allocate memory dynamically during runtime. This means that you can allocate memory as needed, rather than declaring memory beforehand.
There are two functions in C for dynamic memory allocation: malloc()
and calloc()
.
malloc()
function: The malloc()
function is used to allocate a single block of memory dynamically. The syntax of the function is as follows:
void *malloc(size_t size);
where size
is the number of bytes to be allocated. The function returns a void pointer to the first byte of the newly allocated memory block, or NULL
if the allocation failed.
Example:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr, n = 5;
ptr = (int*)malloc(n * sizeof(int));
if (ptr == NULL)
{
printf("Memory allocation failed");
exit(0);
}
for (int i = 0; i < n; i++)
{
ptr[i] = i + 1;
}
for (int i = 0; i < n; i++)
{
printf("%d ", ptr[i]);
}
free(ptr);
return 0;
}
calloc()
function: The calloc()
function is used to allocate multiple blocks of memory dynamically and set all the allocated memory to 0. The syntax of the function is as follows:
void *calloc(size_t nmemb, size_t size);
where nmemb
is the number of blocks to be allocated and size
is the size of each block. The function returns a void pointer to the first byte of the newly allocated memory block, or NULL
if the allocation failed.
Example:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr, n = 5;
ptr = (int*)calloc(n, sizeof(int));
if (ptr == NULL)
{
printf("Memory allocation failed");
exit(0);
}
for (int i = 0; i < n; i++)
{
printf("%d ", ptr[i]);
}
free(ptr);
return 0;
}
In the above example, calloc()
function is used to allocate memory for n
integers and sets all the memory to 0. The output of the program will be all 0s.
(c) Explain the following File Handling functions: 1. fseek( ) 2. ftell( ) 3. fread( )
fwrite( ) 5. fscanf( ) 6. fprintf( ) 7.rewind( )
In C, file handling functions are used to perform operations on files stored on a computer’s hard drive.
fseek( ): This function allows you to move the file pointer to a specific location within a file. Syntax: int fseek(FILE *ptr, long int offset, int position); Example: fseek(fp, 10, SEEK_SET);
ftell( ): This function returns the current position of the file pointer. Syntax: long int ftell(FILE *ptr); Example: current_position = ftell(fp);
fread( ): This function reads a specified number of elements of a specified size from a file and stores them in a buffer. Syntax: size_t fread(void *ptr, size_t size, size_t count, FILE *fp); Example: fread(buffer, 1, 10, fp);
fwrite( ): This function writes a specified number of elements of a specified size from a buffer to a file. Syntax: size_t fwrite(const void *ptr, size_t size, size_t count, FILE *fp); Example: fwrite(buffer, 1, 10, fp);
fscanf( ): This function reads formatted data from a file. Syntax: int fscanf(FILE *fp, const char *format, …); Example: fscanf(fp, “%d”, &number);
fprintf( ): This function writes formatted data to a file. Syntax: int fprintf(FILE *fp, const char *format, …); Example: fprintf(fp, “%d”, number);
rewind( ): This function sets the file pointer to the beginning of the file. Syntax: void rewind(FILE *fp); Example: rewind(fp);
Read More : PPS GTU Paper Solution Winter 2021
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.