Structures:
The collection of dissimilar data types is known as Structures.
It is used to store Student data, employee data, voter data...etc.
Structure Syntax: Ex:
The variable of a structure is called as members of structure or elements of a structure.
The members of a structure can not access out side of the structure directly .it will access only with help of struct type variable.
Note:
In C functions are not allowed inside the structure but in C++ functions are allowed inside the structure.
Note:
In C functions are not allowed inside the structure but in C++ functions are allowed inside the structure.
Syntax for Struct type variable :
Struct structname structtypevar1, structtypevar1,...etc.
Ex: struct employee e1,e2...etc;
Syntax for calling members of a variable:
Structtypevariable.elementname;
Ex: e1.eno, e1.ename...etc;
/*Write a C++ program to demonstrate Structure */
|
/*Write a C++ program to demonstrate Structure and accept the data at runtime */
|
#include<iostream.h>
#include<conio.h>
struct student
{
int sno;
char sname[20];
int m1,m2,m3,tot;
float avg;
};
void main()
{
struct student s={11,"sri”,56,32,51};
clrscr( );
s.tot=s.m1+s.m2+s.m3;
s.avg=s.tot/3;
cout<<"\nsno=“<<s.sno;
cout<<"\nsname=”<<s.sname;
cout<<"\nm1=“<<s.m1;
cout<<"\nm2=“<<s.m2;
cout<<"\nm3=“<<s.m3;
cout<<"\ntotal=“<<s.tot;
cout<<"\navg=”<<s.avg;
getch( );
}
|
#include<iostream.h>
#include<conio.h>
struct student
{
int sno;
char sname[20];
int m1,m2,m3,tot;
float avg;
};
void main()
{
struct student s;
clrscr( );
cout<<"Enter sno,sname,m1,m2,m3:";
cin>>s.sno>>s.sname>>s.m1>>s.m2>>s.m3;
s.tot=s.m1+s.m2+s.m3;
s.avg=s.tot/3;
cout<<"\nsno=“<<s.sno;
cout<<"\nsname=”<<s.sname;
cout<<"\nm1=“<<s.m1;
cout<<"\nm2=“<<s.m2;
cout<<"\nm3=“<<s.m3;
cout<<"\ntotal=“<<s.tot;
cout<<"\navg=”<<s.avg;
getch( );
}
|
Result:
Output:
sno=11
sname=sri
m1=56
m2=32
m3=51
total=139
avg=46.00
|
Result:
Input:
Enter sno,sname,m1,m2,m3:1
abcd
89
65
41
Output:
sno=1
sname=abcd
m1=89
m2=65
m3=41
total=195
avg=65.00
|
/*Write a C++ program to accept 2
students data at runtime using
Structure */
|
/*Write a C++ program to accept n
students data at runtime using Structure
*/
Or
/*Write a C++ program to demonstrate
Structure using arrays */
|
#include<iostream.h>
#include<conio.h>
struct student
{
int sno;
char sname[20];
int m1,m2,m3,tot;
float avg;
};
void main()
{
struct student s1,s2;
clrscr( );
cout<<"\nEnter student 1 details:";
cout<<"\nEnter sno,sname,m1,m2,m3:";
cin>>s1.sno>>s1.sname>>s1.m1>>s1.m2>>s1.m3;
s1.tot=s1.m1+s1.m2+s1.m3;
s1.avg=s1.tot/3;
cout<<"\nEnter student 2 details:";
cout<<"\nEnter sno,sname,m1,m2,m3:";
cin>>s2.sno,s2.sname>>s2.m1>>s2.m2>>s2.m3;
s2.tot=s2.m1+s2.m2+s2.m3;
s2.avg=s2.tot/3;
cout<<"\nStudent 1 details:";
cout<<"\nsno=“<<s1.sno;
cout<<"\nsname=”<<s1.sname;
cout<<"\nm1=“<<s1.m1;
cout<<"\nm2=“<<s1.m2;
cout<<"\nm3=“<<s1.m3;
cout<<"\ntotal=“<<s1.tot;
cout<<"\navg=”<<s1.avg;
cout<<"\nStudent 2 details:";
cout<<"\nsno=“<<s2.sno;
cout<<"\nsname=”<<s2.sname;
cout<<"\nm1=“<<s2.m1;
cout<<"\nm2=“<<s2.m2;
cout<<"\nm3=“<<s2.m3;
cout<<"\ntotal=“<<s2.tot;
cout<<"\navg=”<<s2.avg;
getch( );
}
|
#include<iostream.h>
#include<conio.h>
struct student
{
int sno;
char sname[20];
int m1,m2,m3,tot;
float avg;
};
void main()
{
struct student s[20];
int i,n;
clrscr( );
cout<<"\nEnter the no of student records:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\nEnter student” <<(i+1)<<” details:”;
cout<<"\nEnter sno,sname,m1,m2,m3:";
cin>>s[i].sno>>s[i].sname>>s[i].m1>>s[i].m2>>s[i].m3;
s[i].tot=s[i].m1+s[i].m2+s[i].m3;
s[i].avg=s[i].tot/3;
}
for(i=0;i<n;i++)
{
cout<<"\n student” <<(i+1)<<” details”;
cout<<"\nsno=“<<s[i].sno;
cout<<"\nsname=”<<s[i].sname;
cout<<"\nm1=“<<s[i].m1;
cout<<"\nm2=“<<s[i].m2;
cout<<"\nm3=“<<s[i].m3;
cout<<"\ntotal=“<<s[i].tot;
cout<<"\navg=”<<s[i].avg;
}
getch( );
}
|
Result:
Input:
Enter student 1 detailes:
Enter sno,sname,m1,m2,m3:1
a
52
63
45
Enter student 2 detailes:
Enter sno,sname,m1,m2,m3:2
b
65
32
45
Output:
Student 1 details:
sno=1
sname=a
m1=52
m2=63
m3=45
total=160
avg=53.00
Student 2 details:
sno=2
sname=b
m1=65
m2=32
m3=45
total=142
avg=47.00
|
Result:
Input:
Enter the no of student records:1
Enter student 1 details:
Enter sno,sname,m1,m2,m3:1
xyz
56
85
96
Output:
student 1 details:
sno=1
sname=xyz
m1=56
m2=85
m3=96
total=237
avg=79.00
|