maths

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

1.(A) #include <stdio.

h>

#include <stdlib.h>

#include <string.h>

// Structure to represent a day of the week

struct Day {

char *name;

int date;

char *description;

};

// Function to create a dynamic array of days

struct Day* createCalendar(int size) {

struct Day* calendar = (struct Day*)malloc(size * sizeof(struct Day));

if (calendar == NULL) {

fprintf(stderr, "Memory allocation failed!\n");

exit(1);

// Initialize each day with dynamic memory for name and description

for (int i = 0; i < size; i++) {

calendar[i].name = (char*)malloc(20 * sizeof(char));

calendar[i].description = (char*)malloc(100 * sizeof(char));

return calendar;

int main() {

int numDays = 7;

struct Day* myCalendar = createCalendar(numDays);

// Example: Initialize some days of the week

strcpy(myCalendar[0].name, "Sunday");

myCalendar[0].date = 1;

strcpy(myCalendar[0].description, "Rest day");


strcpy(myCalendar[1].name, "Monday");

myCalendar[1].date = 2;

strcpy(myCalendar[1].description, "Go to work");

// ... (Initialize other days)

// Free the dynamically allocated memory

for (int i = 0; i < numDays; i++) {

free(myCalendar[i].name);

free(myCalendar[i].description);

free(myCalendar);

return 0;

1(B) #include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX_DAYS 7

struct Day {

char *name;

int date;

char *description;

};

struct Day* createCalendar() {

struct Day* calendar = (struct Day*)malloc(MAX_DAYS * sizeof(struct Day));

if (calendar == NULL) {

fprintf(stderr, "Memory allocation failed!\n");

exit(1);

for (int i = 0; i < MAX_DAYS; i++) {

calendar[i].name = (char*)malloc(20 * sizeof(char));

calendar[i].description = (char*)malloc(100 * sizeof(char));


} return calendar;

void readData(struct Day* calendar) {

for (int i = 0; i < MAX_DAYS; i++) {

printf("Enter name of day %d: ", i + 1);

scanf("%s", calendar[i].name);

printf("Enter date of day %d: ", i + 1);

scanf("%d", &calendar[i].date);

printf("Enter description for day %d: ", i + 1);

scanf(" %[^\n]", calendar[i].description); // Read entire line for description

void displayCalendar(struct Day* calendar) {

printf("\nWeekly Activity Report:\n");

for (int i = 0; i < MAX_DAYS; i++) {

printf("Day: %s\tDate: %d\tDescription: %s\n", calendar[i].name, calendar[i].date,


calendar[i].description);

int main() {

struct Day* myCalendar = createCalendar(MAX_DAYS);

readData(myCalendar);

displayCalendar(myCalendar);

// Free the dynamically allocated memory

for (int i = 0; i < MAX_DAYS; i++) {

free(myCalendar[i].name);

free(myCalendar[i].description);

free(myCalendar);

return 0; {
3 . #include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX_SIZE 100

// Structure to represent a stack

struct Stack {

int top;

int arr[MAX_SIZE];

};

// Function to create a new stack

struct Stack* createStack() {

struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));

stack->top = -1;

return stack;

// Function to check if the stack is empty

int isEmpty(struct Stack* stack) {

return stack->top == -1;

// Function to check if the stack is full

int isFull(struct Stack* stack) {

return stack->top == MAX_SIZE - 1;

// Function to push an element onto the stack

void push(struct Stack* stack, int item) {

if (isFull(stack)) {

printf("Stack Overflow\n");

return;

stack->arr[++stack->top] = item;

}
// Function to pop an element from the stack

int pop(struct Stack* stack) {

if (isEmpty(stack)) {

printf("Stack Underflow\n");

return -1;

return stack->arr[stack->top--];

// Function to check if a string is a palindrome using stack

int isPalindrome(char* str) {

struct Stack* stack = createStack();

int len = strlen(str);

int mid = len / 2;

// Push first half of the string onto the stack

for (int i = 0; i < mid; i++) {

push(stack, str[i]);

// If the string has odd number of characters, skip the middle character

if (len % 2 != 0) {

mid++;

// Compare characters from the second half with popped characters

for (int i = mid; i < len; i++) {

if (str[i] != pop(stack)) {

return 0; // Not a palindrome

return 1; // Palindrome

// Function to display the contents of the stack

void display(struct Stack* stack) {

if (isEmpty(stack)) {
printf("Stack is empty\n");

return;

printf("Stack: ");

for (int i = stack->top; i >= 0; i--) {

printf("%d ", stack->arr[i]);

printf("\n");

int main() {

struct Stack* stack = createStack();

int choice, item;

char str[100];

do {

printf("\nStack Operations\n");

printf("1. Push\n");

printf("2. Pop\n");

printf("3. Check Palindrome\n");

printf("4. Display\n");

printf("5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter element to push: ");

scanf("%d", &item);

push(stack, item);

break;

case 2:

item = pop(stack);
if (item != -1) {

printf("Popped element: %d\n", item);

break;

case 3:

printf("Enter string: ");

scanf("%s", str);

if (isPalindrome(str)) {

printf("The string is a palindrome.\n");

} else {

printf("The string is not a palindrome.\n");

break;

case 4:

display(stack);

break;

case 5:

printf("Exiting...\n");

break;

default:

printf("Invalid choice\n");

} while (choice != 5);

return 0;

4 . #include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX_SIZE 100


// Structure to represent a stack

struct Stack {

int top;

char arr[MAX_SIZE];

};

// Function to create a new stack

struct Stack* createStack() {

struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));

stack->top = -1;

return stack;

// Function to check if the stack is empty

int isEmpty(struct Stack* stack) {

return stack->top == -1;

// Function to check if the stack is full

int isFull(struct Stack* stack) {

return stack->top == MAX_SIZE - 1;

// Function to push an element onto the stack

void push(struct Stack* stack, char item) {

if (isFull(stack)) {

printf("Stack Overflow\n");

return;

stack->arr[++stack->top] = item;

// Function to pop an element from the stack

char pop(struct Stack* stack) {

if (isEmpty(stack)) {

printf("Stack Underflow\n");
return -1;

return stack->arr[stack->top--];

// Function to get precedence of an operator

int precedence(char op) {

switch (op) {

case '+':

case '-':

return 1;

case '*':

case '/':

case '%':

return 2;

case '^':

return 3;

default:

return -1;

// Function to check if a character is an operator

int isOperator(char ch) {

return (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%' || ch == '^');

// Function to convert infix expression to postfix expression

void infixToPostfix(char* infix, char* postfix) {

struct Stack* stack = createStack();

int i, j;

for (i = 0, j = 0; infix[i] != '\0'; i++) {

if (isalnum(infix[i])) {

postfix[j++] = infix[i];
} else if (infix[i] == '(') {

push(stack, '(');

} else if (infix[i] == ')') {

while (!isEmpty(stack) && stack->arr[stack->top] != '(') {

postfix[j++] = pop(stack);

if (!isEmpty(stack) && stack->arr[stack->top] != '(') {

printf("Invalid Expression\n");

return;

} else {

pop(stack); // Remove '('

} else if (isOperator(infix[i])) {

while (!isEmpty(stack) && precedence(infix[i]) <= precedence(stack->arr[stack->top])) {

postfix[j++] = pop(stack);

push(stack, infix[i]);

while (!isEmpty(stack)) {

postfix[j++] = pop(stack);

postfix[j] = '\0';

int main() {

char infix[100], postfix[100];

printf("Enter the infix expression: ");

scanf("%s", infix);
infixToPostfix(infix, postfix);

printf("Postfix expression: %s\n", postfix);

return 0;

5 . #include <stdio.h>

#include <stdlib.h>

#include <math.h>

#include <string.h>

// Define the maximum stack size

#define MAX_SIZE 100

// Structure to represent a stack

struct Stack {

int top;

int arr[MAX_SIZE];

};

// Function to create a new stack

struct Stack* createStack() {

struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));

stack->top = -1;

return stack;

// Function to check if the stack is empty

int isEmpty(struct Stack* stack) {

return stack->top == -1;

// Function to check if the stack is full

int isFull(struct Stack* stack) {

return stack->top == MAX_SIZE - 1;

// Function to push an element onto the stack


void push(struct Stack* stack, int item) {

if (isFull(stack)) {

printf("Stack Overflow\n");

return;

stack->arr[++stack->top] = item;

// Function to pop an element from the stack

int pop(struct Stack* stack) {

if (isEmpty(stack)) {

printf("Stack Underflow\n");

return -1;

return stack->arr[stack->top--];

// Function to evaluate a suffix expression

int evaluateSuffix(char* exp) {

struct Stack* stack = createStack();

int i;

for (i = 0; exp[i] != '\0'; i++) {

if (isdigit(exp[i])) {

push(stack, exp[i] - '0'); // Convert character to integer

} else {

int val1 = pop(stack);

int val2 = pop(stack);

switch (exp[i]) {

case '+':

push(stack, val2 + val1);

break;

case '-':
push(stack, val2 - val1);

break;

case '*':

push(stack, val2 * val1);

break;

case '/':

push(stack, val2 / val1);

break;

case '%':

push(stack, val2 % val1);

break;

case '^':

push(stack, pow(val2, val1));

break;

return pop(stack);

// Function to solve the Tower of Hanoi problem

void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) {

if (n == 0) {

return;

towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);

printf("Move disk %d from rod %c to rod %c\n", n, from_rod, to_rod);

towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);

int main() {

// Evaluate suffix expression

char exp[] = "23*5+";


printf("Evaluation of suffix expression: %d\n", evaluateSuffix(exp));

// Solve Tower of Hanoi

int n = 3; // Number of disks

towerOfHanoi(n, 'A', 'C', 'B');

return 0;

6 . #include <stdio.h>

#include <stdlib.h>

#define MAX 5

// Structure to represent a Circular Queue

struct CQueue {

char items[MAX];

int front, rear;

};

// Function to create a new Circular Queue

struct CQueue* createQueue() {

struct CQueue* q = (struct CQueue*)malloc(sizeof(struct CQueue));

q->front = -1;

q->rear = -1;

return q;

// Function to check if the Circular Queue is empty

int isEmpty(struct CQueue* q) {

return (q->front == -1);

// Function to check if the Circular Queue is full

int isFull(struct CQueue* q) {

return ((q->front == 0 && q->rear == MAX - 1) || (q->front == q->rear + 1));

// Function to insert an element into the Circular Queue


void enqueue(struct CQueue* q, char item) {

if (isFull(q)) {

printf("Queue Overflow\n");

return;

if (isEmpty(q)) {

q->front = q->rear = 0;

} else {

q->rear = (q->rear + 1) % MAX;

q->items[q->rear] = item;

// Function to delete an element from the Circular Queue

char dequeue(struct CQueue* q) {

if (isEmpty(q)) {

printf("Queue Underflow\n");

return '\0';

char item = q->items[q->front];

if (q->front == q->rear) {

q->front = q->rear = -1;

} else {

q->front = (q->front + 1) % MAX;

return item;

// Function to display the status of the Circular Queue

void display(struct CQueue* q) {

if (isEmpty(q)) {

printf("Queue is empty\n");

return;
}

printf("Queue: ");

int i = q->front;

do {

printf("%c ", q->items[i]);

i = (i + 1) % MAX;

} while (i != (q->rear + 1) % MAX);

printf("\n");

int main() {

struct CQueue* q = createQueue();

int choice;

char item;

do {

printf("\nCircular Queue Operations\n");

printf("1. Enqueue\n");

printf("2. Dequeue\n");

printf("3. Display\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter element to enqueue: ");

scanf(" %c", &item);

enqueue(q, item);

break;

case 2:

item = dequeue(q);

if (item != '\0') {

printf("Dequeued element: %c\n", item);


}

break;

case 3:

display(q);

break;

case 4:

printf("Exiting...\n");

break;

default:

printf("Invalid choice\n");

} while (choice != 4);

return 0;

11 . #include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

#define MAX_VERTICES 100

// Structure to represent a graph

struct Graph {

int V; // Number of vertices

int adj[MAX_VERTICES][MAX_VERTICES]; // Adjacency matrix

};

// Function to create a graph

struct Graph* createGraph(int V) {

struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));

graph->V = V;

for (int i = 0; i < V; ++i) {

for (int j = 0; j < V; ++j) {

graph->adj[i][j] = 0;
}

return graph;

// Function to add an edge to the graph

void addEdge(struct Graph* graph, int src, int dest) {

graph->adj[src][dest] = 1;

// For undirected graph, add an edge in both directions

// graph->adj[dest][src] = 1;

// Function to print the adjacency matrix

void printGraph(struct Graph* graph) {

for (int i = 0; i < graph->V; ++i) {

for (int j = 0; j < graph->V; ++j) {

printf("%d ", graph->adj[i][j]);

printf("\n");

// Function to perform BFS traversal

void BFS(struct Graph* graph, int startVertex) {

bool visited[graph->V];

for (int i = 0; i < graph->V; ++i) {

visited[i] = false;

int queue[graph->V];

int front = 0, rear = -1;

visited[startVertex] = true;

queue[++rear] = startVertex;

printf("BFS traversal starting from vertex %d: ", startVertex);


while (front <= rear) {

int vertex = queue[front++];

printf("%d ", vertex);

for (int j = 0; j < graph->V; ++j) {

if (graph->adj[vertex][j] == 1 && !visited[j]) {

visited[j] = true;

queue[++rear] = j;

printf("\n");

// Function to perform DFS traversal (recursive)

void DFSUtil(struct Graph* graph, int vertex, bool visited[]) {

visited[vertex] = true;

printf("%d ", vertex);

for (int j = 0; j < graph->V; ++j) {

if (graph->adj[vertex][j] == 1 && !visited[j]) {

DFSUtil(graph, j, visited);

// Function to check if the graph is connected using DFS

bool isConnected(struct Graph* graph) {

bool visited[graph->V];

for (int i = 0; i < graph->V; ++i) {

visited[i] = false;

DFSUtil(graph, 0, visited); // Start DFS from vertex 0

// Check if all vertices are visited

for (int i = 0; i < graph->V; ++i) {


if (!visited[i]) {

return false;

return true;

int main() {

int V, E, src, dest;

printf("Enter the number of vertices: ");

scanf("%d", &V);

struct Graph* graph = createGraph(V);

printf("Enter the number of edges: ");

scanf("%d", &E);

printf("Enter the edges (source and destination):\n");

for (int i = 0; i < E; ++i) {

scanf("%d %d", &src, &dest);

addEdge(graph, src, dest);

printf("\nAdjacency Matrix:\n");

printGraph(graph);

printf("\nEnter the starting vertex for BFS: ");

scanf("%d", &src);

BFS(graph, src);

if (isConnected(graph)) {

printf("The graph is connected.\n");

} else {

printf("The graph is not connected.\n");

return 0;

You might also like