Lab
No. 11
OBJECT
Structures
THEORY
If we want a group of same data
type we use an array. If we want a group of elements ofdifferent data types we
use structures. For Example: To store the names, prices and number ofpages of a
book you can declare three variables. To store this information for more than
onebook three separate arrays may be declared. Another option is to make a
structure. No memory is allocated when a structure is declared. It simply
defines the “form” of thestructure. When a variable is made then memory is
allocated. This is equivalent to saying thatthere is no memory for “int” ,
but when we de clare an integer i.e.
is allocated.The structure for
the above mentioned case will look like
struct books
{
char bookname[20];
float price;
int pages;
};
struct book[50];
the above structure can hold
information of 50 books.
EXERCISE
Consider
the following table.
Code |
Name |
Post |
City |
Tel |
Salary
|
|
1
|
John
|
Clerk
|
Biratnagar
|
545332
|
7000
|
|
2
|
Marry
|
Programmer
|
Dharan
|
538901
|
20000
|
|
3
|
Dipesh
|
Director
|
Dhulabari
|
782112
|
30000
|
|
4
|
Abanish
|
Director
|
Pokhara
|
763452
|
30000
|
|
5
|
Reena
|
Assistance
technician
|
Kathmandu
|
636721
|
15000
|
|
6
|
Manoj
|
Programmer
|
Kathmandu
|
450089
|
20000
|
|
7
|
Shiva
|
Main
technician
|
Ithari
|
810012
|
20000
|
|
8
|
Bivek
|
System
engineer
|
Dharan
|
511134
|
20000
|
|
9
|
Roshan
|
Programmer
|
Patan
|
889975
|
20000
|
|
10
|
Prakash
|
Accountant
|
Patan
|
440054
|
15000
|
The
table consists of records of employees of an organization.
Section-A
(a)
Write a program in “C” to input
records through keyboard and store them in a structure array called Employee.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
struct employee
{
char name;
char post;
char city;
int phone;
float sallary;
};
clrscr();
struct employee emp[10];
int i;
for(i=0;i<10;i++)
{
printf("\nEnter the
name,post,city,phone No. and sallary\n");
scanf("%s%s%s%f%d",&emp[i].name,&emp[i].post,&emp[i].city,&emp[i].phone,&emp[i].sallary);
}
}
linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
return a;
}
(b)
Write a function to display the
list of all Records.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
struct employee
{
char name;
char post;
char city;
int phone;
float sallary;
};
clrscr();
struct employee emp[10];
int i;
for(i=0;i<10;i++)
{
printf("\nEnter the
name,post,city,phone No. and sallary\n");
scanf("%s%s%s%f%d",&emp[i].name,&emp[i].post,&emp[i].city,&emp[i].phone,&emp[i].sallary);
}
printf("Name\tPost\tCity\tPhone.No\tSallary\n");
for(i=0;i<10;i++)
printf("\n%s\t%s\t%s\t%f\t%d\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
}
linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
return a;
}
Section-B
(a)
Display the records of those
employees whose post is Programmer.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void linkfloat();
struct employee
{
char name[20];
char post[20];
char city[20];
int phone;
float sallary;
};
void main()
{
struct employee emp[10];
int i;
for(i=0;i<10;i++)
{
printf("\nEnter the
name,post,city,phone No. and sallary\n");
fflush(stdin);
scanf("%s%s%s%d%f",&emp[i].name,&emp[i].post,&emp[i].city,&emp[i].phone,&emp[i].sallary);
}
printf("Name\tPost\tCity\tPhone.No\tSallary\n");
for(i=0;i<10;i++)
{
if(strcmp(emp[i].post,"programmer")==0)
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
}
getch();
}
void linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}
(b)
Display the records of those
employees whose post is System engineer.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void linkfloat();
struct employee
{
char name[20];
char post[20];
char city[20];
int phone;
float sallary;
};
void main()
{
struct employee emp[10];
int i;
for(i=0;i<10;i++)
{
printf("\nEnter the
name,post,city,phone No. and sallary\n");
fflush(stdin);
scanf("%s%s%s%d%f",&emp[i].name,&emp[i].post,&emp[i].city,&emp[i].phone,&emp[i].sallary);
}
printf("Name\tPost\tCity\tPhone.No\tSallary\n");
for(i=0;i<10;i++)
{
if(strcmp(emp[i].post,"system
engineer")==0)
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
}
getch();
}
void linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}
(c)
Display the records of those
employees whose post is Director.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void linkfloat();
struct employee
{
char name[20];
char post[20];
char city[20];
int phone;
float sallary;
};
void main()
{
struct employee emp[10];
int i;
for(i=0;i<10;i++)
{
printf("\nEnter the
name,post,city,phone No. and sallary\n");
fflush(stdin);
scanf("%s%s%s%d%f",&emp[i].name,&emp[i].post,&emp[i].city,&emp[i].phone,&emp[i].sallary);
}
printf("Name\tPost\tCity\tPhone.No\tSallary\n");
for(i=0;i<10;i++)
{
if(strcmp(emp[i].post,"director")==0)
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
}
getch();
}
void linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}
(d)
Display the records of those
employees whose post is Clerk.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>f
void linkfloat();
struct employee
{
char name[20];
char post[20];
char city[20];
int phone;
float sallary;
};
void main()
{
struct employee emp[10];
int i;
for(i=0;i<10;i++)
{
printf("\nEnter the
name,post,city,phone No. and sallary\n");
fflush(stdin);
scanf("%s%s%s%d%f",&emp[i].name,&emp[i].post,&emp[i].city,&emp[i].phone,&emp[i].sallary);
}
printf("Name\tPost\tCity\tPhone.No\tSallary\n");
for(i=0;i<10;i++)
{
if(strcmp(emp[i].post,"clerk")==0)
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
}
getch();
}
void linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}
(e)
Display the records of those
employees whose salary scale is greater than 15000.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void linkfloat();
struct employee
{
char name[20];
char post[20];
char city[20];
int phone;
float sallary;
};
void main()
{
struct employee emp[10];
int i;
for(i=0;i<10;i++)
{
printf("\nEnter the
name,post,city,phone No. and sallary\n");
fflush(stdin);
scanf("%s%s%s%d%f",&emp[i].name,&emp[i].post,&emp[i].city,&emp[i].phone,&emp[i].sallary);
}
printf("Name\tPost\tCity\tPhone.No\tSallary\n");
for(i=0;i<10;i++)
{
if(emp[i].sallary>15000)
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
}
getch();
}
void linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}
(f)
Display the records of those
employees who lives in Kathmandu.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void linkfloat();
struct employee
{
char name[20];
char post[20];
char city[20];
int phone;
float sallary;
};
void main()
{
struct employee emp[10];
int i;
for(i=0;i<10;i++)
{
printf("\nEnter the
name,post,city,phone No. and sallary\n");
fflush(stdin);
scanf("%s%s%s%d%f",&emp[i].name,&emp[i].post,&emp[i].city,&emp[i].phone,&emp[i].sallary);
}
printf("Name\tPost\tCity\tPhone.No\tSallary\n");
for(i=0;i<10;i++)
{
if(strcmp(emp[i].city,"kathmandu")==0)
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
}
getch();
}
void linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}
(g)
Display the records of those
employees who lives in Dharan.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void linkfloat();
struct employee
{
char name[20];
char post[20];
char city[20];
int phone;
float sallary;
};
void main()
{
struct employee emp[10];
int i;
for(i=0;i<10;i++)
{
printf("\nEnter the
name,post,city,phone No. and sallary\n");
fflush(stdin);
scanf("%s%s%s%d%f",&emp[i].name,&emp[i].post,&emp[i].city,&emp[i].phone,&emp[i].sallary);
}
printf("Name\tPost\tCity\tPhone.No\tSallary\n");
for(i=0;i<10;i++)
{
if(strcmp(emp[i].city,"dharan")==0)
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
}
getch();
}
void linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}
(h)
Display the records of those
employees who lives in Patan.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void linkfloat();
struct employee
{
char name[20];
char post[20];
char city[20];
int phone;
float sallary;
};
void main()
{
struct employee emp[10];
int i;
for(i=0;i<10;i++)
{
printf("\nEnter the
name,post,city,phone No. and sallary\n");
fflush(stdin);
scanf("%s%s%s%d%f",&emp[i].name,&emp[i].post,&emp[i].city,&emp[i].phone,&emp[i].sallary);
}
printf("Name\tPost\tCity\tPhone.No\tSallary\n");
for(i=0;i<10;i++)
{
if(strcmp(emp[i].city,"patan")==0)
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
}
getch();
}
void linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}
(i)
Display the records of those
employees who are not Programmer.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void linkfloat();
struct employee
{
char name[20];
char post[20];
char city[20];
int phone;
float sallary;
};
void main()
{
struct employee emp[10];
int i;
for(i=0;i<10;i++)
{
printf("\nEnter the
name,post,city,phone No. and sallary\n");
fflush(stdin);
scanf("%s%s%s%d%f",&emp[i].name,&emp[i].post,&emp[i].city,&emp[i].phone,&emp[i].sallary);
}
printf("Name\tPost\tCity\tPhone.No\tSallary\n");
for(i=0;i<10;i++)
{
if((strcmp(emp[i].post,"programmer")>0)||(strcmp(emp[i].post,"programmer")<0))
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
}
getch();
}
void linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}
(j)
Display the records of those
employees who are not Director.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void linkfloat();
struct employee
{
char name[20];
char post[20];
char city[20];
int phone;
float sallary;
};
void main()
{
struct employee emp[10];
int i;
for(i=0;i<10;i++)
{
printf("\nEnter the
name,post,city,phone No. and sallary\n");
fflush(stdin);
scanf("%s%s%s%d%f",&emp[i].name,&emp[i].post,&emp[i].city,&emp[i].phone,&emp[i].sallary);
}
printf("Name\tPost\tCity\tPhone.No\tSallary\n");
for(i=0;i<10;i++)
{
if((strcmp(emp[i].post,"director")>0)||(strcmp(emp[i].post,"director")<0))
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
}
getch();
}
void linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}
(k)
Display the records of those
employees whose salary scale is lesser than 20000
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void linkfloat();
struct employee
{
char name[20];
char post[20];
char city[20];
int phone;
float sallary;
};
void main()
{
struct employee emp[10];
int i;
for(i=0;i<10;i++)
{
printf("\nEnter the
name,post,city,phone No. and sallary\n");
fflush(stdin);
scanf("%s%s%s%d%f",&emp[i].name,&emp[i].post,&emp[i].city,&emp[i].phone,&emp[i].sallary);
}
printf("Name\tPost\tCity\tPhone.No\tSallary\n");
for(i=0;i<10;i++)
{
if(emp[i].sallary<20000)
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
}
getch();
}
void linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}
(l)
Sort the records with respect to
City in ascending order and print them.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void linkfloat();
struct employee
{
char name[20];
char post[20];
char city[20];
int phone;
float sallary;
};
void main()
{
struct employee emp[10];
int i,j;
struct employee temp;
for(i=0;i<10;i++)
{
printf("\nEnter the
name,post,city,phone No. and sallary\n");
fflush(stdin);
scanf("%s%s%s%d%f",&emp[i].name,&emp[i].post,&emp[i].city,&emp[i].phone,&emp[i].sallary);
}
printf("\nbefore sorting");
printf("Name\tPost\tCity\tPhone.No\tSallary\n");
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(strcmp(emp[i].city,emp[j].city)<0)
{
temp=emp[i];
emp[i]=emp[j];
emp[j]=temp;
}
}
}
printf("\nAfter soring Acording to
city");
for(i=0;i<10;i++)
{
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
}
getch();
}
void linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}
(m) List
the post of employees who gets highest salary.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void linkfloat();
struct employee
{
char name;
char post;
char city;
int phone;
float sallary;
};
void main()
{
struct employee emp[10];
int i,j,k;
char temp;
float h;
for(i=0;i<10;i++)
{
printf("\nEnter the
name,post,city,phone No. and sallary\n");
fflush(stdin);
scanf("%s%s%s%d%f",&emp[i].name,&emp[i].post,&emp[i].city,&emp[i].phone,&emp[i].sallary);
}
printf("\nPost of higeher
sallery\n");
h=emp[0].sallary;
for(i=0;i<10;i++)
{
if(h<=emp[i].sallary)
{
h=emp[i].sallary;
k=i;
}
}
printf("%s",emp[k].post);
getch();
}
void linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}
(n)
List the post of employees who
gets lowest salary.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void linkfloat();
struct employee
{
char name;
char post;
char city;
int phone;
float sallary;
};
void main()
{
struct employee emp[10];
int i,j,k;
char temp;
float l;
for(i=0;i<10;i++)
{
printf("\nEnter the
name,post,city,phone No. and sallary\n");
fflush(stdin);
scanf("%s%s%s%d%f",&emp[i].name,&emp[i].post,&emp[i].city,&emp[i].phone,&emp[i].sallary);
}
printf("\nPost of lower
sallery\n");
l=emp[0].sallary;
for(i=0;i<10;i++)
{
if(l>=emp[i].sallary)
{
l=emp[i].sallary;
k=i;
}
}
printf("%s",emp[k].post);
getch();
}
void linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}
Section-C
(a)
Sort the records with respect to
Name in ascending order and print them.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void linkfloat();
struct employee
{
char name[20];
char post[20];
char city[20];
int phone;
float sallary;
};
void main()
{
struct employee emp[10];
int i,j;
struct employee temp;
for(i=0;i<10;i++)
{
printf("\nEnter the
name,post,city,phone No. and sallary\n");
fflush(stdin);
scanf("%s%s%s%d%f",&emp[i].name,&emp[i].post,&emp[i].city,&emp[i].phone,&emp[i].sallary);
}
printf("\nbefore sorting");
printf("Name\tPost\tCity\tPhone.No\tSallary\n");
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(strcmp(emp[i].name,emp[j].name)<0)
{
temp=emp[i];
emp[i]=emp[j];
emp[j]=temp;
}
}
}
printf("\nAfter soring Acording to
name");
for(i=0;i<10;i++)
{
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
}
getch();
}
void linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}
(b)
Sort the records with respect to
Name in descending order and print them.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void linkfloat();
struct employee
{
char name[20];
char post[20];
char city[20];
int phone;
float sallary;
};
void main()
{
struct employee emp[10];
int i,j;
struct employee temp;
for(i=0;i<10;i++)
{
printf("\nEnter the
name,post,city,phone No. and sallary\n");
fflush(stdin);
scanf("%s%s%s%d%f",&emp[i].name,&emp[i].post,&emp[i].city,&emp[i].phone,&emp[i].sallary);
}
printf("\nbefore sorting");
printf("Name\tPost\tCity\tPhone.No\tSallary\n");
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(strcmp(emp[i].name,emp[j].name)>0)
{
temp=emp[i];
emp[i]=emp[j];
emp[j]=temp;
}
}
}
printf("\nAfter soring Acording to
name");
for(i=0;i<10;i++)
{
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
}
getch();
}
void linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}
(c)
Sort the records with respect to
Salary in ascending order and print them.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void linkfloat();
struct employee
{
char name[20];
char post[20];
char city[20];
int phone;
float sallary;
};
void main()
{
struct employee emp[10];
int i,j;
struct employee temp;
for(i=0;i<10;i++)
{
printf("\nEnter the name,post,city,phone
No. and sallary\n");
fflush(stdin);
scanf("%s%s%s%d%f",&emp[i].name,&emp[i].post,&emp[i].city,&emp[i].phone,&emp[i].sallary);
}
printf("\nbefore sorting");
printf("Name\tPost\tCity\tPhone.No\tSallary\n");
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(emp[i].sallary>emp[j].sallary)
{
temp=emp[i];
emp[i]=emp[j];
emp[j]=temp;
}
}
}
printf("\nAfter soring Acording to
sallary");
for(i=0;i<10;i++)
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
getch();
}
void linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}
(d)
Sort the records with respect to
Salary in descending order and print them.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void linkfloat();
struct employee
{
char name[20];
char post[20];
char city[20];
int phone;
float sallary;
};
void main()
{
struct employee emp[10];
int i,j;
struct employee temp;
for(i=0;i<10;i++)
{
printf("\nEnter the
name,post,city,phone No. and sallary\n");
fflush(stdin);
scanf("%s%s%s%d%f",&emp[i].name,&emp[i].post,&emp[i].city,&emp[i].phone,&emp[i].sallary);
}
printf("\nbefore sorting");
printf("Name\tPost\tCity\tPhone.No\tSallary\n");
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(emp[i].sallary<emp[j].sallary)
{
temp=emp[i];
emp[i]=emp[j];
emp[j]=temp;
}
}
}
printf("\nAfter soring Acording to
sallary");
for(i=0;i<10;i++)
{
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
}
getch();
}
void linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}
(e)
Sort the records with respect to
City in ascending order and print them.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void linkfloat();
struct employee
{
char name[20];
char post[20];
char city[20];
int phone;
float sallary;
};
void main()
{
struct employee emp[10];
int i,j;
struct employee temp;
for(i=0;i<10;i++)
{
printf("\nEnter the
name,post,city,phone No. and sallary\n");
fflush(stdin);
scanf("%s%s%s%d%f",&emp[i].name,&emp[i].post,&emp[i].city,&emp[i].phone,&emp[i].sallary);
}
printf("\nbefore sorting");
printf("Name\tPost\tCity\tPhone.No\tSallary\n");
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(strcmp(emp[i].city,emp[j].city)<0)
{
temp=emp[i];
emp[i]=emp[j];
emp[j]=temp;
}
}
}
printf("\nAfter soring Acording to
city");
for(i=0;i<10;i++)
{
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
}
getch();
}
void linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}
(f)
Sort the records with respect to
City in descending order and print them.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void linkfloat();
struct employee
{
char name[20];
char post[20];
char city[20];
int phone;
float sallary;
};
void main()
{
struct employee emp[10];
int i,j;
struct employee temp;
for(i=0;i<10;i++)
{
printf("\nEnter the
name,post,city,phone No. and sallary\n");
fflush(stdin);
scanf("%s%s%s%d%f",&emp[i].name,&emp[i].post,&emp[i].city,&emp[i].phone,&emp[i].sallary);
}
printf("\nbefore sorting");
printf("Name\tPost\tCity\tPhone.No\tSallary\n");
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(strcmp(emp[i].city,emp[j].city)>0)
{
temp=emp[i];
emp[i]=emp[j];
emp[j]=temp;
}
}
}
printf("\nAfter soring Acording to
city");
for(i=0;i<10;i++)
{
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
}
getch();
}
void linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}
(g) Sort
the records with respect to Tel in ascending order and print them.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void linkfloat();
struct employee
{
char name[20];
char post[20];
char city[20];
int phone;
float sallary;
};
void main()
{
struct employee emp[10];
int i,j;
struct employee temp;
for(i=0;i<10;i++)
{
printf("\nEnter the
name,post,city,phone No. and sallary\n");
fflush(stdin);
scanf("%s%s%s%d%f",&emp[i].name,&emp[i].post,&emp[i].city,&emp[i].phone,&emp[i].sallary);
}
printf("\nbefore sorting");
printf("Name\tPost\tCity\tPhone.No\tSallary\n");
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(emp[i].phone<emp[j].phone)
{
temp=emp[i];
emp[i]=emp[j];
emp[j]=temp;
}
}
}
printf("\nAfter soring Acording to
phone");
for(i=0;i<10;i++)
{
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
}
getch();
}
void linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}
(h)
Sort
the records with respect to Tel in descending order and print them.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void linkfloat();
struct employee
{
char name[20];
char post[20];
char city[20];
int phone;
float sallary;
};
void main()
{
struct employee emp[10];
int i,j;
struct employee temp;
for(i=0;i<10;i++)
{
printf("\nEnter the
name,post,city,phone No. and sallary\n");
fflush(stdin);
scanf("%s%s%s%d%f",&emp[i].name,&emp[i].post,&emp[i].city,&emp[i].phone,&emp[i].sallary);
}
printf("\nbefore sorting");
printf("Name\tPost\tCity\tPhone.No\tSallary\n");
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(emp[i].phone>emp[j].phone)
{
temp=emp[i];
emp[i]=emp[j];
emp[j]=temp;
}
}
}
printf("\nAfter soring Acording to
phone");
for(i=0;i<10;i++)
{
printf("\n%s\t%s\t%s\t%d\t%f\n",emp[i].name,emp[i].post,emp[i].city,emp[i].phone,emp[i].sallary);
}
getch();
}
void linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}
can u post first sem question and ans... ? if u can then i m very much thankful to u..
ReplyDelete