0% found this document useful (0 votes)
25 views

Language Constructs - Struct C

The document introduces structs in C++ as a way to define custom data structures that allow bundling related data together, like the details of students, rather than having scattered in separate arrays. It demonstrates defining a struct called "student" to hold a name, roll number, marks in different subjects, and average for each student, and creating an array of these student structs to organize the data. Accessing and modifying individual elements of the structs in the array is also demonstrated.

Uploaded by

Md. Salman Azad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Language Constructs - Struct C

The document introduces structs in C++ as a way to define custom data structures that allow bundling related data together, like the details of students, rather than having scattered in separate arrays. It demonstrates defining a struct called "student" to hold a name, roll number, marks in different subjects, and average for each student, and creating an array of these student structs to organize the data. Accessing and modifying individual elements of the structs in the array is also demonstrated.

Uploaded by

Md. Salman Azad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

STRUCTS

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!

BETTER APPROACH TO ORGANISE THE DATA:


Now can you think of some better way of organising the details. What about tieing the details of
each student together in a container just like we did in pair. Each container will belong to a
particular student. Then we can have an array of such containers. So instead of having our data
scattered in different arrays, we would have it all tied. Sounds good? If we had to accept only
two details for each student we could have used pairs, but since we have to store more than two
details it turns out that we have to create our own container. We specify C++ that in this
container we will be storing 2 strings(for name,roll no.) and 4 ints(for class and marks in 3
subjects) and 1 double(to store average of 3 marks).
Then we will have an array of this container we defined.
Now, container looks to be a very informal word. From now on we will refer to such containers
we programmers create as “user defined data structures”. Nice name, right?

CREATING USER DEFINED DATA STRUCTURES:


Syntax:
struct <name of container >
{
//Create variables depending on data you want to store in this container
};
**Notice the ; after }
*The ‘struct’ keyword tells C++ that we are defining our own data structure/container.

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];

Later on we will see the importance of tying data this way.

ACCESSING THE DETAILS OF THE ith STUDENT IN ARRAY A:

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;

Notice the dot. It is formally called the dot operator.

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.

You might also like