Professional Documents
Culture Documents
Ict 107 - Dsa - Notes
Ict 107 - Dsa - Notes
**Problem 1:**
#include <stdio.h>
int main() {
int sum = 0;
sum += arr[i];
return 0;
**Problem 2:**
#include <stdio.h>
*min = arr[i];
*max = arr[i];
int main() {
int arr[] = {12, 5, 7, 19, 22, 8};
return 0;
```
**Problem 3:**
Write a C program to implement a matrix multiplication function that takes two matrices as input and returns the result. Ensure proper error
handling for incompatible matrix sizes.
```c
#include <stdio.h>
void multiplyMatrices(int A[][3], int B[][2], int result[][2], int m, int n, int p) {
if (n != p) {
return;
result[i][j] = 0;
int main() {
int result[2][2];
printf("Resultant Matrix:\n");
printf("\n");
return 0;
**Problem 4:**
Write a C program that finds the second largest element in an array of integers.
```c
#include <stdio.h>
firstMax = arr[0];
secondMax = arr[1];
} else {
firstMax = arr[1];
secondMax = arr[0];
secondMax = firstMax;
firstMax = arr[i];
} else if (arr[i] > secondMax && arr[i] != firstMax) {
secondMax = arr[i];
return secondMax;
int main() {
int size;
scanf("%d", &size);
int array[size];
scanf("%d", &array[i]);
return 0;
```
**Problem 5:**
Write a C program that takes an array of integers as input and finds the sum and product of the elements.
**Solution:**
```c
#include <stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int array[size];
scanf("%d", &array[i]);
sum += array[i];
product *= array[i];
return 0;
}
POINTERS
**Problem 1:**
```c
#include <stdio.h>
*a = *b;
*b = temp;
int main() {
int x = 5, y = 10;
swap(&x, &y);
return 0;
**Problem 2:**
```c
#include <stdio.h>
ptr++;
}
int main() {
return 0;
**Problem 3:**
Write a C program to implement a function that receives an array of integers and a function pointer. The function should use the function pointer
to perform a specific operation (e.g., square, cube, etc.) on each element of the array and print the modified array.
```c
#include <stdio.h>
arr[i] = operation(arr[i]);
int square(int x) {
return x * x;
int cube(int x) {
return x * x * x;
int main() {
printf("Original Array:\n");
for (int i = 0; i < size; i++) {
return 0;
```
**Problem 4:**
```c
#include <stdio.h>
*a = *b;
*b = temp;
int main() {
printf("Number 1: ");
scanf("%d", &num1);
printf("Number 2: ");
scanf("%d", &num2);
swap(&num1, &num2);
return 0;
```
**Problem 5:**
Write a C program that takes an integer input, calculates its square and cube using pointers, and prints the results.
```c
#include <stdio.h>
int main() {
scanf("%d", &num);
return 0;
```
QUEUES
**Problem 1:**
Implement a simple queue in C using an array. Include functions for enqueue and dequeue operations.
```c
#include <stdio.h>
#define MAX_SIZE 5
int queue[MAX_SIZE];
if (rear == MAX_SIZE - 1) {
return;
if (front == -1)
front = 0;
queue[++rear] = item;
int dequeue() {
if (front == -1) {
return -1;
return item;
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
dequeue();
dequeue();
dequeue();
return 0;
```
**Problem 2:**
Implement a circular queue in C using an array. Include functions for enqueue and dequeue operations.
```c
#include <stdio.h>
#define MAX_SIZE 5
int queue[MAX_SIZE];
return;
if (front == -1)
front = 0;
rear = (rear + 1) % MAX_SIZE;
queue[rear] = item;
int dequeue() {
if (front == -1) {
return -1;
if (front == rear)
else
return item;
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
dequeue();
dequeue();
dequeue();
return 0;
```
**Problem 3:**
Implement a priority queue in C using a linked list. Include functions for enqueue with priority and dequeue operations.
```c
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
int priority;
};
if (newNode == NULL) {
return;
newNode->data = item;
newNode->priority = priority;
newNode->next = front;
front = newNode;
} else {
temp = temp->next;
newNode->next = temp->next;
temp->next = newNode;
printf("%d enqueued to the priority queue with priority %d\n", item, priority);
int dequeue() {
if (front == NULL) {
return -1;
front = front->next;
free(temp);
return item;
int main() {
enqueueWithPriority(10, 2);
enqueueWithPriority(20, 1);
enqueueWithPriority(30, 3);
dequeue();
dequeue();
dequeue();
return 0;
```
STACKS
**Problem 1:**
Implement a stack in C using an array. Include functions for push and pop operations.
```c
#include <stdio.h>
#define MAX_SIZE 5
int stack[MAX_SIZE];
if (top == MAX_SIZE - 1) {
return;
stack[++top] = item;
int pop() {
if (top == -1) {
return -1;
return item;
int main() {
push(10);
push(20);
push(30);
pop();
pop();
pop();
return 0;
```
**Problem 2**
Implement a stack in C using linked lists. Include functions for push and pop operations.
```c
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
if (newNode == NULL) {
return;
newNode->data = item;
newNode->next = top;
top = newNode;
int pop() {
if (top == NULL) {
return -1;
top = top->next;
free(temp);
return item;
int main() {
push(10);
push(20);
push(30);
pop();
pop();
pop();
return 0;
```
**Problem 3:**
Write a C program to evaluate a postfix expression using a stack. The postfix expression is provided as an array of strings, where each element is
either an operand or an operator.
```c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int top;
int items[MAX_SIZE];
};
if (stack->top == MAX_SIZE - 1) {
return;
stack->items[++stack->top] = item;
if (stack->top == -1) {
return -1;
return stack->items[stack->top--];
stack.top = -1;
if (isdigit(expression[i][0])) {
push(&stack, atoi(expression[i]));
} else {
switch (expression[i][0]) {
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
break;
return pop(&stack);
int main() {
return 0;
```
STRUCTURES
**Problem 1:**
Define a structure named "Person" with members name, age, and address. Write a C program to create an array of 3 Person structures and print
their details.
```c
#include <stdio.h>
struct Person {
char name[50];
int age;
char address[100];
};
int main() {
printf("Name: ");
scanf("%s", people[i].name);
printf("Age: ");
scanf("%d", &people[i].age);
printf("Address: ");
scanf("%s", people[i].address);
printf("\nDetails of People:\n");
printf("\n");
}
return 0;
```
**Problem 2**
Create a structure named "Employee" with members name, salary, and experience. Write a C program to read details of n employees and
display the names of employees with more than 5 years of experience and a salary greater than 50000
```c
#include <stdio.h>
struct Employee {
char name[50];
float salary;
int experience;
};
int main() {
int n;
scanf("%d", &n);
printf("Name: ");
scanf("%s", employees[i].name);
printf("Salary: ");
scanf("%f", &employees[i].salary);
scanf("%d", &employees[i].experience);
}
// Display names of employees with more than 5 years of experience and salary > 50000
printf("\nEmployees with more than 5 years of experience and salary > 50000:\n");
printf("%s\n", employees[i].name);
return 0;
**Problem 3**
Create a structure named "Book" with members title, author, and price. Write a C program to create an array of 5 Book structures, sort them
based on price in ascending order, and then print the details.
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Book {
char title[100];
char author[100];
float price;
};
int main() {
struct Book books[5];
printf("Title: ");
scanf("%s", books[i].title);
printf("Author: ");
scanf("%s", books[i].author);
printf("Price: ");
scanf("%f", &books[i].price);
printf("\n");
return 0;
```
**Problem 4**
Create a C program that defines a structure representing a book with attributes like title, author, and price. Allow the user to input information
for two books and display the details of the book with the higher price.
```c
#include <stdio.h>
struct Book {
char title[100];
char author[50];
float price;
};
int main() {
printf("Title: ");
scanf("%s", book1.title);
printf("Author: ");
scanf("%s", book1.author);
printf("Price: ");
scanf("%f", &book1.price);
printf("Title: ");
scanf("%s", book2.title);
printf("Author: ");
scanf("%s", book2.author);
printf("Price: ");
scanf("%f", &book2.price);
} else {
return 0;
}
COMBINED
**Problem 1:**
Write a C program that takes an array of integers as input, and using pointers, find and print the sum of the even elements in the array.
```c
#include <stdio.h>
int main() {
int sum = 0;
if (*ptr % 2 == 0) {
sum += *ptr;
ptr++;
return 0;
```
**Problem 2:**
Create a structure named "Student" with members name and marks. Write a C program that uses pointers to an array of structures to find and
print the name of the student with the highest marks.
```c
#include <stdio.h>
struct Student {
char name[50];
float marks;
};
int main() {
float maxMarks = 0;
char maxName[50];
maxMarks = ptr->marks;
strcpy(maxName, ptr->name);
ptr++;
return 0;
```
**Problem 3:**
Implement a queue in C using an array of structures. Each element in the queue represents a book with details such as title, author, and
publication year. Include functions for enqueue and dequeue operations.
```c
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 5
struct Book {
char title[100];
char author[100];
int year;
};
struct Queue {
};
return;
if (q->front == q->rear) {
return emptyBook;
return book;
int main() {
struct Book book1 = {"The Catcher in the Rye", "J.D. Salinger", 1951};
struct Book book2 = {"To Kill a Mockingbird", "Harper Lee", 1960};
enqueue(&bookQueue, &book1);
enqueue(&bookQueue, &book2);
return 0;
```
**Problem 4:**
```c
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 50
struct Stack {
char items[MAX_SIZE];
int top;
};
if (stack->top == MAX_SIZE - 1) {
return;
stack->items[++stack->top] = item;
}
if (stack->top == -1) {
return '\0';
return stack->items[stack->top--];
push(&charStack, str[i]);
str[i] = pop(&charStack);
int main() {
char str[MAX_SIZE];
scanf("%s", str);
reverseString(str);
return 0;
```
Create a structure named "Rectangle" with members length and width. Write a C program that uses an array of structures to find and print the
area of the rectangle with the maximum area.
```c
#include <stdio.h>
struct Rectangle {
float length;
float width;
};
int main() {
int maxIndex = 0;
maxIndex = i;
return 0;
```
**Problem 1:**
Create a structure named "Student" with members name, age, and marks. Write a C program that takes an array of students as input and finds
and prints the student with the highest marks.
```c
#include <stdio.h>
struct Student {
char name[50];
int age;
float marks;
};
int main() {
struct Student students[] = {{"Alice", 20, 85.5}, {"Bob", 22, 92.0}, {"Charlie", 21, 78.3}};
int maxIndex = 0;
maxIndex = i;
return 0;
```
**Problem 2:**
Write a C program to implement a function that takes an array of integers and a pointer to a function. The function pointer should determine
whether each element is even or odd, and the program should print the count of even and odd numbers in the array.
```c
#include <stdio.h>
evenCount++;
} else {
oddCount++;
int isEven(int x) {
return x % 2 == 0;
int main() {
return 0;
```
**Problem 3:**
Implement a priority queue in C using a linked list of structures. Each element in the queue represents a task with details such as priority and
description. Include functions for enqueue with priority and dequeue operations.
```c
#include <stdio.h>
#include <stdlib.h>
struct Task {
int priority;
char description[100];
if (newTask == NULL) {
return;
newTask->priority = priority;
strcpy(newTask->description, description);
newTask->next = NULL;
newTask->next = front;
front = newTask;
} else {
temp = temp->next;
newTask->next = temp->next;
temp->next = newTask;
void dequeue() {
if (front == NULL) {
return;
front = front->next;
free(temp);
int main() {
dequeue();
dequeue();
dequeue();
return 0;
```
**Problem 4:**
Create a structure named "Person" with members name, age, and address. Write a C program that uses a stack implemented with an array of
structures to reverse the order of people in a list.
```c
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 5
struct Person {
char name[50];
int age;
char address[100];
};
struct Stack {
int top;
};
if (stack->top == MAX_SIZE - 1) {
return;
stack->items[++stack->top] = *person;
if (stack->top == -1) {
return emptyPerson;
return stack->items[stack->top--];
int main() {
push(&personStack, &person1);
push(&personStack, &person2);
push(&personStack, &person3);
printf("Original Order:\n");
printf("%s\n", personStack.items[i].name);
}
printf("\nReversed Order:\n");
printf("%s\n", pop(&personStack).name);
return 0;
```
**Problem 5:**
Create a structure named "Rectangle" with members length and width. Write a C program that uses pointers to an array of structures to find
and print the area of the rectangle with the maximum area.
```c
#include <stdio.h>
struct Rectangle {
float length;
float width;
};
int main() {
int maxIndex = 0;
maxIndex)->width) {
maxIndex = i;
}
printf("Rectangle with the maximum area:\n");
return 0;
```
**Problem:**
Write a C program that takes two integer arrays of the same length as input and calculates the dot product of the two arrays using pointers.
**Solution:**
```c
#include <stdio.h>
int dotProduct = 0;
return dotProduct;
int main() {
int size;
scanf("%d", &size);
scanf("%d", &array1[i]);
scanf("%d", &array2[i]);
return 0;
```
**Problem:**
Create a program that manages a student database using a structure. Allow the user to input information for multiple students, including name,
age, and grades. Then, calculate the average grade for each student and display the student with the highest average.
**Solution:**
```c
#include <stdio.h>
struct Student {
char name[50];
int age;
float grades[5];
float averageGrade;
};
sum += student->grades[i];
student->averageGrade = sum / 5;
int main() {
int numStudents;
scanf("%d", &numStudents);
printf("Name: ");
scanf("%s", students[i].name);
printf("Age: ");
scanf("%d", &students[i].age);
scanf("%f", &students[i].grades[j]);
calculateAverage(&students[i]);
// Find and display the student with the highest average grade
int maxIndex = 0;
maxIndex = i;
}
return 0;
```
**Problem:**
Implement a circular queue in C using an array to simulate a printer queue. Each element in the queue represents a print job with details such as
document name and the number of pages. Include functions for enqueue and dequeue operations.
**Solution:**
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
struct PrintJob {
char documentName[50];
int numPages;
};
struct Queue {
};
return;
}
q->items[q->rear] = *job;
if (q->front == q->rear) {
return emptyJob;
return job;
int main() {
enqueue(&printerQueue, &job1);
enqueue(&printerQueue, &job2);
enqueue(&printerQueue, &job3);
dequeue(&printerQueue);
dequeue(&printerQueue);
dequeue(&printerQueue);
return 0;
```
**Problem:**
Create a program to check whether a given expression containing parentheses is balanced using a stack. The program should handle multiple
types of parentheses, including (), [], and {}.
**Solution:**
```c
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 50
struct Stack {
char items[MAX_SIZE];
int top;
};
if (stack->top == MAX_SIZE - 1) {
return;
stack->items[++stack->top] = item;
if (stack->top == -1) {
return '\0';
return stack->items[stack->top--];
}
int isMatchingPair(char opening, char closing) {
push(&charStack, expression[i]);
return 0; // Unbalanced
int main() {
char expression[MAX_SIZE];
scanf("%s", expression);
if (isBalanced(expression)) {
} else {
return 0;
```
### Problem 5: Structures and Arrays
**Problem:**
Create a program to manage a library catalog using structures. Each book in the catalog has details such as title, author, and ISBN. Allow the
user to input information for multiple books, then display the details of books with titles longer than 20 characters.
**Solution:**
```c
#include <stdio.h>
struct Book {
char title[100];
char author[50];
char isbn[13];
};
int main() {
int numBooks;
scanf("%d", &numBooks);
printf("Title: ");
scanf("%s", libraryCatalog[i].title);
printf("Author: ");
scanf("%s", libraryCatalog[i].author);
printf("ISBN: ");
scanf("%s", libraryCatalog[i].isbn);
}
printf("\nBooks with titles longer than 20 characters:\n");
printf("-----------------------------\n");
return 0;
```
**Problem:**
Create a C program that uses a structure to represent a point in 2D space (x, y). Allow the user to input the coordinates of two points, and
calculate and display the distance between them.
**Solution:**
```c
#include <stdio.h>
#include <math.h>
struct Point {
float x;
float y;
};
int main() {
printf("x: ");
scanf("%f", &point1.x);
printf("y: ");
scanf("%f", &point1.y);
printf("x: ");
scanf("%f", &point2.x);
printf("y: ");
scanf("%f", &point2.y);
return 0;
```
**Problem:**
Write a C program that calculates and prints the factorial of a given positive integer using a recursive function.
**Solution:**
```c
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
if (num < 0) {
} else {
return 0;
```
**Problem:**
Write a C program that takes an array of integers as input, and a function to find and print the sum of the array elements.
**Solution:**
```c
#include <stdio.h>
int sum = 0;
sum += arr[i];
return sum;
int main() {
int size;
scanf("%d", &size);
int array[size];
scanf("%d", &array[i]);
return 0;
```
**Problem:**
Create a C program that uses a structure to represent a rectangle. Allow the user to input the length and width of multiple rectangles, and use
pointers to calculate and display the area of each rectangle.
**Solution:**
```c
#include <stdio.h>
struct Rectangle {
float length;
float width;
};
rect->length *= 2;
rect->width *= 2;
int main() {
int numRectangles;
printf("Enter the number of rectangles: ");
scanf("%d", &numRectangles);
printf("Length: ");
scanf("%f", &rectangles[i].length);
printf("Width: ");
scanf("%f", &rectangles[i].width);
calculateArea(&rectangles[i]);
printf("-----------------------------\n");
return 0;
```
**Problem:**
Write a C program that uses a function to find and print the average of an array of floating-point numbers using pointers.
**Solution:**
```c
#include <stdio.h>
int main() {
int size;
scanf("%d", &size);
float array[size];
scanf("%f", &array[i]);
return 0;
```
### Problem 13: Arrays, Pointers, and Functions
**Problem:**
Write a C program that takes an array of integers as input and a function to check whether the array is sorted in ascending order. If not, the
program should sort the array using pointers.
**Solution:**
```c
#include <stdio.h>
printf("\n");
return 1; // Sorted
*(arr + j + 1) = temp;
}
int main() {
int size;
scanf("%d", &size);
int array[size];
scanf("%d", &array[i]);
printArray(array, size);
if (!isSorted(array, size)) {
sortArray(array, size);
printArray(array, size);
} else {
return 0;
```
**Problem:**
Create a C program to manage a student database using structures. Allow the user to input information for multiple students, including name,
age, and grades. Use pointers to find and display the student with the highest average grade.
**Solution:**
```c
#include <stdio.h>
struct Student {
char name[50];
int age;
float grades[5];
float averageGrade;
};
float sum = 0;
sum += student->grades[i];
student->averageGrade = sum / 5;
int main() {
int numStudents;
scanf("%d", &numStudents);
printf("Name: ");
scanf("%s", students[i].name);
printf("Age: ");
scanf("%d", &students[i].age);
scanf("%f", &students[i].grades[j]);
}
calculateAverage(&students[i]);
// Find and display the student with the highest average grade
int maxIndex = 0;
maxIndex = i;
return 0;
```
**Problem:**
Implement a program that uses a stack to check whether a given arithmetic expression is well-formed, considering parentheses, square brackets,
and curly braces.
**Solution:**
```c
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 50
struct Stack {
char items[MAX_SIZE];
int top;
};
return;
stack->items[++stack->top] = item;
if (stack->top == -1) {
return '\0';
return stack->items[stack->top--];
push(&charStack, expression[i]);
}
int main() {
char expression[MAX_SIZE];
scanf("%s", expression);
if (isWellFormed(expression)) {
} else {
return 0;
```