Language Constructs - Struct C
Language Constructs - Struct C
Prerequisites:
1) Data Types
2) Comfort in writing simple C++ code
MOTIVATION PROBLEM:
Let us begin with a simple problem.
Suppose I ask you to write a program to accept the details of N students in a school. These
details include the name of the student(string), class(int) of the student, roll number of the
students(string), marks in Physics(int), marks in Chemistry(int) and marks in Computers(int).
We are required to calculate and store the average marks(in Physics, Chemistry and
Computers) of these students.
How would you do this?
Well you would have created two string arrays of size N(for name of the student and for roll no.)
and 4 int arrays of size N(for class, Physics marks, Chemistry marks, Computer marks), a
double array of size N where you would store the average of each student.
A particular index(i) in each array corresponds to the details of one particular person(i) The
following code will help us solve the above problem:
Link: https://ideone.com/zpdc1f
All of you must have got that solution!
Let’s look at an example where we create a container to solve our problem. We will name our
container student and it will have the following variables:
string name : to store the name of the student
string roll : to store the roll number of the student
int ph : to store the Physics mark of the student
int ch : to store the Chemistry marks of the student
int comp: to store the Computer marks of the student
double avg : to store the average marks obtained by the student.
This how it is done:
struct student{
string name;
string roll;
int standard;
int ph;
int ch;
int comp;
double avg;
};
This creates a data structure of our choice called ‘student’ that has data types which will help us
store the details of each student.
And then we can create an array of size N where each element of the array is this student data
structure as follows:
student A[N];
Now for the ith indexed student how do I access his details.
Say you want to access the name of the ith student. You write:
cout<<A[i].name;
Say you want to access the Physics marks of the ith indexed student. You write:
cout<<A[i].phy;
CHANGING/STORING THE DETAILS OF THE ith STUDENT IN ARRAY A:
Syntax:
A[i].<variable> = <some value>
EXERCISE:
Can you write a line of code to compute the average of the 5th indexed student.
Soln.:
A[5].avg = (A[5].phy+A[5].ch+A[5].comp)/3.0;
CONCLUSION:
Great if you understood this you understand one of the most important language constructs in
C++- structs. To summarize, struct keyword creates a user defined data structure which helps
us in tieing data together for a particular object(in this case each student is an object) rather
than having it scattered in different places. If you see here we replaced so many arrays by just 1
array.
Think:
Can you think of any other example where this can be used?
Well it can be anything. It could be cars in a show room. You have a car data structure and the
data inside it can be things like colour,model,maximum speed, price etc.