PPS Pyq
PPS Pyq
Give an
example.
// A function in C is a set of statements that perform a specific task. It acts as
a building block for organizing code, providing modularity, and enabling
code reusability.
Functions are enclosed within curly braces {} and have a name, return
type, and parameters.
They enhance readability, simplify complex tasks, and allow us to avoid
repeating the same code.
Example of a simple function:
#include <stdio.h>
int main() {
int result = add(5, 3);
printf("Sum: %d\n", result);
return 0;
}
User-Defined Functions:
Created by programmers to meet specific requirements.
Defined by the user.
No need to include any particular library.
// Library function example
#include <stdio.h>
int main() {
printf("Hello, world!\n");
return 0;
}
int main() {
greet(); // Calling user-defined function
return 0;
}
3. Write some properties and advantages of user defined functions in
C?
//Code Reusability: Functions can be called multiple times, reducing code
repetition.
Modularity: Breaks down complex programs into smaller sub-programs.
Easy Debugging and Maintenance: Isolates faulty functions during
debugging.
Readability: Divides complex problems into clear sub-programs.
Enhanced Organization: Keeps code organized and manageable.
4. Explain the various categories of user defined functions in C with
examples?
//Categories of user-defined functions in C include:
Functions with no arguments and no return value
Functions with arguments but no return value
Functions with no arguments but with a return value
Functions with arguments and a return value
Example:
cCopy code
#include <stdio.h>
int main() {
greet();
printNum(10);
printf("Random Number: %d\n",
generateRandom());
printf("Sum: %d\n", add(5, 3));
return 0;
}
void modifyValue(int a) {
a += 5;
}
Pass by Pointers:
Passes memory address (pointer) of a variable.
Allows modification of content at that memory location.
Example:
return_type function_name(parameters) {
// Base case (terminates recursion)
if (condition) {
return base_value;
}
// Recursive case
else {
return function_name(modified_parameters);
}
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d = %d\n", num, factorial(num));
return 0;
}
9. Differentiate between recursion and non recursion.
//Recursion is a technique in which a function calls itself directly or
indirectly. Non-recursion refers to solving a problem without using recursive
function calls. Recursion often simplifies the code and makes it more
readable but can sometimes lead to stack overflow errors if not implemented
carefully. Non-recursive solutions usually involve iteration or using auxiliary
data structures like stacks or queues.
10. How to Pass Array Individual Elements to Functions? Explain
with example program.
//Passing Individual Array Elements to Functions:
To pass individual elements of an array to a function in C, you can treat
them like regular variables. Here’s an example:
#include <stdio.h>
int main() {
int ageArray[] = {2, 8, 4, 12};
display(ageArray[1], ageArray[2]);
return 0;
}
11. write a Program using call function by value and call by address
or reference.
//Call by Value and Call by Address (Reference):
In C, we can pass arguments to functions either by value or by address
(reference).
Call by Value: The function receives a copy of the argument’s value.
Call by Address (Reference): The function receives the memory address of
the argument.
Example of call by value:
Example of static:
Example of register:
13. Explain briefly auto and static storage classes with examples?
//Auto and Static Storage Classes:
Auto: Variables declared inside a function block are by default auto
variables. They are created when the function is called and destroyed
when the function exits.
cCopy code
#include <stdio.h>
void func() {
auto int x = 10; // Auto variable
printf("Auto variable x: %d\n", x);
}
int main() {
func();
return 0;
}
void func() {
static int y = 10; // Static variable
printf("Static variable y: %d\n", y);
y++;
}
int main() {
func(); // Output: Static variable y: 10
func(); // Output: Static variable y: 11
return 0;
}
cCopy code
// File: file2.c
#include <stdio.h>
extern int globalVar; // Declaration
int main() {
printf("Value of globalVar: %d\n", globalVar);
return 0;
}
Register: Used to define local variables that should be stored in a
register instead of RAM for faster access.
cCopy code
#include <stdio.h>
int main() {
register int x = 10; // Register variable
printf("Register variable x: %d\n", x);
return 0;
}
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1)
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n = 10; // Change 'n' to the desired number
of terms
for (int i = 0; i < n; ++i) {
printf("%d ", fibonacci(i));
}
return 0;
}
16. Write functions using recursion for the quick sorting.
//#include <stdio.h>
int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
printf("Sorted array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
17. Write program for the merge sort using functions.
//#include <stdio.h>
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int arr_size = sizeof(arr) / sizeof(arr[0]);
// Declare a structure
struct Person {
char name[50];
int age;
float salary;
};
int main() {
// Declare structure variables
struct Person person1, person2;
return 0;
}
19. What is an Array of Structures?
//An array of structures is a collection of multiple structure variables. It
allows you to manage related data for multiple entities efficiently. Consider
the scenario of storing data for multiple employees or students. Instead of
declaring individual variables for each, you can use an array of structures.
Example of an array of structures:
struct Employee {
char name[50];
int employeeID;
};
struct Car {
char name[30];
int price;
};
int main() {
struct Car myCar = {"Tata", 1021};
printCarInfo(myCar);
return 0;
}
2.Call By Reference:
Pass a pointer to the structure (address of the structure) to the function.
Example:
struct Student {
char name[50];
int roll;
float marks;
};
int main() {
struct Student st1 = {"Aman", 19, 8.5};
display(&st1);
return 0;
}
21. Why should one prefer call by address method using a function
to swap two numbers?
//Call by Address Method for Swapping Two Numbers:
When using the call by address method (also known as call by reference),
we pass the memory address (pointer) of the variables to a function.
Advantages:
Efficiency: It avoids copying large data (like arrays or structures) during
function calls, which can be more efficient.
In-Place Swapping: Swapping occurs directly in memory, reducing
overhead.
Modifies Original Values: The original variables are modified directly.
Example program to swap two numbers using call by address:
#include <stdio.h>
int main() {
int a = 10, b = 20;
printf("Before swapping: a = %d, b = %d\n", a,
b);
swap(&a, &b);
printf("After swapping: a = %d, b = %d\n", a,
b);
return 0;
}
22. How many methods are three to pass value to a function? Write a
program to swap two numbers using call by address method with the
help of a function.
//There are three methods to pass values to a function: call by value, call by
address (or call by reference), and call by pointer. Here's a program to swap
two numbers using the call by address method:
c
#include <stdio.h>
*a = *b;
*b = temp;
int main() {
return 0;
struct Employee {
char name[50];
int empId;
float salary;
};
Initialization:
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
25. What is the difference between string and array? Also explain
how you will decide when to use which data ?
//String:
Represents a sequence of characters.
Immutable (cannot be changed after creation).
Stored as an object.
Example: "Hello, World!".
Array:
Collection of elements of the same data type.
Mutable (elements can be modified).
Stored in contiguous memory locations.
Example: int arr[] = {1, 2, 3, 4}.
int main() {
double a, b, c, discriminant, root1, root2, realPart, imagPart;
discriminant = b * b - 4 * a * c;
struct Student {
int rollNumber;
char name[50];
float marks;
};
int main() {
int arr[] = {8, 3, 1, 6, 4, 7, 2, 5};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);