0% found this document useful (0 votes)
49 views24 pages

Chapter 08 - Structure

The document discusses structures in C++. It defines a structure as a collection of related elements that can be of different types and have a single name. Each element in a structure is called a field. Structures allow grouping of related data and can contain arrays. Structures can be nested within other structures. Arrays of structures and structures containing arrays are also discussed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views24 pages

Chapter 08 - Structure

The document discusses structures in C++. It defines a structure as a collection of related elements that can be of different types and have a single name. Each element in a structure is called a field. Structures allow grouping of related data and can contain arrays. Structures can be nested within other structures. Arrays of structures and structures containing arrays are also discussed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

BEEA 1343:

STRUCTURE

1
OBJECTIVES:

 To understand the concept of enum & structure

 To understand the differences between array and


structure

 To understand how to use structure in program

 To understand structure containing array and array


of structure

2
Structure

• Structure – is a collection of related elements, possibly of different


types, having a single name.

• Each element in a structure is called a field.

• A field is the smallest element of named data that has meaning.

• The different between an array and a structure is that all elements


in an array must be of the same type, while the elements in a
structure can be of the same or different types. 3
Structure
•The first example fraction, has two fields, both of which are
integers. The second example, student, has three fields, an
integer number, an array and a floating point number.

5 7

1234 F A T I M A H /0 3.7
4
Structure Declaration and Definition

• To declare a structure type, use the keyword struct followed


by the name of the type and its field list.

General Format:
struct <structName>
{
type1 field1;
type2 field2;
. . .
};
• To define its variables, use the structure tag (name) as the
variable’s type.
5
Structure Declaration and Definition
Example :

struct
structure tag
{
int ; structure members
string ;
short ;
double ;
bill
};
studentID
STUDENT bill;
name
yearInSchool

gpa
6
Structure Initialization

• A structure can be initialized.

• The rules for structure initialization are similar to the rules for array
initialization:
(1) the initializers are enclosed in braces and separated by commas;

(2) the initializers must match their corresponding types in the structure
declaration;

(3) if use a nested structure, the nested initializers must be enclosed in


7
their own set of braces.
Structure Initialization
Example :
struct SAMPLE
{
int x;
int y; sam1
float t;
char u;
};
SAMPLE sam1 = { 2, 5, 3.2, ’A’ };

SAMPLE sam2 = { 7, 3 };
sam2

8
Referencing Individual Field
• We can read data into and write data from structure members just
as we can from individual variables.

• For example the value for the field of the sample structure can be
read from the keyboard and placed in sam2 using the input statement
below.

cin >> sam2.y >>sam2.x >>sam2.t >>sam2.u;

9
Structure Operation
• The structure is an entity that can be treated as a whole.
• However, only one operation, assignment is allowed on the structure
itself. In other words, a structure can only be copied to another structure of
the same type using the assignment operator.
• Example :

10
struct STUDDATA
{
int id;
char name[20];
float gradePoint;
};

int main( )
{ Exercise 1
STUDDATA studBITG1113;

cout << "Please enter your id";


cin >> studBITG1113.id; cin.ignore();
cout << "\nPlease enter your name";
cin.getline(studBITG1113.name,19);
cout << "\nPlease enter your gradePoint";
cin >> studBITG1113.gradePoint;

cout << "\nYour id is :"<<studBITG1113.id;


cout << "\nYour name is :"<<studBITG1113.name;
cout << "\nYour grade point is :"<<studBITG1113.gradePoint;

if(studBITG1113.gradePoint > 3.5)


cout<<"Excellent!“
return 0;
} 11
Nested Structure

 We can have structures as members of a structures.

 When a structure includes another structure, it is a nested


structure.

 For example, we can have a structure called STAMP that


stores the date and the time.

 The DATE is in turn a structure that stores the month, day


and year.

 The TIME is also structure, one that stores the hour,


minute and second.
12
•This structure design as :

struct DATE
{
int month;
int day;
int year;
};
struct TIME
{
int hour;
int min;
int sec;
};
struct STAMP
{
DATE date;
TIME time;
};
13

STAMP stamp;
• It is possible to nest the same structure type more than once
in a declaration.Example :
struct JOB
{
STAMP startTime;
STAMP endTime;
};
JOB job;

14
Referencing Nested Structure
• When access a nested structure, we must include each level
from the highest (stamp) to the component being referenced.

15
Structure Containing Array
• Structures can have one or more arrays as members.

/*Global declarations */
struct PUPIL
{
char name[26];
int midterm[3];
int final;
};
16
PUPIL student;
struct STUDDATA
{ A struct with array
char name[20];
float test[3];
members
float ass[5];
float quiz[2];
float final;
float total;
float project;
Exercise 2
};

int main( )
{
STUDDATA studBITG1113;
float totTest =0, totAss = 0, totQuiz=0;

cout << "\nPlease enter your name : ";


cin.getline(studBITG1113.name,19);

for( int i = 0; i < 3; i++ ){


cout << "\nPlease enter the score for test : "<< i+1;
cin >> studBITG1113.test[i];
totTest += studBITG1113.test[i];
}
30
for(i=0; i<5; i++){
cout << "\nPlease enter the score for assignment"<<i+1<<" : ";
cin >> studBITG1113.ass[i];
totAss += studBITG1113.ass[i];
}

for(i=0; i<2; i++){


cout << "\nPlease enter the score for quiz"<<i+1 <<" : ";
cin >> studBITG1113.quiz[i];
totQuiz += studBITG1113.quiz[i];
}

cout << "\nPlease enter the score for final : ";


cin >> studBITG1113.final;

cout << "\nPlease enter the score for project : ";


cin >> studBITG1113.project;

studBITG1113.total = totTest + totAss + totQuiz +


studBITG1113.final + studBITG1113.project;

cout << "\nYour score for this subject is : ” << studBITG1113.total;


return 0;
31
}
Output :

19
Array of Structure
• As a programmer, you will encounter many situations that
require you to create an array of structures.

• By putting the data in an array, we can quickly and easily work


with the data.

• Example array of structures might look.

20
Example : Array of Structure
struct PELAJAR
{
int id;
char name[31];
float project_mark;
int test_mark;
int final_mark;
char gred;
};

PELAJAR rekod_pelajar[3];

OR with initialization :
struct pelajar rekod_pelajar[] = {
{1342, "Zulaiha Ismail", 10.2, 10, 20, ‘F’},
{1343, "Aina Ahmad", 51.4, 60, 60, ‘C’},
21
{1344, "Maria Musa", 90.0, 99, 99, ‘A’}
};
Example : Array of Structure

To print the elements in rekod_pelajar :


for(i=0; i<3;i++){
cout << rekod_pelajar[i].id <<endl;
cout << rekod_pelajar[i].name <<endl;
cout << rekod_pelajar[i].project_mark <<endl;
cout << rekod_pelajar[i].test_mark <<endl;
cout << rekod_pelajar[i].final_mark <<endl;
cout << rekod_pelajar[i].gred <<endl;
}

22
struct STUDDATA
{ Array with struct as
char name[20];
float test[3]; its elements
float ass[5];
float quiz[2];
float final;
float total;
float project; Exercise 3
};

int main( )
{
STUDDATA studBITG1113[3];
float totTest = 0, totAss = 0, totQuiz = 0;

for( int j = 0; j<3; i++ ){


cout << "\nPlease enter your name : ";
cin.getline(studBITG1113[i].name,19);

for( i=0; i<3;i++ ){


cout << "\nPlease enter the score for test : "<<i+1;
cin >> studBITG1113[j].test[i];
totTest += studBITG1113[j].test[i];
}
36
for( i = 0; i < 5; i++ ){
cout << "\nPlease enter the score for assignment"<<i+1<<" : ";
cin >> studBITG1113[j].ass[i];
totAss += studBITG1113[j].ass[i];
}

for( i = 0; i < 2; i++ ){


cout << "\nPlease enter the score for quiz"<<i+1 <<" : ";
cin >> studBITG1113[j].quiz[i];
totQuiz += studBITG1113[j].quiz[i];
}

cout << "\nPlease enter the score for final : ";


cin >> studBITG1113[j].final;

cout << "\nPlease enter the score for project : ";


cin >> studBITG1113[j].project;

studBITG1113[j].total = totTest + totAss + totQuiz +


studBITG1113[j].final + studBITG1113[j].project;
cin.ignore();
}

for( i = 0; i < 3; i++ ){


cout<<endl<<studBITG1113[i].name<<endl;
cout <<"Your score for this subject is : ”<<studBITG1113[i].total<<endl;
}
37
return 0;
}

You might also like