Module-3
1. Explain declaration and initialization of an 1D-Array with an Example.
An array is a collection of elements of the same data type stored at contiguous memory locations. Declaration
involves specifying the data type and size, while initialization involves assigning initial values.
Declaration
The syntax for declaring a one-dimensional array is:
dataType arrayName[arraySize];
For example, to declare an integer array named numbers that can hold 5 elements:
int numbers[5];
Initialization
Initialization can be done during declaration or after.
During declaration:
int numbers[5] = {10, 20, 30, 40, 50};
Alternatively, the size can be omitted if initialized at the time of declaration:
int numbers[ ] = {10, 20, 30, 40, 50};
2. Write a program accept and display 5 integer numbers, store it in array.
#include <stdio.h>
int main() {
int numbers[5];
int i;
printf("Enter 5 integer numbers:\\n");
for (i = 0; i < 5; i++) {
scanf("%d", &numbers[i]);
}
printf("The numbers entered are:\\n");
for (i = 0; i < 5; i++) {
printf("%d\\n", numbers[i]);
}
return 0;
}
3. Write a C program to find the sum and average of n integer numbers using
arrays.
#include <stdio.h>
int main() {
Edited by:- LINGAMURTHY K 1
int n, i, sum = 0;
float avg;
printf("Enter the number of elements: ");
scanf("%d", &n);
int numbers;
printf("Enter %d numbers:\\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &numbers[i]);
sum += numbers[i];
}
avg = (float)sum / n;
printf("Sum = %d\\n", sum);
printf("Average = %.2f\\n", avg);
return 0;
}
4. Write a C program to print of the smallest number in an array.
#include <stdio.h>
int main() {
int n, i, smallest;
printf("Enter the number of elements: ");
scanf("%d", &n);
int numbers;
printf("Enter %d numbers:\\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &numbers[i]);
}
smallest = numbers[0];
for (i = 1; i < n; i++) {
if (numbers[i] < smallest) {
smallest = numbers[i];
}
}
printf("The smallest number in the array is: %d\\n", smallest);
return 0;
}
Edited by:- LINGAMURTHY K 2
5. Write a program to search an element in an array using linear search.
#include <stdio.h>
int main() {
int n, i, search, found = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
int numbers;
printf("Enter %d numbers:\\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &numbers[i]);
}
printf("Enter the element to search: ");
scanf("%d", &search);
for (i = 0; i < n; i++) {
if (numbers[i] == search) {
found = 1;
break;
}
}
if (found) {
printf("%d found at position %d\\n", search, i + 1);
} else {
printf("%d not found in the array\\n", search);
}
return 0;
}
6. Write a program to search an element in an array using binary search.
#include <stdio.h>
int main() {
int n, i, search, low, high, mid;
printf("Enter the number of elements: ");
scanf("%d", &n);
int numbers;
printf("Enter %d *sorted* numbers:\\n", n);
for (i = 0; i < n; i++) {
Edited by:- LINGAMURTHY K 3
scanf("%d", &numbers[i]);
}
printf("Enter the element to search: ");
scanf("%d", &search);
low = 0;
high = n - 1;
while (low <= high) {
mid = (low + high) / 2;
if (numbers[mid] == search) {
printf("%d found at position %d\\n", search, mid + 1);
break;
} else if (numbers[mid] < search) {
low = mid + 1;
} else {
high = mid - 1;
}
}
if (low > high) {
printf("%d not found in the array\\n", search);
}
return 0;
}
7. Write a C program to Sort the given set of N numbers using bubble sort.
#include <stdio.h>
int main() {
int n, i, j, temp;
printf("Enter the number of elements: ");
scanf("%d", &n);
int numbers;
printf("Enter %d numbers:\\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &numbers[i]);
}
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
Edited by:- LINGAMURTHY K 4
if (numbers[j] > numbers[j + 1]) {
temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
printf("Sorted array in ascending order:\\n");
for (i = 0; i < n; i++) {
printf("%d\\n", numbers[i]);
}
return 0;
}
8. Explain declaration and initialization of an 2D-Array with an Example.
A two-dimensional (2D) array is an array of arrays, represented as a grid with rows and columns. Declaration
involves specifying the data type, rows, and columns, while initialization involves assigning initial values to the
grid elements.
Declaration
The syntax for declaring a two-dimensional array is:
dataType arrayName[rows][columns];
For example, to declare an integer 2D array named matrix with 3 rows and 3 columns:
int matrix[3][3];
Initialization
Initialization can be done during declaration.
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
The inner braces are optional:
int matrix[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
If initialized at declaration, the row size can be omitted, but the column size must be present:
int matrix[][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
9. Write a C program to addition of two 3x3 Matrix.
#include <stdio.h>
int main() {
int A[3][3], B[3][3], C[3][3], i, j;
printf("Enter elements of 1st matrix:\n");
for (i = 0; i < 3; ++i)
Edited by:- LINGAMURTHY K 5
for (j = 0; j < 3; ++j)
scanf("%d", &A[i][j]);
printf("Enter elements of 2nd matrix:\n");
for (i = 0; i < 3; ++i)
for (j = 0; j < 3; ++j)
scanf("%d", &B[i][j]);
for (i = 0; i < 3; ++i)
for (j = 0; j < 3; ++j)
C[i][j] = A[i][j] + B[i][j];
printf("\nSum of two matrices:\n");
for (i = 0; i < 3; ++i) {
for (j = 0; j < 3; ++j)
printf("%d ", C[i][j]);
printf("\n");
}
return 0;
}
10. Write a C program to subtract of two 3x3 Matrix.
#include <stdio.h>
int main() {
int A[3][3], B[3][3], C[3][3], i, j;
printf("Enter elements of 1st matrix:\n");
for (i = 0; i < 3; ++i)
for (j = 0; j < 3; ++j)
scanf("%d", &A[i][j]);
printf("Enter elements of 2nd matrix:\n");
for (i = 0; i < 3; ++i)
for (j = 0; j < 3; ++j)
scanf("%d", &B[i][j]);
for (i = 0; i < 3; ++i)
for (j = 0; j < 3; ++j)
C[i][j] = A[i][j] - B[i][j];
printf("\nDifference of two matrices:\n");
for (i = 0; i < 3; ++i) {
Edited by:- LINGAMURTHY K 6
for (j = 0; j < 3; ++j)
printf("%d ", C[i][j]);
printf("\n");
}
return 0;
}
11. Explain declaration and initialization of a String with an Example.
A string in C is an array of characters terminated by a null character ('\0').
Declaration: A string is declared like any other array, specifying the data type char and the size. For example,
char str[20]; declares a string variable that can hold up to 19 characters plus the null terminator.
Initialization: Strings can be initialized during declaration in several ways:
Using individual characters: char str[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
Using a string literal: char str[] = "Hello"; (The compiler automatically adds the null terminator).
12. Write a program to compare two strings.
#include <stdio.h>
#include <string.h>
int main() {
char str1[20], str2[20];
int result;
printf("Enter first string: ");
scanf("%s", str1);
printf("Enter second string: ");
scanf("%s", str2);
result = strcmp(str1, str2);
if (result == 0)
printf("Strings are same.\n");
else
printf("Strings are different.\n");
return 0;
}
13. Write a program lower to upper, Upper to Lower of a given string.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
Edited by:- LINGAMURTHY K 7
char str[100];
int i;
printf("Enter a string: ");
scanf("%s", str);
// Convert to uppercase
for (i = 0; i < strlen(str); i++) {
str[i] = toupper(str[i]);
}
printf("Uppercase string: %s\n", str);
// Convert to lowercase
for (i = 0; i < strlen(str); i++) {
str[i] = tolower(str[i]);
}
printf("Lowercase string: %s\n", str);
return 0;
}
14. Explain the following string handling functions.
a) strcat b) strcpy c) strlen d) strcmp
a)strcat
The strcat( ) function is used to concatenate (join) two strings.
It appends the source string to the end of the destination string, overwriting the null terminator of the
destination string and adding a new null terminator at the end of the combined string. The destination array
must have enough space to hold the combined string.
Example:
char dest[20] = "Hello";
char src[ ] = " World";
strcat(dest, src); // dest becomes "Hello World"
b) strcpy
The strcpy( ) function is used to copy one string to another.
It copies the content of the source string (including the null character) to the destination string. The destination
array must be large enough to store the source string.
Example:
char dest[20];
char src[ ] = "Hello";
strcpy(dest, src); // dest becomes "Hello"
c) strlen
Edited by:- LINGAMURTHY K 8
The strlen() function is used to calculate the length of a string.
It returns the number of characters in the string, excluding the null terminator (\0).
Example:
char str[ ] = "Hello";
int length = strlen(str); // length becomes 5
d) strcmp
The strcmp() function is used to compare two strings lexicographically.
It compares strings character by character. It returns an integer value:
0 if the two strings are equal.
A negative value if the first string is less than the second string.
A positive value if the first string is greater than the second string.
Example:
char str1[ ] = "apple";
char str2[ ] = "banana";
int result = strcmp(str1, str2); // result will be negative
Edited by:- LINGAMURTHY K 9
Module -4
1. Explain the Need of a function.
Function is a block of code that performs a specific task and is executed when it is called.
Need of Functions:
Code Reusability: Same function can be used many times.
Modularity: Divides a large program into small parts.
Easy Debugging: Errors can be easily identified and fixed.
Improves Readability: Program becomes easy to understand.
Reduces Program Size: Avoids repetition of code.
Easy Maintenance: Changes can be made easily.
Example:
#include <stdio.h>
void show( )
{
printf("Hello C");
}
int main( )
{
show( );
return 0;
}
2. Explain the three elements of user defined function.
(Note: function declaration, function call, function definition.)
The three elements are function declaration (prototype), function call, and function definition.
Function Declaration (Prototype): This tells the compiler about the function's name, return type, and
parameters. It specifies the function's signature.
Example: int sum(int a, int b);
Function Call: This is the statement that executes the function's code. The program's control transfers to the
function.
Example: result = sum(10, 20);
Function Definition: This contains the actual code or body of the function, which performs the specific task.
Example:
int sum(int a, int b) {
return a + b;
Edited by:- LINGAMURTHY K 10
}
3. Briefly describe Call by value and Call by reference in the functions.
Call by value passes copies of arguments, while call by reference passes the actual memory addresses
(pointers) of arguments.
Call by Value:
Copies of the actual parameters are passed to the formal parameters of the function.
Changes made to the parameters inside the function do not affect the original variables outside the function.
Call by Reference:
The addresses of the actual parameters are passed to the function.
Changes made to the parameters inside the function directly modify the original variables outside the function
because they are accessing the same memory locations.
4. Write a program to swap two values using call by value.
#include <stdio.h>
void swap(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
printf("Inside function: a = %d, b = %d\n", a, b);
}
int main()
{
int x = 10, y = 20;
printf("Before swap: x = %d, y = %d\n", x, y);
swap(x, y);
printf("After swap: x = %d, y = %d\n", x, y);
return 0;
}
5. Write a program to swap two values using call by reference.
#include <stdio.h>
void swap(int *a, int *b)
{
int temp;
temp = *a;
Edited by:- LINGAMURTHY K 11
*a = *b;
*b = temp;
}
int main()
{
int x = 10, y = 20;
printf("Before swap: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swap: x = %d, y = %d\n", x, y);
return 0;
}
6. Explain nesting of a functions with an example.
Nesting of functions refers to calling one function from within the body of another function.
Example:
#include <stdio.h>
void second()
{
printf("Second function\n");
}
void first()
{
printf("First function\n");
second();
}
int main()
{
first();
return 0;
}
7. Explain different categories of functions.
Functions in C are categorized based on arguments and return value:
No arguments, no return value
Arguments, no return value
No arguments, return value
Edited by:- LINGAMURTHY K 12
Arguments and return value
Example (Arguments and Return Value):
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int main()
{
int sum = add(5, 3);
printf("Sum = %d\n", sum);
return 0;
}
8. Write a program with functions that do not communicate any data between them
(without passing without return).
#include <stdio.h>
void sum()
int a = 10, b = 20;
printf("Sum = %d\n", a + b);
int main()
sum();
return 0;
}
9. Write a program with functions that communicate data between them (with
passing with return).
#include <stdio.h>
int sum(int a, int b)
{
Edited by:- LINGAMURTHY K 13
return a + b;
}
int main()
{
int x, y, result;
printf("Enter two numbers: ");
scanf("%d %d", &x, &y);
result = sum(x, y);
printf("Sum = %d\n", result);
return 0;
}
10. Write a program to find the GCD of two numbers using a recursion function.
#include <stdio.h>
int gcd(int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
int main()
{
int x, y;
printf("Enter two numbers: ");
scanf("%d %d", &x, &y);
printf("GCD = %d\n", gcd(x, y));
return 0;
}
Module-5
1. Explain structures in C. How is a structure defined, declared, and accessed?
Provide a suitable example.
Structures in C are user-defined data types that allow grouping related data items of different data types
under a single name.
Definition, Declaration, and Access
Definition: A structure is defined using the struct keyword, which creates a template for the structure.
Declaration: Structure variables are declared either during definition or separately using the defined
template.
Edited by:- LINGAMURTHY K 14
Access: Members of a structure are accessed using the dot (.) operator (member access operator) with
the structure variable.
Example:
#include <stdio.h>
#include <string.h>
// Structure definition
struct Student {
int roll_no;
float percentage;
char name[20];
};
int main() {
// Structure variable declaration and initialization
struct Student s1;
// Accessing and assigning values to members
s1.roll_no = 101;
[Link] = 90.5;
strcpy([Link], "Alice");
// Accessing and printing values
printf("Student Name: %s\n", [Link]);
printf("Roll No: %d\n", s1.roll_no);
printf("Percentage: %.2f\n", [Link]);
return 0;
2. What is an array of structures? Explain how to declare, initialize, and
access array elements with an example program.
An array of structures is a collection of structure variables, where each element of the array is a
structure variable that holds data members of various types.
Edited by:- LINGAMURTHY K 15
Declaration, Initialization, and Access
Declaration: Declared like a normal array, specifying the structure type and the size of the array. For
example, struct Student students[10];
Initialization: Can be initialized during declaration using nested curly braces, or individually after
declaration.
Access: Individual structure elements are accessed using the array index, and structure members are
accessed using the dot (.) operator. For example, students[0].roll_no.
Example
#include <stdio.h>
struct Point {
int x;
int y;
};
int main( ) {
// Array of structures declaration and initialization
struct Point points[3] = {{1, 2}, {3, 4}, {5, 6}};
// Accessing and printing elements
for (int i = 0; i < 3; i++) {
printf("Point %d: (%d, %d)\n", i + 1, points[i].x, points[i].y);
}
return 0;
}
3. Write a program to illustrate the comparison of structure variables.
#include <stdio.h>
struct Point {
int x;
int y;
};
int main() {
struct Point p1 = {10, 20};
struct Point p2 = {10, 20};
struct Point p3 = {30, 40};
// Compare p1 and p2
if (p1.x == p2.x && p1.y == p2.y) {
printf("p1 and p2 are equal\n");
} else {
printf("p1 and p2 are not equal\n");
}
// Compare p1 and p3
if (p1.x == p3.x && p1.y == p3.y) {
printf("p1 and p3 are equal\n");
Edited by:- LINGAMURTHY K 16
} else {
printf("p1 and p3 are not equal\n");
}
return 0;
}
4. Write a program to calculate the subject-wise and student wise totals and
store them as a part of the structure.
#include <stdio.h>
struct Student {
char name[50];
int marks[3]; // Marks for 3 subjects
int total;
float average;
};
int main() {
struct Student s;
int i;
printf("Enter student name: ");
scanf("%s", [Link]);
printf("Enter marks for 3 subjects:\n");
for (i = 0; i < 3; i++) {
printf("Subject %d: ", i + 1);
scanf("%d", &[Link][i]);
[Link] = 0;
for (i = 0; i < 3; i++) {
[Link] += [Link][i];
[Link] = (float)[Link] / 3;
Edited by:- LINGAMURTHY K 17
printf("\n--- Student Details ---\n");
printf("Name: %s\n", [Link]);
printf("Total Marks: %d\n", [Link]);
printf("Average Marks: %.2f\n", [Link]);
return 0;
5. What are pointers? Explain pointer declaration, initialization, and
accessing variables using pointers with a proper example
Pointers are variables that store the memory address of another variable.
Declaration, Initialization, and Access
Declaration: Declared using the asterisk (*) symbol before the variable name. For example, int *ptr;
declares a pointer to an integer.
Initialization: A pointer is initialized with the address of another variable using the address-of operator
(&). For example, ptr = &var;.
Access (Dereferencing): The value of the variable pointed to by a pointer is accessed using the
dereference operator (*). For example, *ptr gives the value stored at the address in ptr.
Example
#include <stdio.h>
int main() {
int var = 10; // A normal integer variable
int *ptr; // Pointer declaration
ptr = &var; // Pointer initialization (storing address of var)
printf("Value of var: %d\n", var);
printf("Address of var: %p\n", &var);
printf("Value of ptr (address of var): %p\n", ptr);
printf("Value pointed to by ptr (*ptr): %d\n", *ptr);
// Modifying value using pointer
*ptr = 20;
printf("Value of var after modification: %d\n", var);
return 0;
}
6. Write a program to calculate the area of a circle using pointers.
#include <stdio.h
int main() {
Edited by:- LINGAMURTHY K 18
float radius;
float area;
float *ptr_radius;
ptr_radius = &radius; // Store the address of radius
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
// Calculate area using the pointer
area = 3.14159 * (*ptr_radius) * (*ptr_radius);
printf("The area of the circle is: %.2f\n", area);
return 0;
}
7. Define pointer and explain declaration of a pointer to a variable.
A pointer is a variable that holds the memory address of another variable.
Explanation of Pointer Declaration
Pointers are declared using the asterisk (*) operator.
The syntax is data_type *pointer_name;.
data_type specifies the type of the variable whose address the pointer will hold (e.g., int *ptr; holds the
address of an integer variable).
The asterisk indicates that it's a pointer variable, not a normal variable of that data type.
The pointer itself stores an address, which is typically a large integer value, but it is type-aware to ensure
correct memory access and manipulation.
Edited by:- LINGAMURTHY K 19