Introduction to structures :-
1) The c + + allows us to create our own data type. Due to this facility, programmer can create his or her own data type for a specific application.
2) A structure is nothing but group of data elements, which can be of same type as well as of different types.
Defining a structure data type :-
The general syntax of declaring structure data type is a follows.
struct datatype
{
datatype member 1 ;
datatype member 2 ;
……………………
……………………
……………………
datatype member n ;
} ;
As the general syntax indicates, the structure datatype is made up[ of members. The member can be of different data types.
Example :-
struct student
{
int roll-no ;
float percent ;
char name [ 40 ] ;
}
In the above example, the student is defined as structure datatype with members, roll-no of type integer, percent of type float and name which is array of 40 characters.
The structure data type is also called as structure tag.
Declaring variables of structure data type –
The general syntax of declaring a structure variable is as follows.
Structure datatype variable name.
example
struct student s1 ;
Due to the above example, the variable S1 of type struct student is declared. The compiler will allocate a space of 46 bytes to store the information of S1.
Method 1 : Method 2 :
struct student struct student
{ {
int roll_no; int roll_no ;
float percent; float percent ;
char name [ 40 ] ; char name [ 40 ] ;
} S1; }
struct student S1;
The net effect of both the above method is same.
Accessing individual Members of the structures
1) When we define a structure data type and its variable, then it becomes necessary to deal with every individual member of the structure.
2) The general syntax of accessing the structure members is as follows.
Structure variable name, member name.
Example :-
struct student
{
int roll_no ;
float percent ;
char name [ 40 ] ;
}
struct student S1 ;
…………………
…………………
S1. roll_ no = 12 ;
S1. percent = 86.33 ;
S1. name = “xyz” ;
………………..
………………..
As the example indicates, using three assignment statements, we have assigned information to every individual member of a structure.
Assignment of complete structure variable :-
Consider the following example
Example –
struct student
{
int roll-no ;
float percent ;
char name [ 40 ] ;
}
struct student S1, S2 ;
……………………..
……………………...
S1. roll_no = 2 ;
S1. percent = 87.67 ;
S1. name = “abc” ;
S2 = S1 ;
…………………….
…………………….
Due to the statement S2 = S1 ; the values of all the members of Slar copied into corresponding members of S2.
The effect of,
S2 = S1;
Can be shown as follows
The statement S2 = S1 ; carry out he job of three assignment statement as shown below.
S2. roll_no = S1. roll_no ;
S2. percent = S1. percent ;
S2. name = S1. name ;
Nested Structures / Embedded Structures / Structure within Structure / Structure as a members of structure:-
It is possible to define a structure as a member of the other structure resulting into nested structure. Consider the following example.
struct data
{
int dd,mm,yy ;
} ;
struct student
{
int roll_no ;
float percent ;
char name [ 40 ] ;
struct date birth date ;
} ;
struct student S1 ;
In this example we have defined structure data type date with three members dd, mm and yy. The student is also defined as structure data type with the following member.
roll_no of type int
percent of type float
name as array of 40 characters
birth date of type date.
It means that, member birthdate of structure –e student itself is another structure.
ARRAY OF STRUCTURES :-
1) We have defined a structure data type called as student. When we want to deal with information of number of students, then we can define array of struct student.
2) Consider the following example
struct student
{
int roll_no ;
char name [ 40 ] ;
float percent ;
} ;
struct student S [ 60 ] ;
In the above example, ‘S’ is defined as array of 60 elements and every element is of type struct student. In other word ‘S’ is an array of 60 students.
The member of the ith student can be accessed as follows.
S [i]. roll_no ;
S [i]. name
S [i]. percent
1) WAP that reads one date and prints the day number of the year.
→ # include
# include
struct date
{
int dd, mm, yy :
} ;
void main ( )
{
int year [ 13 ] = { 0, 31, 28, 31, 30, 31, 31, 30, 31, 30, 31}
struct date d1;
int i, sum = 0 ;
clrscr ( ) ;
cout << “Enter a date in dd mm yy format =” ;
cin >> dl.dd >> dl.mm>> dl.yy ;
for ( i – 1 ; i < d1.mm ; i + + )
sum = sum + year [i] ;
sum = sum + dl.dd ;
if (dl.mm >2)
if ( dl.yy % 400 = = 0 || dl.yy % 4 = = 0 & &. dl.yy % 100 ! = 0)
sum + + ;
cout << “It is day number” << sum “of the year” ;
getch ( ) ;
}
2) WAP that reads one date and prints ‘Happy New Year’ if it is first day of the year, otherwise it should print ‘Have a Nice Day’.
→ # include
# include
struct date
{
int dd. mm. yy ;
};
void main ( )
{
struct date dl ;
clrscr ( ) ;
cout << “Enter a date in dd. mm. yy format =” ;
cin >> dl. dd >> dl. mm >> dl. yy ;
if ( dl. dd = = 1 & & dl. mm = = 1 )
cout << “Happy New Year \n” ;
else
cout << “Have a Nice Day \n” ;
getch ( );
}
3) WAP that reads one today’s date and any other date. The pgm should print whether the other date has been already passed or yet to come or whether it is today’s date only.
→ # include
# include
struct date
{
int dd,mm. yy ;
} ;
void main ( )
{
struct td, ad ;
clrscr ( ) ;
cout << “Enter today’s date =” ;
cin >> td. dd >> td. mm >> td. yy ;
cout << “Enter other date =”;
cin >> ad. dd >> ad. mm >> ad. yy ;
if ( ad. yy > td. yy )
cout << “other date is yet to come \n” ;
else
if ( ad. yy < td. yy )
cout << “Other date has been passed \n” ;
else
if ( ad. mm > td. mm )
cout << “other date is yet to come \n” ;
else
if ( ad. dd > td. dd )
cout << “ It is today’s date only \n” ;
getch ( ) ;
}
4) WAP that reads roll_no and marks of PCM of not more than 60 students. Your pgm should calculate average of PCM of all
students and it should print roll_no and average of all students.
→ # include
# include
# include
struct student
{
int roll_no ;
int p, c, m,
float avg ;
} ;
void main ( )
{
struct student s[ 60 ] ;
int i , n ;
clrscr ( ) ;
cout << “How many students =” ;
cin >> n ;
cout << “Enter into all students \n” ;
for ( i = 0 ; i < n ; i + + )
{
cout << “Roll_no ;
cout << “marks of p , c, & m = ” ;
cin >> s[ i ] – p >> s[ i ] – c >> s[ i ] . m ;
s[ i ] . avg = ( s[ i ] p + s[ i ] c + s[ i ] . m
}
cout << “Roll_no \t average \n”;
for ( i = 0 , i < n ; i + + )
{
cout << setw ( 7 ) << s[ i ] . roll_no << ‘\t’ ;
cout << setw ( 7 ) << setprecision [ 2 ] << s[ i ]. avg << ‘\n’ ;
}
getch ( ) ;
}
1) WAP that reads information of not more than 100 employees. It consists of :
a)Employees_id
b)Employees_name
c)Designation
d) Salary
Your pgm should calculate allowances and taxes based on salary and should print the net amount payable to every employee. The details are as follows :
If salary > = 1000 then DA = 110% of salary
HRA = 15% of salary
TAX = 10% of salary
If salary > = 5000 and salary < then DA = 110% of salary
HRA = 20% of salary
TAX = 8% of salary
Otherwise DA = 100% of salary
HRA = 25% of salary
TAX = 2% of salary
→ # include
# include
# include
struct employees
{
int id ;
char name [ 40 ] ;
char designation [ 20 ] ;
float salary , da, hra, tax, net ;
} ;
void main ( )
{
struct employee e[ 100 ] ;
int i , n ;
clrscr ( ) ;
cout << “Enter info all employees =” ;
cin >> n ;
cout << “Enter info of all employees \n” ;
for ( i = 0 ; i < n ; i + + )
{
cout << “ id = ” ;
cin >> e[ i ] .id ;
cout << “ name = ” ;
gets ( e[i].name ) ;
cout << “ Designation = ” ;
gets ( e[i] .designation ) ;
cout << “ salary = ” ;
cin >> e[i].salary ;
for ( i = 0 , i < n ; i + + )
{
if ( e[i].salary > = 10000 )
{
e [ i ] . da = 1.1 * e [ i ] . salary ;
e [ i ] . hra = 0.15 * e [ i ] . salary ;
e [ i ] . tax = 0.1 * e [ i ] . salary ;
}
else
if ( e[ i ] . salary > = 5000 )
{
e [ i ] . da = 1.1 * e [ i ] . salary ;
e [ i ] . hra = 0.2 * e [ i ] . salary ;
e [ i ] . tax = 0.08 * e [ i ] . salary ;
}
else
{
e [ i ] . da = 1.1 * e [ i ] . salary ;
e [ i ] . hra = 0.25 * e [ i ] . salary ;
e [ i ] . tax = 0.02 * e [ i ] . salary ;
}
e [ i ] . net = e [ i ] . salary + e [ i ] . da + e [ i ] . hra – e [ i ].tax;
}
for ( i = 0 ; i < n ; i + + )
{
cout << “Id =” << e [ i ] . id << ‘\t’ << “Name =” << e [ i ] . name << ‘\n’ ;
cout << “Designation =” << e [ i ] . designation << ‘\n’ ;
cout << “Salary =” << e [ i ] . salary << ‘\n’ ;
cout << “DA =” << e [ i ] . da << ‘\t’ HRA =” << e [ i ] . hra << ‘\n’ ;
cout << “Tax =” << e [ i ] . tax << ‘\n’ ;
cout << “Net =” << e [ i ] . net << ‘\n’ ;
}
getch ( ) ;
}
Structure and pointer :
Using the pointer the structure variable can be accessed indirectly. Consider following example :
Structure student
{
int roll_no ;
Float percent ;
char name [ 40 ] ;
} ;
Struct student S1 ;
Struct student * P ;
- - - - - - - - - - -
- - - - - - - - - - -
p = & S1 ;
- - - - - - - - - - -
- - - - - - - - - - -
In the above example . . have define S1 as variable type struct student and p is defined type pointer to struct student.
- Consider the following statement :
P = & S1
Due to this statement, the p stores address of S1. In other word P will act as the pointer to S1.
- If we want to assign a value 10 to the roll_no of S1 then it can be done with three different methods
a) S1roll_no = 10 ;
b) (*p) . roll_no = 10 ;
c) P → roll_no = 10 ;
- The first method is without using pointer. Where as next two methods we use the pointers.
- the meaning of the second and third method is nothing but roll _no of that structure variable, which is being pointer by P. Since P point to S1, effectively second and third method result into S1 roll_no.
Program :
Write a function that accepts pointer to struct student and calculate the PCM for that student. write suitable main ( ) to read roll_no and marks of PCM for more than 60 students program should calculate the average of PCM fro every student using a function and should print roll_no. average of the PCM and class for every student.
Program :
→ # include
# include
struct students
{
int roll_no ;
int p, c, m ;
float avg ;
}
void main ( ){
struct student s[ 60 ] ;
int i , n ;
void cal_avg ( struct student * ) ;
clrscr ( ) ;
cout << “How many student ( n.m + 60 ) =” ;
cin >> n ;
cout << “Enter the information of all the students \n” ;
for ( i = 0 ; i < n ; i + + )
{
cout << “roll_no =” ;
cin >> s[i].roll_no ;
cout << “Marks of P, C & M =” ;
cin >> s[i]. p >> s[i] .m>>s[i].c ;
}
for ( i = 0 ; i > n ; i + + )
{
cout << “Roll_no =” << s [ i ] . roll_no << \t ;
cout << “Average =” << s [ i ] . avg << \t ;
if ( s [ i ] . avg > = 70 ) cout << “Distinction \n” ;
else if ( s [ i ] avg > 0 60 )
cout << “first class \n” ;
else
if ( s[i].avg >=50 )
cout <<” second class” \n ;
else
if ( s[ i ].avg > = 40)
cout << “Pass \n” ;
else
cout << “fail \n” ;
}
getch ( ) ;
}
void cal_avg ( struct student * q )
{
q → avg = q → p + q → + q → m ) /3.0 ;
}
void cal_avg ( struct student * q )
{
( * q ) . avg = ( c * q ) . P + ( * q ) . c + ( * q ) m \3.0 ;
}