You are on page 1of 19

CTSD HOME ASSIGNMENT-6

NAME: S Adarsh
ID-NO: 2100010062

Q1.
#include <stdio.h>
struct student
{
int id;
char name[10];
int age;
float height;
};
int main(){
struct student s[10];
int i;
printf("Enter information of students:\n");
for(i=0;i<10;++i)
{
s[i].id=i+1;
printf("\nFor id number %d\n",s[i].id);
printf("Enter name: ");
scanf("%s",s[i].name);
printf("Enter age:");
scanf("%d",&s[i].age);
printf("Enter height: ");
scanf("%f",&s[i].height);
printf("\n");
}
printf("Displaying information of students:\n\n");
for(i=0;i<10;++i)
{
printf("\nInformation for id number %d:\n",i+1);
printf("id\t name\t age\t height\t");
printf("%d%s%d%f",s[i].id,s[i].name,s[i].age,s[i].height);
}
return 0;
}

Q2.
[8:00 PM, 12/8/2021] Klu Biotech Praveen: #include<stdio.h>
#include<stdlib.h>
int stack[100],top=-1;
void push();
void pop();
void display();
void count();
void sum();
void min();
void max();
void search();
int main()
{
int choice;
while(1)
{
printf("\nstack menu is :");
printf("1.push\n");
printf("2.pop\n");
printf("3.display\n");
printf("4.sum\n");
printf("5.min\n");
printf("6.max\n");
printf("7.search\n");
printf("8.quit\n");
printf("enter your choice(1-8):\n");
scanf("%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:sum();
break;
case 5:min();
break;
case 6:max();
break;
case 7:search();
break;
case 8:exit(0);
default:printf("\nwrong choice");
}
}
return 0;
}
void push()
{
int val;
if(top==99)
{
printf("stack is full");
}
else
{
printf("enter the elements into push");
scanf("%d",&val);
top=top+1;
stack[top]=val;
}
}
void pop()
{
if(top==-1)
printf("stack is empty!");
else
{
printf("deleted element is %d",stack[top]);
top=top-1;
}
}
void display()
{
int i;
if(top==-1)
printf("stack is empty!");
else
{
printf("stack is \n");
for(i=top;i>=0;--i)
printf("%d\n",stack[i]);
}
}
void count()
{
int i,count=0;
if(top==-1)
printf("stack is empty!");
else
{
for(i=top;i>=0;i--)
count++;
}
printf("%d",count);
}
void sum()
{
int s=0, i;
if(top==-1)
printf("\nstack is empty!");
else
{
for(i=top; i>=0; i--)
s=s+stack[i];
}
printf("the sum of the elements in the stack is %d",s);
}
void max()
{
int max=stack[top],i;
if(top==-1)
printf("\nstack is empty!");
else
{
for(i=top; i>=0; i--)
{
if(stack[i]>max)
max=stack[i];
}
}
printf("the maximum element in the stack is %d",max);
}
void min()
{
int min=stack[top],i;
if(top==-1)
printf("\nstack is empty!");
else
{
for(i=top; i>=0; i--)
{
if(stack[i]<min)
min=stack[i];
}
}
printf("\nthe minimum element in the stack is %d",min);
}
void search()
{
int r,i,flag=0;
if(top==-1)
printf("\nstack is empty!");
else
{
printf("\nenter the element to be searched:\t");
scanf("%d",&r);
for(i=top; i>=0; i--)
{
if(stack[i]==r)
{
flag=1;
printf("\nthe element %d is found in position %d in the
stack",r,i+1);
break;
}
}
if(flag==0)
printf("\nthe element %d is not found",r);
}
}
Q3.
#include<stdio.h>
#include<stdlib.h>
int queue[100];
int front=-1,r=-1;
void enqueue();
void dequeue();
void display();
void count();
void sum();
void max();
void min();
void search();
int main()
{
int ch;
while(1)
{
printf("\n\n**QUEUE MENU**\n");
printf("1.insert element\n");
printf("2.delete element\n");
printf("3.display\n");
printf("4.count\n");
printf("5.sum\n");
printf("6.max\n");
printf("7.min\n");
printf("8.search\n");
printf("9.quit\n");
printf("\nenter your choice from 1 to 9:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
count();
break;
case 5:
sum();
break;
case 6:
max();
break;
case 7:
min();
break;
case 8:
search();
break;
case 9:
exit(0);
default:
printf("\noops, wrong choice, please try again!");
}
}
return 0;
}
void enqueue()
{
int item;
if(r==99)
printf("\nqueue overflow!");
else
{
if(front==-1)
front=0;
printf("the element to be inserted:\t");
scanf("%d",&item);
r=r+1;
queue[r]=item;
}
}
void dequeue()
{
if(front==-1)
printf("\nqueue underflow!");
else
{
printf("the deleted element is:%d",queue[front]);
front=front-1;
}
}
void display()
{
int i;
if(front==-1)
printf("\nqueue underflow!");
else
{
printf("the elements in the queue are:\n");
for(i=front; i<=r; i++)
printf("%d\n",queue[i]);
}
}
void count()
{
int i,c=0;
if(front==-1)
printf("\nqueue underflow!");
else
{
for(i=front; i<=r; i++)
c++;
}
printf("the number of elements in the queue is %d",c);
}
void sum()
{
int s=0, i;
if(front==-1)
printf("\nqueue underflow!");
else
{
for(i=front; i<=r; i++)
s=s+queue[i];
}
printf("the sum of the elements in the queue is %d",s);
}
void max()
{
int max=queue[front],i;
if(front==-1)
printf("\nqueue underflow!");
else
{
for(i=front; i<=r; i++)
{
if(queue[i]>max)
max=queue[i];
}
}
printf("the maximum element in the queue is %d",max);
}
void min()
{
int min=queue[front],i;
if(front==-1)
printf("\nqueue underflow!");
else
{
for(i=front; i<=r; i++)
{
if(queue[i]<min)
min=queue[i];
}
}
printf("\nthe minimum element in the queue is %d",min);
}
void search()
{
int r,i,flag=0;
if(front==-1)
printf("\nqueue underflow!");
else
{
printf("\nenter the element to be searched:\t");
scanf("%d",&r);
for(i=front; i<=r; i++)
{
if(queue[i]==r)
{
flag=1;
printf("\nthe element %d is found in position %d in the queue",r,i+1);
break;
}
}
if(flag==0)
printf("\nthe element %d is not found",r);
}
}
Q4.
#include<stdio.h>
#include<stdlib.h>
void add(int m[3][3], int n[3][3], int sum[3][3])
{
int i,j;
for(i=0;i<3;i++)
for(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])
{
int i,j;
for(i=0;i<3;i++)
for(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])
{
int i,j;
for(i=0; i < 3; i++)
{
for(j=0; j < 3; j++)
{
result[i][j] = 0; // assign 0
int k;
for (k = 0; k < 3; k++)
result[i][j] += m[i][k] * n[k][j];
}
}
}
void transpose(int matrix[3][3], int trans[3][3])
{
int i,j;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
trans[i][j] = matrix[j][i];
}
void display(int matrix[3][3])
{
int i,j;
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
printf("%d\t",matrix[i][j]);

printf("\n"); // new line


}
}
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("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;
}

You might also like