Вы находитесь на странице: 1из 34

Assignment 7

(Structure and Union)

1. Write a structure template called students as follows:


Struct student
{
int roll;
char name[20];
Int marks[3];
Int total;
};
Now enter information for n students and display them.
->
#include <stdio.h>
struct student
{
int roll;
char name[20];
int marks[3];
int total;
};
main()
{
int i,j,n;
printf("Enter no of students: ");
scanf("%d",&n);
struct student s1[n];
for(j=0;j<n;j++)
{
printf("\nStudent %d: \n",j+1);
printf("Enter roll: ");
scanf("%d",&s1[j].roll);
printf("Enter name: ");
scanf("%s",s1[j].name);
s1[j].total=0;
for(i=0;i<3;i++)
{
printf("Enter marks %d: ",i+1);
scanf("%d",&s1[j].marks[i]);
s1[j].total+=s1[j].marks[i];
}
}
printf("\nThe entered information are:\n");
printf("Roll\tName\tMarks 1\tMarks 2\tMarks 3\tTotal\n");
printf("----------------------------------------\n");
for(j=0;j<n;j++)
{
printf("%d\t\t%s\t\t",s1[j].roll,s1[j].name);
for(i=0;i<3;i++)
{
printf("%d\t\t",s1[j].marks[i]);
}
printf("%d\n",s1[j].total);
}
}

Output:-
Enter no of students: 5

Student 1:
Enter roll: 1
Enter name: Rajdip
Enter marks 1: 79
Enter marks 2: 81
Enter marks 3: 92

Student 2:
Enter roll: 2
Enter name: Rimpa
Enter marks 1: 76
Enter marks 2: 80
Enter marks 3: 72

Student 3:
Enter roll: 3
Enter name: Suman
Enter marks 1: 75
Enter marks 2: 65
Enter marks 3: 81

Student 4:
Enter roll: 4
Enter name: Arnab
Enter marks 1: 90
Enter marks 2: 81
Enter marks 3: 76

Student 5:
Enter roll: 5
Enter name: Aniket
Enter marks 1: 67
Enter marks 2: 76
Enter marks 3: 80

The entered information are:


Roll Name Marks 1 Marks2 Marks3 Total
-------------------------------------------------------------------------------------------------------
1 Rajdip 79 81 92 252
2 Rimpa 76 80 72 228
3 Suman 75 65 81 221
4 Arnab 90 81 76 247
5 Aniket 67 76 80 223

2. Rewrite the above program and print the name of the topper among all students.
->
#include <stdio.h>
struct student
{
int roll;
char name[20];
int marks[3];
int totmarks;
};
main()
{
int i,j,n,pos;
printf("Enter no of students: ");
scanf("%d",&n);
struct student s1[n];
for(j=0;j<n;j++)
{
printf("\nStudent %d: \n",j+1);
printf("Enter roll: ");
scanf("%d",&s1[j].roll);
printf("Enter name: ");
scanf("%s",s1[j].name);
s1[j].totmarks=0;
for(i=0;i<3;i++)
{
printf("Enter marks %d: ",i+1);
scanf("%d",&s1[j].marks[i]);
s1[j].totmarks+=s1[j].marks[i];
}
}
printf("\nThe entered information are:\n\n");
printf("Roll\tName\tMarks 1\tMarks 2\tMarks 3\tTotal\n");
printf("---------------------------------------------------------------\n");
for(j=0;j<n;j++)
{
printf("%d\t\t%s\t\t",s1[j].roll,s1[j].name);
for(i=0;i<3;i++)
{
printf("%d\t\t",s1[j].marks[i]);
}
printf("%d\n",s1[j].totmarks);
}
pos=0;
for(i=1;i<n;i++)
{
if(s1[i].totmarks>s1[pos].totmarks)
pos=i;
}
printf("\nThe name of the topper is: %s",s1[pos].name);
}

Output:-
Enter no of students: 3

Student 1:
Enter roll: 1
Enter name: Rajdip
Enter marks 1: 79
Enter marks 2: 81
Enter marks 3: 92

Student 2:
Enter roll: 2
Enter name: Rimpa
Enter marks 1: 76
Enter marks 2: 80
Enter marks 3: 72

Student 3:
Enter roll: 3
Enter name: Suman
Enter marks 1: 75
Enter marks 2: 65
Enter marks 3: 81

Student 4:
Enter roll: 4
Enter name: Arnab
Enter marks 1: 90
Enter marks 2: 81
Enter marks 3: 76

Student 5:
Enter roll: 5
Enter name: Aniket
Enter marks 1: 67
Enter marks 2: 76
Enter marks 3: 80

The entered information are:


Roll Name Marks 1 Marks2 Marks3 Total
---------------------------------------------------------------------------------------------------------
3 Rajdip 79 81 92 252
4 Rimpa 76 80 72 228
3 Suman 75 65 81 221
4 Arnab 90 81 76 247
6 Aniket 67 76 80 223

The name of the topper is: Rajdip


3. Create a structure template called employee as follows:
Struct employee
{
int empid; //employee id
char name[20]; // name
char job[15]; // designation
float sal; //salary
int deptid; //dept id where the employee works
};
Now enter information for n(n>=10) employees and do the following using separate
functions:
(i)Display all employee records.
(ii) Display the employees having salary in a specified range.
(iii) Display the employees who work in the department id 10 and having salary
>10000
(iv) Display the names of all employees who are ‘Typists'.
(v) Display the highest and lowest paid employee name.
->
#include <stdio.h>
#include <string.h>
struct employee
{
int empid; //employee id
char ename[20]; //name
char job[15]; //designation
float sal; //salary
int deptid; //dept id where the emp works
};

void display(struct employee s1[],int n)


{
int i;
printf("\nTHE ENTERED INFORMATION ARE:\n");
printf("Employee ID\tName\tDesignation\tsalary(Rs.)\t Department ID\n");
printf("----------------------------------------\n");
for(i=0;i<n;i++)
{

printf("%d\t%s\t%s\t%f\t%d\n",s1[i].empid,s1[i].ename,s1[i].job,s1[i].sal,s1[i].deptid);
}
}

void dissal_spcrange(struct employee s1[],int n,int a,int b)


{
int i;
printf("The name of the employee having salary range %d to %d is: ",a,b);
for(i=0;i<n;i++)
{
if(s1[i].sal>=a && s1[i].sal<=b)
{
printf("%s, ",s1[i].ename);
}
}
}

void dis_dept10sal(struct employee s1[],int n)


{
int i;
printf("The name of employee having department id 10 and salary greater then
10000 is: ");
for(i=0;i<n;i++)
{
if(s1[i].deptid==10 && s1[i].sal>10000)
{
printf("%s ",s1[i].ename);
}
}
}

void dis_typist(struct employee s1[],int n)


{
int i;
char s[100]="Typist";
printf("\nThe name of the typists are: ");
for(i=0;i<n;i++)
{
if(strcmp(s1[i].job,s)==0);
{
printf("%s, ",s1[i].ename);
}
}
}

void dis_highlowsal(struct employee s1[],int n)


{
int i,pos1,pos2;
pos1=pos2=0;
for(i=1;i<n;i++)
{
if(s1[i].sal>s1[pos1].sal)
{
pos1=i;
}
if(s1[i].sal<s1[pos2].sal)
{
pos2=i;
}
}
printf("The name of highest and lowest salary paid employee is %s and %s
respectively.\n",s1[pos1].ename,s1[pos2].ename);
}

main()
{
int ch,i,n,a,b;
printf("Enter the no of employees: ");
scanf("%d",&n);
struct employee s1[n];
for(i=0;i<n;i++)
{
printf("\n-----Employee %d-----\n",i+1);
printf("Enter employees ID: ");
scanf("%d",&s1[i].empid);
printf("Enter name: ");
scanf("%s",s1[i].ename);
printf("Enter employee's job (designation): ");
scanf("%s",s1[i].job);
printf("Enter salary of the employee: ");
scanf("%f",&s1[i].sal);
printf("Enter department ID of the employee: ");
scanf("%d",&s1[i].deptid);
}

do
{
printf("\n\n---------------------MENU-----------------------\n");
printf("\n 1.Display all employees records");
printf("\n 2.Display the employees having given salary range");
printf("\n 3.Display the employee whose depart id is 10 and having salary greater
than 10000");
printf("\n 4.Display the employees who are typists");
printf("\n 5.Display the highest & lowest salary paid employee");
printf("\n 0.Exit");
printf("\n\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
display(s1,n);
break;
case 2:
printf("Enter the salary range: ");
scanf("%d%d",&a,&b);
dissal_spcrange(s1,n,a,b);
break;
case 3:
dis_dept10sal(s1,n);
break;
case 4:
dis_typist(s1,n);
break;
case 5:
dis_highlowsal(s1,n);
break;
case 0:
printf("\n--------------End-------------\n");
break;
default:
printf("\n---------INVALID CHOICE-----------");
printf("\n-----------Try Again-------------\n");
}
}while (ch);
}

Output:-
Enter the no of employees: 5

-----Employee 1-----
Enter employees ID: 1
Enter name: Rajdip
Enter employee's job (designation): Analyst
Enter salary of the employee: 20000
Enter department ID of the employee: 14

-----Employee 2-----
Enter employees ID: 2
Enter name: Nilanjan
Enter employee's job (designation): Typist
Enter salary of the employee: 9000
Enter department ID of the employee: 13

-----Employee 3-----
Enter employees ID: 3
Enter name: Rimpa
Enter employee's job (designation): Developer
Enter salary of the employee: 15000
Enter department ID of the employee: 10

-----Employee 4-----
Enter employees ID: 4
Enter name: Arnab
Enter employee's job (designation): Supervisor
Enter salary of the employee: 16000
Enter department ID of the employee: 12

-----Employee 5-----
Enter employees ID: 5
Enter name: Aniket
Enter employee's job (designation): Typist
Enter salary of the employee: 8000
Enter department ID of the employee: 11

-----Employee 6-----
Enter employees ID: 6
Enter name: Hritwick
Enter employee's job (designation): Officer
Enter salary of the employee: 30000
Enter department ID of the employee: 16

-----Employee 7-----
Enter employees ID: 7
Enter name: Shamayita
Enter employee's job (designation): Typist
Enter salary of the employee: 9000
Enter department ID of the employee: 20

-----Employee 8-----
Enter employees ID: 8
Enter name: Shreya
Enter employee's job (designation): Clerk
Enter salary of the employee: 15000
Enter department ID of the employee: 21

-----Employee 9-----
Enter employees ID: 9
Enter name: Nabin
Enter employee's job (designation): Clerk
Enter salary of the employee: 16000
Enter department ID of the employee: 17

-----Employee 10-----
Enter employees ID: 10
Enter name: Utsab
Enter employee's job (designation): Analyst
Enter salary of the employee: 9000
Enter department ID of the employee: 19

---------------------MENU-----------------------

1.Display all employees records


2.Display the employees having given salary range
3.Display the employee whose depart id is 10 and having salary greater than 10000
4.Display the employees who are typists
5.Display the highest & lowest salary paid employee

0.Exit

Enter your choice: 1


THE ENTERED INFORMATION ARE:
Employee ID Name Designation salary Department ID
------------------------------------------------------------------------------------------
1 Rajdip Analyst 20000.000000 14
2 Nilanjan Typist 9000.000000 13
3 Rimpa Developer 15000.000000 10
4 Arnab Superviser 16000.000000 12
5 Aniket Typist 8000.000000 11
6 Hritwick Officer 30000.000000 16
7 Shamayita Typist 9000.0000000 20
8 Shreya Clerk 15000.000000 21
9 Nabin Clerk 16000.000000 17
10 Utsab Analyst 8000.0000000 19
---------------------MENU-----------------------

1.Display all employees records


2.Display the employees having given salary range
3.Display the employee whose depart id is 10 and having salary greater than 10000
4.Display the employees who are typists
5.Display the highest & lowest salary paid employee
0.Exit
Enter your choice: 2
Enter the salary range: 10000 20000
The name of the employee having salary range 10000 to 20000 is: Rajdip, Rimpa, Arnab,
Shreya, Nabin

---------------------MENU-----------------------

1.Display all employees records


2.Display the employees having given salary range
3.Display the employee whose depart id is 10 and having salary greater than 10000
4.Display the employees who are typists
5.Display the highest & lowest salary paid employee
0.Exit

Enter your choice: 3


The name of employee having department id 10 and salary greater then 10000 is: Rimpa

---------------------MENU-----------------------

1.Display all employees records


2.Display the employees having given salary range
3.Display the employee whose depart id is 10 and having salary greater than 10000
4.Display the employees who are typists
5.Display the highest & lowest salary paid employee
0.Exit

Enter your choice: 4


The name of the typists are: Nilanjan, Aniket, Shamayita
---------------------MENU-----------------------

1.Display all employees records


2.Display the employees having given salary range
3.Display the employee whose depart id is 10 and having salary greater than 10000
4.Display the employees who are typists
5.Display the highest & lowest salary paid employee
0.Exit

Enter your choice: 5


The name of highest and lowest salary paid employee is Hritwick and Aniket respectively.

---------------------MENU-----------------------

1.Display all employees records


2.Display the employees having given salary range
3.Display the employee whose depart id is 10 and having salary greater than 10000
4.Display the employees who are typists
5.Display the highest & lowest salary paid employee
0.Exit

Enter your choice: 9

---------INVALID CHOICE-----------
-----------Try Again-------------

---------------------MENU-----------------------
1.Display all employees records
2.Display the employees having given salary range
3.Display the employee whose depart id is 10 and having salary greater than 10000
4.Display the employees who are typists
5.Display the highest & lowest salary paid employee
0.Exit

Enter your choice: 0


----------------End-------------

4. Rewrite the above structure by making member sal as a nested structure as follows:
{
int basis;
int da;
int hra;
int city;
}sal;
Now enter information for n employees and print their details.

include <stdio.h>
struct employee
{
int empid; //employee id
char ename[20]; //name
char job[15]; //designation
struct sal; //salary
int deptid; //dept id where the emp works
struct{
int basic;
int da;
int hra;
int city;
}sal;
};
main()
{
int i,n;
printf("Enter the no of employees: ");
scanf("%d",&n);
struct employee s1[n];
for(i=0;i<n;i++)
{
printf("\n-----Employee %d-----\n",i+1);
printf("Enter employees ID: ");
scanf("%d",&s1[i].empid);
printf("Enter name: ");
scanf("%s",s1[i].ename);
printf("Enter employee's job (designation): ");
scanf("%s",s1[i].job);
printf("Enter basic amount: ");
scanf("%d",&s1[i].sal.basic);
printf("Enter DA amount: ");
scanf("%d",&s1[i].sal.da);
printf("Enter HRA amount: ");
scanf("%d",&s1[i].sal.hra);
printf("Enter city allownance amount: ");
scanf("%d",&s1[i].sal.city);
printf("Enter department ID of the employee: ");
scanf("%d",&s1[i].deptid);
}
printf("\nThe entered information are:\n");
printf("Emp. ID\tName\tDesignation\tBasic\tDA\tHRA\tCity\t Dept. ID\n");
printf("-------------------------------------------------\n");
for(i=0;i<n;i++)
{

printf("%d\t\t%s\t\t%s\t\t%d\t%d\t%d\t%d\t%d\n",s1[i].empid,s1[i].ename,s1[i].job
,
s1[i].sal.basic,s1[i].sal.da,s1[i].sal.hra,s1[i].sal.city,s1[i].deptid);
}
}

Output:-
Enter the no of employees: 3

-----Employee 1-----
Enter employees ID: 1
Enter name: Rajdip
Enter employee's job (designation): Manager
Enter basic amount: 400
Enter DA amount: 200
Enter HRA amount: 500
Enter city allownance amount: 600
Enter department ID of the employee: 10

-----Employee 2-----
Enter employees ID: 28
Enter name: Suman
Enter employee's job (designation): Typist
Enter basic amount: 400
Enter DA amount: 500
Enter HRA amount: 600
Enter city allownance amount: 700
Enter department ID of the employee: 11

-----Employee 3-----
Enter employees ID: 3
Enter name: Nilanjan
Enter employee's job (designation): Analyst
Enter basic amount: 300
Enter DA amount: 500
Enter HRA amount: 600
Enter city allownance amount: 200
Enter department ID of the employee: 12

The entered information are:


Emp. ID Name Designation Basic DA HRA City Dept. ID
------------------------------------------------------------------------------------------------
1 Rajdip Manager 400 200 500 600 10
2 Suman Typist 400 500 600 700 11
3 Nilanjan Analyst 300 500 600 200 12

5. Consider the following structure intended to bank customers having members


account number, customer name and balance amount:
Struct cust
{
int accno;
int pin; // 4 digit code to access ATM
char name[20];
int amt;
};
Now take inputs in an array of structures for n customers. After input display them and
input the following
----------------------------------
----WELCOME TO ATM----
----------------------------------
Enter your account number:
Enter pin:
If account number and pin match, display and perform the following in a menu driven
manner:
i)Deposit an amount (Assuming that this ATM can take cash from you)
ii) Withdraw an amount
iii) Check balance
iv) Change pin
v) Exit

->
#include<stdio.h>
struct cust
{
int accno;
int pin; //4 digit number to access ATM
char name[20];
int amt;
};

void deposit(struct cust *q,int n,int acc,int p)


{
int i,amount,f=0;
printf("\n-----DEPOSIT-----\n");
printf("Enter amount to Diposit: ");
scanf("%d",&amount);
for(i=0;i<n;i++)
{
if(q[i].accno==acc)
{
if(q[i].pin==p)
{
q[i].amt+=amount;
f=1;
}
}
}
if(f==0)
printf("\nInvalid Account Number/Pin Number\n Try Again");
else
printf("----Deposit Successful----\n");
}

void withdraw(struct cust *q,int n,int acc,int p)


{
int i,amount,f=0;
printf("\n----WITHDRAW----\n");
printf("Enter amount to Withdraw: ");
scanf("%d",&amount);
for(i=0;i<n;i++)
{
if(q[i].accno==acc)
{
if(q[i].pin==p)
{
if(q[i].amt-amount>=100)
{
q[i].amt-=amount;
f=1;
}
}
}
}
if(f==0)
printf("\nInvalid Account Number/Pin Number or Insufficient Balance\n
Try Again");
else
printf("----Withdrawl Successful----\n");
}

void change_pin( struct cust *q,int n,int acc,int p)


{
int i,pin,f=0;
printf("\n----CHANGE PIN----\n");
printf("Enter the new pin: ");
scanf("%d",&pin);
for(i=0;i<n;i++)
{
if(q[i].accno==acc)
{
if(q[i].pin==p)
{
q[i].pin=pin;
f=1;
}
}
}
if(f==0)
printf("\nInvalid Account Number/Pin Number\n Try Again");
else
printf("----Pin Changed Successful----\n");
}
void balance_enq(struct cust *q,int n,int acc,int p)
{
int i;
printf("\n----Balance Enq.----\n");
for(i=0;i<n;i++)
{
if(q[i].accno==acc)
{
if(q[i].pin==p)
printf("Your initial balance is %d",q[i].amt);
else
printf("\nInvalid Account Number/Pin Number\n Try
Again");
}
}
}
main()
{
int i,ch,n,acc,p;
printf("Enter the number of customers: ");
scanf("%d",&n);
struct cust c[n];
for(i=0;i<n;i++)
{
printf("\n----Customer %d-----\n",i+1);
printf("Enter Account number: ");
scanf("%d",&c[i].accno);
printf("Enter pin: ");
scanf("%d",&c[i].pin);
printf("Enter Customer Name: ");
scanf("%s",c[i].name);
printf("Enter initial balance: ");
scanf("%d",&c[i].amt);
}

printf("\n\n---Account Information---\n\n");
printf("AccNo\tPin\tName\tBalance\n");
printf("-----------------------------\n");
for(i=0;i<n;i++)
{
printf("%d\t%d\t%s\t%d\n",c[i].accno,c[i].pin,c[i].name,c[i].amt);

do
{
printf("--------------------\n");
printf("---WELCOME TO ATM---\n");
printf("--------------------\n\n");
printf("Enter your Account No: ");
scanf("%d\n",&acc);
printf("Enter pin: ");
scanf("%d\n\n",&p);
printf("1. Deposit\n");
printf("2. Withdrawl\n");
printf("3. Check Balance\n");
printf("4. Change Pin\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
deposit(c,n,acc,p);
break;
case 2:
withdraw(c,n,acc,p);
break;
case 3:
balance_enq(c,n,acc,p);
break;
case 4:
change_pin(c,n,acc,p);
case 0:
printf("\nTransaction Complete\n");
printf("\n--Have a nice day---");
break;
default:
printf("\n---Invalid choice\nTry Again---\n");
}
}while(ch);
}

Output:-

Enter the number of customers: 5

----Customer 1-----
Enter Account number: 10026
Enter pin: 1234
Enter Customer Name: Rajdip
Enter initial balance: 3400

----Customer 2-----
Enter Account number: 10027
Enter pin: 3456
Enter Customer Name: Nilanjan
Enter initial balance: 5400

----Customer 3-----
Enter Account number: 10028
Enter pin: 3367
Enter Customer Name: Aniket
Enter initial balance: 6100

----Customer 4-----
Enter Account number: 10029
Enter pin: 8921
Enter Customer Name: Rimpa
Enter initial balance: 4300

----Customer 5-----
Enter Account number: 10030
Enter pin: 8820
Enter Customer Name: Sougata
Enter initial balance: 7100

---Account Information---

AccNo Pin Name Balance


---------------------------------------------------
10026 1234 Rajdip 3400
10027 3456 Nilanjan 5400
10028 3367 Aniket 6100
10029 8921 Rimpa 4300
10030 8820 Sougata 7100

--------------------------------
---WELCOME TO ATM---
--------------------------------
Enter your Account No: 10027
Enter pin: 3456
1. Deposit
2. Withdrawl
3. Check Balance
4. Change Pin
0. Exit
Enter your choice: 1

-----DEPOSIT-----
Enter amount to Diposit: 1200
----Deposit Successful----

--------------------------------
---WELCOME TO ATM---
--------------------------------
Enter your Account No: 10027
Enter pin: 3456
1. Deposit
2. Withdrawl
3. Check Balance
4. Change Pin
0. Exit
Enter your choice: 3

----Balance Enq.----
Your initial balance is 6600
--------------------------------
---WELCOME TO ATM---
--------------------------------
Enter your Account No: 10029
Enter pin: 8921
1. Deposit
2. Withdrawl
3. Check Balance
4. Change Pin
0. Exit
Enter your choice: 4

----CHANGE PIN----
Enter the new pin: 4546
----Pin Changed Successful----

--Have a nice day---


-------------------------------
---WELCOME TO ATM---
--------------------------------
Enter your Account No: 10026
Enter pin: 4568
1. Deposit
2. Withdrawl
3. Check Balance
4. Change Pin
0. Exit
Enter your choice: 2

----WITHDRAW----
Enter amount to Withdraw: 3400

Invalid Account Number/Pin Number or Insufficient Balance


Try Again
--------------------------------
---WELCOME TO ATM---
--------------------------------
Enter your Account No: 10027
Enter pin: 5262
1. Deposit
2. Withdrawl
3. Check Balance
4. Change Pin
0. Exit
Enter your choice: 0

--Have a nice day---

6. Create the following union, then take input and display.


Union student
{
int roll;
char name[20];
float marks;
};

->

#include <stdio.h>
union student
{
int roll;
char name[20];
float marks;
};
main()
{
union student s;
printf("Enter roll: ");
scanf("%d ",&s.roll);
printf("Roll is: %d\n",s.roll);
printf("Enter name: ");
scanf("%s",s.name);
printf("Name is: %s\n",s.name);
printf("Enter marks: ");
scanf("%f ",&s.marks);
printf("Marks is: %f",s.marks);
}

Output:-

Enter roll: 4
Roll is: 4
Enter name: Rajdip Pal
Name is: Rajdip
Enter marks: 98
Marks is: 98.000000

Вам также может понравиться