CTSD HOME ASSIGNMENT-06 (Y.
Deepak-2100090151)
1. Consider the following typedef structure
typedef struct student{
int id;
char name[50];
int age;'
float height;
};
Develop a C program which accepts 10 students records and print those
record according to id
ANSWER:
#include <stdio.h>
struct student {
char firstName[50];
int age;
float height;
int roll;
} s[10];
int main() {
int i;
printf("**************Enter information ofstudents:**************");
for (i = 0; i < 10;++i)
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name\n: ");
scanf("%s", s[i].firstName);
printf("enter the age\n");
scanf("%d",&s[i].age);
printf("enter the height:");
scanf("%f",&s[i].height);
printf("**************Displaying Information**************");
for (i = 0; i < 10; ++i) {
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("age : %d",s[i].age);
printf("\n");
printf("height: %.1f", s[i].height);
return 0;}
2. Define a stack structure to store integers and develop a menu based
application to perform following operations on stack
a. push b. pop c. display d. count d. sum of elements e. Find minimum
element f. Find maximum element g. search for an element
ANSWER:
#include <stdio.h>
#include <stdlib.h>
#define size 10
void push();
void pop();
void display();
void count();
void sum();
void find_element();
void min();
void max();
int stack[size];
int top=-1;
void push(int value)
if(top==size-1)
printf("\n======stack is full!!! push not possible======\n");
else
top++;
stack[top]=value;
printf("\n=======insertion sucsessful=======\n");
}
void pop()
if(top==-1)
printf("\n=======stack is empty!!! pop is not possible=======\n");
else
printf("\n=======poped %d=======\n",stack[top]);
top--;
void display()
if(top==-1)
printf("\n=======stack is empty!!! pop is not possible=======\n");
else
int i;
printf("\nthe stack elements are:\n");
for(i=top;i>=0;i--)
printf("%d\n",stack[i]);
void count()
{
int c=0;
if(top==-1)
printf("\n=======stack is empty!!! c=%d========\n",c);
else
int i;
for(i=top;i>=0;i--)
c++;
printf("\nthe count is: %d\n",c);
void sum()
int sum=0;
if(top==-1)
printf("\n=======stack is empty!!! sum=%d========\n",sum);
else
int i;
for(i=top;i>=0;i--)
sum=sum+stack[i];
printf("\nthe sum is: %d\n",sum);
}
void find_element()
int k;
printf("enter the element to search: ");
scanf("%d",&k);
int flag =0;
if(top==-1)
printf("\n=======stack is empty!!!=========\n");
else
int i;
for(i=top;i>=0;i--)
if(stack[i]==k)
flag =1;
break;
if(flag==0)
printf("\nelement is not found in the satck\n");
else
printf("\nelement is found in the satck\n");
}
void min()
if(top==-1)
printf("\n=======stack is empty!!! pop is not possible=======\n");
else
int i,j,min=0;
for(i=top;i>=0;i--)
for(j=top;j>0;j--)
if(stack[j]<stack[i])
min=stack[j];
break;
printf("the minimum element is %d",min);
void max()
if(top==-1)
printf("\n=======stack is empty!!! pop is not possible=======\n");
else
{
int i,j,max=0;
for(i=top;i>=0;i--)
for(j=top;j>0;j--)
if(stack[j]>stack[i])
max=stack[j];
break;
printf("the maximum element is %d",max);
void main()
int value,choice;
while(1)
printf("\n***MENU***\n");
printf("\n1.push\n2.pop\n3.display\n4.count\n5.sum\n6.search for an element\n7.min\n8.max\n9.exit\n");
printf("\nenter your choice:");
scanf("%d",&choice);
switch(choice)
case 1:
printf("\nenter the value to be inserted:");
scanf("%d",&value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
count();
break;
case 5:
sum();
case 6:
find_element();
break;
case 7:
min();
break;
case 8:
max();
break;
case 9:
exit(0);
break;
default: printf("\n wrong selection!!! try again !!!");
}
3. Define a Queue data structure to store integers and develop a menu-based
application to perform following operations on queue
a. enqueue b. dequeue c. display d. count d. sum of elements e. Find minimum
element f. Find maximum element g. search for an element
answer:
#include <stdio.h>
#include <stdlib.h>
#define maxsize 5
void enqueue();
void dequeue();
void display();
void count();
void sum();
void find_element();
void min();
void max();
int front = -1, rear = -1;
int queue[maxsize];
void main()
int choice;
while(choice!=5)
printf("\n****MAIN MENU**");
printf("\n=========================================");
printf("\n1.enqueue\n2.dequeue\n3.display the queue\n4.count\n5.sum\n6.search for an
element\n7.min\n8.max\n9.exit\n");
printf("\nEnter your choice? ");
scanf("%d",&choice);
switch(choice)
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
count();
break;
case 5:
sum();
case 6:
find_element();
break;
case 7:
min();
break;
case 8:
max();
break;
case 9:
exit(0);
break;
default: printf("\n wrong selection!!! try again !!!");
void enqueue()
int item;
printf("\nEnte the element:");
scanf("\n%d",&item);
if(rear==maxsize-1)
printf("\nOVERFLOW\n");
return;
if(front=-1 && rear==-1)
front=0;
rear=0;
else
rear=rear+1;
queue[rear] = item;
printf("\nValue inserted");
}
void dequeue()
int item;
if(front==-1)
printf("\nUNDERFLOW\n");
return;
item = queue[front];
if(front==rear)
front=-1;
rear=-1;
else
front = front+1;
printf("\nValue deleted is %d",item);
void display()
int i;
if(rear==-1)
printf("\nEmpty queue\n");
}
else
printf("\nprinting values.....\n");
for(i=front;i<=rear;i++)
printf("\n%d\n",queue[i]);
void count()
int c=0;
if(rear==-1)
printf("\n=======UNDERFLOW!!! c=%d========\n",c);
else
int i;
for(i=front;i<=rear;i++)
c++;
printf("\nthe count is: %d\n",c);
void sum()
int sum=0;
if(rear==-1)
printf("\n=======UNDERFLOW!!! sum=%d========\n",sum);
else
int i;
for(i=front;i<=rear;i++)
sum=sum+queue[i];
printf("\nthe sum is: %d\n",sum);
void find_element()
int k;
printf("enter the element to search: ");
scanf("%d",&k);
int flag =0;
if(rear==-1)
printf("\n=======UNDERFLOW!!!=========\n");
else
int i;
for(i=front;i<=rear;i++)
if(queue[i]==k)
flag =1;
break;
if(flag==0)
printf("\nelement is not found in the queue\n");
else
printf("\nelement is found in the queue\n");
void min()
if(rear==-1)
printf("\n=======OVERFLOW!!!=======\n");
else
int i,j,min=0;
for(i=front;i<=rear;i++)
for(j=front;j<=rear;j++)
if(queue[j]<queue[i])
min=queue[j];
printf("the minimum element is %d",min);
}
}
void max()
if(rear==-1)
printf("\n=======queue is empty!!!=======\n");
else
int i,j,max=0;
for(i=front;i<=rear;i++)
for(j=front;j<=rear;j++)
if(queue[j]>queue[i])
max=queue[j];
printf("the maximum element is %d",max);
}
4. Develop appropriate functions to perform following operations on matrices
a. Addition b. subtraction c. multiplication d. Transpose of matrix
answer:
#include<stdio.h>
#include<stdlib.h>
void add(int m[3][3], int n[3][3], int sum[3][3])
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
sum[i][j] = m[i][j] + n[i][j];
void subtract(int m[3][3], int n[3][3], int result[3][3])
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
result[i][j] = m[i][j] - n[i][j];
void multiply(int m[3][3], int n[3][3], int result[3][3])
for(int i=0; i < 3; i++)
for(int j=0; j < 3; j++)
result[i][j] = 0;
for (int k = 0; k < 3; k++)
result[i][j] += m[i][k] * n[k][j];
}
void transpose(int matrix[3][3], int trans[3][3])
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
trans[i][j] = matrix[j][i];
void display(int matrix[3][3])
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
printf("%d\t",matrix[i][j]);
printf("\n");
int main()
int a[][3] = { {5,6,7}, {8,9,10}, {3,1,2} };
int b[][3] = { {1,2,3}, {4,5,6}, {7,8,9} };
int c[3][3];
printf("First Matrix:\n");
display(a);
printf("Second Matrix:\n");
display(b);
int choice;
do
printf("\nChoose the matrix operation,\n");
printf("----------------------------\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Transpose\n");
printf("5. Exit\n");
printf("----------------------------\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add(a, b, c);
printf("Sum of matrix: \n");
display(c);
break;
case 2:
subtract(a, b, c);
printf("Subtraction of matrix: \n");
display(c);
break;
case 3:
multiply(a, b, c);
printf("Multiplication of matrix: \n");
display(c);
break;
case 4:
printf("Transpose of the first matrix: \n");
transpose(a, c);
display(c);
printf("Transpose of the second matrix: \n");
transpose(b, c);
display(c);
break;
case 5:
printf("Thank You.\n");
exit(0);
default:
printf("Invalid input.\n");
printf("Please enter the correct input.\n");
}while(1);
return 0;