Structure is the user defined data type which can store heterogeneous data i.e. data of different types. It means in structure member with all possible data type can be used and there is no restriction that elements of the structure are stored at adjacent memory locations. Structure is declared with key word struct followed by its name and then by block.
Below structure it can be referred by new name and format is:
struct name
{
int num1;
double num2;
float num3;
};
void main()
{
……………;
struct name n;
…………….;
}
The members of the structure are accessed by using structure name.member name
Example: n.num1, n.num2, n.num3 etc.
It means during input or output calculation this format should be followed strictly.
SUB-SCRIPTED STRUCTURE
In this structure whenever same procedure has to repeat again and again then the reference of the structure should have a subscript name along with the structure name and this sub-scripted variable is used in square brackets []. It means tag of the array should be 1-D.Format of sub-scripted structure is:
struct name
{
int st1;
float st2;
char st3;
};
void main()
{
………….;
struct name nm[100];
………….;
(rest it is as 1-D array);
…………..;
}
It means one for loop is required to store numbers in sub-scripted array and other for loop is required to print such a structure and for processing.
/* Program to enter number student details and print its record*/
#include<iostream.h>
#include<conio.h>
struct stud
{
int age;
char name[20],clas[10];
};
main()
{
struct stud s[100];
int n,i;
cout<<"\n How many student data you want to enter";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"\n Enter Students Name,Age and Class";
cin>>s[i].name>>s[i].age>>s[i].clas;
cout<<"\n";
}
cout<<"\n Sr.\tAge\tName\tClass";
for(i=1;i<=n;i++)
{
cout<<"\n "<<i<<"\t"<<s[i].age<<"\t"<<s[i].name<<"\t"<<s[i].clas<<"\n";
}
getch();
}
/* Program to check whether the medicine is suitable for sale or not */
#include<iostream.h>
#include<conio.h>
struct date
{
int m,y;
};
struct med
{
int mdcd;
char mdnm[20];
float pr;
struct date cd,xd;
};
main()
{
struct med m;
cout<<"\n Enter medicine name and code";
cin>>m.mdnm>>m.mdcd;
cout<<"\n Enter price of the medicine";
cin>>m.pr;
cout<<"\n Enter Current date month(01,...,12) and year(20**) for medicine to sell";
cin>>m.cd.m>>m.cd.y;
cout<<"\n Enter Expiry date month(01,...,12) and year(20**) of medicine";
cin>>m.xd.m>>m.xd.y;
if(m.cd.y<=m.xd.y)
{
if(m.cd.m<m.xd.m)
{
cout<<"\n Code\tName\tPrice\tStatus";
cout<<"\n "<<m.mdcd<<"\t"<<m.mdnm<<"\t"<<m.pr<<"\tSOLD";
}
else
{
cout<<"\n Code\tName\tPrice\tStatus";
cout<<"\n "<<m.mdcd<<"\t"<<m.mdnm<<"\t"<<m.pr<<"\tEXPIRED";
}
}
getch();
}
No comments:
Post a Comment