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

Data Structures

Data structures by Josphat Mpofu...

Uploaded by

Josphat Mpofu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Data Structures

Data structures by Josphat Mpofu...

Uploaded by

Josphat Mpofu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

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.

DEFINING A DATA STRUCTURE


struct struct_name {
data_type1 member_name1;
data_type2 member_name2;
data_type3 member_name3;
.
.
} object_names;

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;

//Define a structure called student


struct student{
string name;
char course[15];
int age;
}stud[2];

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

1. Write a program that creates an array of structure called student. The


structure is made up of the following fields; name, course, age and gender. The
program accepts and prints
a. The details of five students entered by the user
b. IT students ONLY

2. Code a program that creates an array of structure called STUDENTS. An array


size is five. The structure is made up of the following fields: Name, Surname and
Mark.
a. The program calculates and displays the all details of the student who got the
smallest mark
b. The marks scored in descending order

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.

You might also like