Assignment 8 Yash Tiwari
Assignment 8 Yash Tiwari
Tiwari
Sec : A
Roll No. : 91
Subject : PPS-II
Topic : ASSIGNMENT
8
1|Page
ASSIGNMENT-8
1. Write a program in C to store information (name, roll,
marks) of a student using structure.
Solution:
#include<stdio.h>
struct student
{
char name[50];
int roll;
int marks;
}st;
int main()
{
int d;
printf("\n Enter the name of student: ");
gets(st.name);
printf("\n Enter the roll number: ");
scanf("%d",&st.roll);
printf("\n Enter the marks: ");
scanf("%d",&st.marks);
printf("\n \n Details of the student :\n \n %s\n %d\n %d",st.name,st.roll,st.marks);
}
2|Page
Output:
3|Page
scanf("%d",&d1.feet);
printf("\n Enter inches: ");
scanf("%f",&d1.inch);
printf("\n Enter the 2nd distance: ");
printf("\n Enter feet: ");
scanf("%d",&d2.feet);
printf("\n Enter inches: ");
scanf("%f",&d2.inch);
sum.feet=d1.feet+d2.feet;
sum.inch=d1.inch+d2.inch;
if(sum.inch>12.0)
{
sum.inch=sum.inch-12.0;
++sum.feet;
}
printf("\n Sum of distances: %d %.2lf",sum.feet,sum.inch);
}
Output:
4|Page
3. Write a program in C to calculate the difference between
two time periods.
Solution:
#include<stdio.h>
struct time
{
int sec;
int min;
int hr;
}start,stop,diff;
int main()
{
printf("\n Enter start time: ");
printf("\n Enter hours,minutes and seconds: ");
scanf("%d %d %d",&start.hr,&start.min,&start.sec);
printf("\n Enter stop time: ");
printf("\n Enter hours,minutes and seconds: ");
scanf("%d %d %d",&stop.hr,&stop.min,&stop.sec);
if(stop.sec>start.sec)
{
--start.min;
start.sec+=60;
}
diff.sec=start.sec-stop.sec;
if(stop.min>start.min)
{
--start.hr;
start.min+=60;
}
5|Page
diff.min=start.min-stop.min;
diff.hr=start.hr-stop.hr;
printf("\n Time difference: %d %d %d",diff.hr,diff.min,diff.sec);
}
Output:
6|Page
int main()
{
int i;
printf("\n Enter information of students: ");
for(i=0;i<10;i++)
{
printf("\n Enter the name of the student: ");
scanf("%s",s[i].name);
printf("\n Enter the roll number: ");
scanf("%d",&s[i].roll);
printf("\n Enter the marks: ");
scanf("%d",&s[i].marks);
}
printf("\n INFORMATION OF THE STUDENTS: \n");
for(i=0;i<10;i++)
{
printf(" %s %d %d\n",s[i].name,s[i].roll,s[i].marks);
}
}
Output:
7|Page
5. Write a program in C to store information using
structures for n elements dynamically.
Solution:
8|Page
#include<stdio.h>
#include<stdlib.h>
struct details
{
char sub[20];
int marks;
}*ptr;
int main()
{
int i,n;
printf("\n Enter the number of records: ");
scanf("%d",&n);
ptr=(struct details*)malloc(n*sizeof(struct details));
printf("\n Enter the subject and marks respectively: \n");
for(i=0;i<n;i++)
{
scanf(" %s %d",&(ptr+i)->sub,&(ptr+i)->marks);
}
printf("\n Displaying Information: \n");
for(i=0;i<n;i++)
{
printf(" %s \t %d\n",(ptr+i)->sub,(ptr+i)->marks);
}
}
Output:
9|Page
6. Write a program in C to access array members using
structure.
Solution:
#include<stdio.h>
struct student
{
char name[20];
int marks[5];
}s1;
int main()
{
printf("\n Enter the name of the student: ");
scanf("%s",&s1.name);
printf("\n Enter his marks in different subjects: \n");
int i;
for(i=0;i<5;i++)
{
scanf("%d",&s1.marks[i]);
}
printf("\n Student Name: %s",s1.name);
printf("\n The following marks are: \n");
10 | P a g e
for(i=0;i<5;i++)
printf("\n%d",s1.marks[i]);
}
Output:
11 | P a g e
{
struct emp *ptr,emp1;
ptr=&emp1;
printf("\n DETAILS OF AN EMPLOYEE: \n\n");
printf("\n Enter the name of employee: ");
gets(ptr->name);
printf("\n Enter salary: ");
scanf("%d",&ptr->salary);
printf("\n DISPLAYING INFORMATION : \n");
printf("\n Employee Name: %s",ptr->name);
printf("\n Salary is: %d",ptr->salary);
}
Output:
12 | P a g e
employee in another structure named Address which is
nested. Then print the total information of the employee
using the concept of nested structure.
Solution:
#include<stdio.h>
struct address
{
char addr[100];
};
struct employee
{
int id;
char name[50];
int salary;
struct address add;
}emp1;
int main()
{
printf("\n Enter the details of the employee: \n");
printf("\n Enter the name, id, salary, and address of the employee: \n");
scanf("%s %d %d %s",&emp1.name,&emp1.id,&emp1.salary,&emp1.add.addr);
printf("\n DISPLAYING INFORMATION: \n");
printf("\n Employee Name: %s",emp1.name);
printf("\n Employee id: %d",emp1.id);
printf("\n Salary : %d",emp1.salary);
printf("\n Address of the employee: %s",emp1.add.addr);
}
Output:
13 | P a g e
9. Write a C program to implement stack using linked list,
each node should have the following information about a
Student: S_Name (string), S_address (string), S_Marks. (Use
local pointer i.e. with return statement).
Solution:
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
char s_name[20],s_add[50];
int s_marks;
struct node *next;
}s;
s*push(s*);
14 | P a g e
s*pop(s*);
void display(s*);
int main()
{
s*top=NULL;
int ch,x,c=0;
printf("\n 1.Push operation.");
printf("\n 2.Pop operation.");
printf("\n 3.Display");
do
{
printf("\n Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
top=push(top);
break;
case 2:
top=pop(top);
break;
case 3:
display(top);
break;
}
printf("\n Do you want to continue press 1: ");
scanf("%d",&c);
}while(c==1);
}
15 | P a g e
s*push(s*top)
{
s*p;
p=(s*)malloc(sizeof(s));
if(p==NULL)
{
printf("\n No memory allocated.");
}
else
{
printf("\n Enter the student name: ");
scanf("%s",&p->s_name);
printf("\n Enter student address: ");
scanf("%s",&p->s_add);
printf("\n Enter the marks of student: ");
scanf("%d",&p->s_marks);
p->next=top;
top=p;
}
return (top);
}
s*pop(s*top)
{
s*p;
if(top==NULL)
{
printf("\n Nothing to pop.");
}
else
16 | P a g e
{
printf("\n The student name is: %s",top->s_name);
printf("\n The student address is: %s",top->s_add);
printf("\n The marks of the student: %d",top->s_marks);
top=top->next;
}
return top;
}
void display(s*top)
{
if(top==NULL)
{
printf("\n Nothing to display.");
}
else
{
while(top!=NULL)
{
printf("\n The student name: %s",top->s_name);
printf("\n The student address is: %s",top->s_add);
printf("\n The marks of the student: %d",top->s_marks);
top=top->next;
}
}
}
Output:
17 | P a g e
10. Write a C program to convert infix expression into
postfix expression.
Solution:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char stack[50];
int top=-1;
void post(char infix[]);
void push(char);
char pop();
int main()
{
char infix[25];
printf("\n Enter the infix expression: ");
gets(infix);
post(infix);
getchar();
}
18 | P a g e
void push(char symb)
{
if(top>=49)
{
printf("\n Stack Overflow.");
getchar();
return;
}
else
{
top++;
stack[top]=symb;
}
}
char pop()
{
char item;
if(top==-1)
{
printf("\n Stack is empty.");
getchar();
return(0);
}
else
{
item=stack[top];
top--;
}
return item;
19 | P a g e
}
int preced(char ch)
{
if(ch==47)
{
return 5;
}
else if(ch==42)
{
return 4;
}
else if(ch==43)
{
return 3;
}
else
return 2;
}
void post(char infix[])
{
int l;
int index=0,pos=0;
char symbol,temp;
char postfix[40];
l=strlen(infix);
push('#');
while(index<l)
{
symbol=infix[index];
20 | P a g e
switch(symbol)
{
case '(':
push(symbol);
break;
case ')':
temp=pop();
while(temp!='(')
{
postfix[pos]=temp;
pos++;
temp=pop();
}
break;
case '+':
case '-':
case '*':
case '/':
case '^':
while(preced(stack[top])>=preced(symbol))
{
temp=pop();
postfix[pos]=temp;
pos++;
}
push(symbol);
break;
default:
postfix[pos++]=symbol;
21 | P a g e
break;
}
index++;
}
while(top>0)
{
temp=pop();
postfix[pos++]=temp;
}
postfix[pos++]='\0';
puts(postfix);
return;
}
Output:
22 | P a g e