Structure is a user defined data type, it is used to combine different types of available data type(s) to form a new data type, to suite a real life problem. The syntax of a structure is as follows
Syntax
struct <identifietr>
{
Datatype <identifier>;
Datatype <identifier>;
Datatype <identifier>;
----
-----
};
Example : Define a structure of student having fields Roll No , Name, Address and fees paid ( Make appropriate assumption about datatypes)
Solution
struct student {
int roll;
char name[30];
char address[60];
float fees;
};
How to define structure type variable(s)
Method -1
struct student {
int roll;
char name[30];
char address[60];
float fees;
} s ;
Method -1I
struct student {
int roll;
char name[30];
char address[60];
float fees;
} ;
Student s;
Note : The above declaration ( any ) of variable create a new memory block named as S. This is further divided into four parts namely, roll, name, address and fees. These block now contains some garbage values.
S
Roll |
Name |
Address |
Fees |
These block can be accessed using DOT OPERATOR (.) i,e if you want to access roll sub-block of S block then you have to write
s.roll
Initialization method of structure variable
Method-1
struct student {
int roll;
char name[30];
char address[60];
float fees;
}s ={1,”ramji”,”b-100 surya nagar”,125.75};
Method-1I
struct student {
int roll;
char name[30];
char address[60];
float fees;
};
student s ={1,”ramji”,”b-100 surya nagar”,125.75};
Method-1II
struct student {
int roll;
char name[30];
char address[60];
float fees;
};
student s;
s.roll =1;
strcpy(s.name,”Ramji”);
strcpy(s.address ,”b-100 surya nagar”);
s.fees =125.75;
Method-1V
struct student {
int roll;
char name[30];
char address[60];
float fees;
};
student s;
cin>>s.roll >>s.name>>s.address>>s.fees;
Method-V
struct student {
int roll;
char name[30];
char address[60];
float fees;
}s ={1,”ramji”,”b-100 surya nagar”,125.75};
student s1;
Task : Initialize variable s1 using variable s
Method 5.1
S1=S;
Methos 5.2
s1.roll = s.roll;
strcpy(s1.name,s.name);
strcpy(s1.address , s.address);
s1.fees = s.fees;
Nested Structure : if a structure is used inside another structure then it is called nested structure or
embedded structure.
Task : Define a structure of student having following structure diagram
The above mentioned problem can be solved using two method
Method – I
struct stud_name
{
Char first[15];
Char second[15];
};
struct student
{
int rollno;
stud_name name;
char address[80];
float fees;
};
Method-II
struct student {
int rollno;
struct stud_name {
char first[15];
char second[15];
}name;
char address[80];
float fees;
};
Note :
Example :Define a structure of student having fields roll, name, marks obtained in three subject physics, chemistry, mathematics, computer and English, total marks and percentage. Write a program in C++ to read a record of student. Calculate total marks and percentage. Display the same record on the screen.
Solution
Program | Output |
#include<iostream> #include<conio.h> using namespace std; struct student{ int roll; char name[30]; int phy; int chem; int math; int comp; int eng; int total; float per; }; int main() { student s; //input phase cout<<"\n Enter roll number :"; cin>>s.roll; cout<<"\n Enter name :"; cin>>s.name; cout<<"\n Enter marks obtained in physics :"; cin>>s.phy; cout<<"\n Enter marks obtained in chemistry :"; cin>>s.chem; cout<<"\n Enter marks obtained in maths :"; cin>>s.math; cout<<"\n Enter marks obtained in Computer :"; cin>>s.comp; cout<<"\n Enter marks obtained in English :"; cin>>s.eng; // processing phase s.total = s.phy+s.chem+s.math+s.comp+s.eng; s.per = s.total *100/500.0; //output Phase cout<<"\n Roll No. :"<<s.roll; cout<<"\n Name :"<<s.name; cout<<"\n Physics :"<<s.phy; cout<<"\n Chemistry :"<<s.chem; cout<<"\n Mathematics :"<<s.math; cout<<"\n Total marks :"<<s.total; cout<<"\n Percentage :"<<s.per; getch(); return 0; } |
Array of structure : The above mentioned program is designed to hold the record of a single student, if you want to store and process more records than a possible solution is array of structure
How to define and use array of structure
Q1. Define a structure of student having fields roll, name, marks obtained in three subject physics, chemistry, mathematics, computer and English, total marks and percentage. Write a program in C++ to read the record of 10 student. Calculate total marks and percentage. Display the same records on the screen.
Program | Output |
#include<iostream> #include<conio.h> using namespace std; struct student{ int roll; char name[30]; int phy; int chem; int math; int comp; int eng; int total; float per; }; int main() { student s[10]; int i; //input phase for(i=0;i<3;i++) { cout<<"\n Enter roll number :"; cin>>s[i].roll; cout<<"\n Enter name :"; cin>>s[i].name; cout<<"\n Enter marks obtained in physics :"; cin>>s[i].phy; cout<<"\n Enter marks obtained in chemistry :"; cin>>s[i].chem; cout<<"\n Enter marks obtained in mathematics :"; cin>>s[i].math; cout<<"\n Enter marks obtained in Computer :"; cin>>s[i].comp; cout<<"\n Enter marks obtained in English :"; cin>>s[i].eng; } // processing phase for(i=0;i<3;i++) { s[i].total = s[i].phy+s[i].chem+s[i].math+s[i].comp+s[i].eng; s[i].per = s[i].total *100/500.0; } //output Phase for(i=0;i<3;i++) { cout<<"\n Roll No. :"<<s[i].roll; cout<<"\n Name :"<<s[i].name; cout<<"\n Physics :"<<s[i].phy; cout<<"\n Chemistry :"<<s[i].chem; cout<<"\n Mathematics :"<<s[i].math; cout<<"\n Total marks :"<<s[i].total; cout<<"\n Percentage :"<<s[i].per; } getch(); return 0; } |
Passing Structure as a function Parameter : Structure can be passed as a function parameter as shown in the following example.
Task : Define a structure of student having fields name and roll no. Write a program to read name and roll number and print the value of this structure using a function Print( ).
Possible Solution :
I ( improper) | II ( Proper Solution) |
#include<iostream> #include<conio.h> using namespace std; struct student{ int roll; char name[30]; }; void print( int x, char y[]) { cout<<"\n Roll No :"<<x; cout<<"\n Name :"<<y; } int main() { student s={10,"Rakesh Kumar"}; print(s.roll,s.name); getch(); return 0; } Output | #include<iostream> #include<conio.h> using namespace std; struct student{ int roll; char name[30]; }; void print( student s) { cout<<"\n Roll No :"<<s.roll; cout<<"\n Name :"<<s.name; } int main() { student s={10,"Rakesh Kumar"}; print(s); getch(); return 0; } Output |
Structure as a function return type value :
Program | Output |
#include<iostream> #include<conio.h> using namespace std; struct student{ int roll; char name[30]; }; student read_data(void) { student s; cout<<"\n Enter roll no :"; cin>>s.roll; cout<<"\n Enter name :"; cin>>s.name; return (s); } int main() { student s; s= read_data(); cout<<"\n Roll No :"<<s.roll; cout<<"\n Name :"<<s.name; getch(); return 0; } Output | |
Passing structure as a call value method in a function
program | Output |
#include<iostream> #include<conio.h> #include<string.h> using namespace std; struct student{ int roll; char name[30]; }; void change( student s) // parameter as value { s.roll=100; strcpy(s.name,"Ramji"); } int main() { student s={10,"Rakesh Kumar"}; cout<<"\n Before function call\n"; cout<<"\n Roll No :"<<s.roll; cout<<"\n Name :"<<s.name; change(s); cout<<"\n after function call by value\n"; cout<<"\n Roll No :"<<s.roll; cout<<"\n Name :"<<s.name; getch(); return 0; } | No Change in the output as the value is passed in the function as a value. |
Passing structure as a call by reference method in a function
Program | Output |
#include<iostream> #include<conio.h> #include<string.h> using namespace std; struct student{ int roll; char name[30]; }; void change( student & s) // parameter as reference { s.roll=100; strcpy(s.name,"Ramji"); } int main() { student s={10,"Rakesh Kumar"}; cout<<"\n Before function call\n"; cout<<"\n Roll No :"<<s.roll; cout<<"\n Name :"<<s.name; change(s); cout<<"\n after function call by value\n"; cout<<"\n Roll No :"<<s.roll; cout<<"\n Name :"<<s.name; getch(); return 0; } |
Changes made inside function is visible in the main program as value had been passed as reference |
No comments:
Post a Comment