Data Structures
Data Structures
IN
C++ PROGRAMMING
A data structure is a group of related data elements, possibly of different data
types grouped together under one name. Structures are used to represent a
record e.g Student record: student id, name, course, gender, … or Bank
account: account number, name, currency, balance, … A structure type in C++ is
called struct. Individual components of a struct are known as members, can have
different types and different lengths.
object_name can be a set of valid identifiers for objects that have the type of this
structure. Within braces { }, there is a list with the data members, each one is
specified with a type and a valid identifier as its name.
When a structure is created, no memory is allocated. Structure definition only
specifies that, what property a structure variable holds when it is defined.
The examples below declare a structure type, called student, and defines it
having three members: name, course and age, each of a different fundamental
type. The declaration creates a new type called student, which is then used to
declare object stud
Example 1(a):
struct student {
string name;
char course[15];
int age;
};
student stud
Example 1(b):
struct student {
string name;
char course[15];
int age;
} stud;
ACCESSING ELEMENTS OF A STRUCTURE
To access any member of a structure, we use the member access operator (.).
The member access operator is coded as a period between the structure variable
name (object name) and the structure member (member name) that we wish to
access.
Example
stud.course
stud.age
EXAMPLE 1a – Application of a data structure
#include <iostream>
#include <string>
using namespace std;
//Define a structure called student
struct student{
string name;
char course[15];
int age;
};
int main() {
// Create an instance of the Student structure
student stud;
cout <<"Enter your name: ";
cin>>stud.name;
cout << "Enter your course: ";
cin>>stud.course;
cout << "Enter age: ";
cin>>stud.age;
cout << "\nDisplaying Student Details." << endl;
cout << “Name: " << stud.name << endl;
cout << "Course: " << stud.course << endl;
cout <<"Age: " << stud.age << endl;
return 0;
}
EXAMPLE 1a
#include <iostream>
#include <string>
using namespace std;
//Define a structure called student
struct student{
string name;
char course[15];
int age;
}stud;
int main() {
cout <<"Enter your name: ";
cin>>stud.name;
cout << "Enter your course: ";
cin>>stud.course;
cout << "Enter age: ";
cin>>stud.age;
cout << "\nDisplaying Student Details." << endl;
cout << “Name: " << stud.name << endl;
cout << "Course: " << stud.course << endl;
cout <<"Age: " << stud.age << endl;
return 0;
}
EXAMPLE 1b
#include <iostream>
#include <string>
using namespace std;
int main()
{
for (int i=0; i<2;i++)
{
cout <<"Enter your name: ";
cin>>stud[i].name;
cout << "Enter your course: ";
cin>>stud[i].course;
cout << "Enter age: ";
cin>>stud[i].age;
}
cout << "\nDisplaying Student Details." << endl;
for (int i=0; i<2;i++)
{
cout << “Name: " << stud[i].name << endl;
cout << "Course: " << stud[i].course << endl;
cout <<"Age: " << stud[i].age << endl;
}
return 0;
}
LEARNING TASKS – Time 1:30 mins
3. The Zimbabwe National Team coach has requested you to write a program to
capture details of the players he has selected for AFCON. The details to be
captured for each player are Name, local club, age, number of caps. Your program
should display the details of the oldest and youngest players in the team as well
as calculate and display the average age per player.