0% found this document useful (0 votes)
12 views14 pages

CSC 202 Session 3

Uploaded by

Abimbade Jamiu
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)
12 views14 pages

CSC 202 Session 3

Uploaded by

Abimbade Jamiu
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/ 14

Prepared by : Akande Noah O.

DATA STRUCTURES IN C ++

 Data structures are program elements that are used to


efficiently store, organize, manage or modify data
 In achieving these, data structures specify the relationship
among the data elements as well as the functions and
operations that can be performed on them.
 STRUCTURES: they are typically used to group several data
items/elements together to form a single entity.
 These data elements known as members are grouped
together under the same name but they can have different
data types and lengths.
 A structure must be defined before it can be used
 Structure definition lists the members that make up the
structure
 A structure definition consists of 1) the keyword struct,
2) an optional identifier called the structure name,
and 3) the member data fields listed inside
parentheses.
STRUCTURE DEFINITION IN C++
 The syntax for a Structure definition is:
struct structure_name
{
Data_type member_1;
Data_type member_2;
…………………
…………………
Data_type member_n;
};
STRUCTURE DEFINITION IN C++
 The syntax for a Structure definition is:
struct
{
Data_type member_1;
Data_type member_2;
…………………
…………………
Data_type member_n;
}structure_variable;
EXAMPLE OF STRUCTURE DEFINITION
 Structure definition with a structure name:
struct student
{
char name[50];
int age;
int level;
int No_of_Courses;
char Matric_no[15];
} ;
 If you included a structure name when you originally
defined your structure, then you need to declare the
structure variable in your main () e.g
 student csc_student, acc_student;
EXAMPLE OF STRUCTURE DEFINITION
 The syntax for a Structure definition without a
structure name is:
struct
{
char name [50];
int age;
int level;
int No_of_Courses;
char Matric_no [15];
} csc_student;
INITIALIZING STRUCTURE VARIABLES

 A struct variable can be initialized, like any other data


variables in C++, at the time it is declared. We could use:
struct staff
{
char name[20];
int age;
float salary;
} admin = {"Tom", 25, 40000.50} ;
 OR inside main () function
 staff admin = {"Tom", 25, 40000.50};
 You CANNOT assign values inside the declaration itself.
.
ACCESSING STRUCTURE MEMBERS

 Members of a structure can be accessed by using the


member access operator or the dot operator. E.g.:

. . .
 csc_student name, csc_student level, csc_student age
etc.
 Remember that the first component of an expression
involving the dot operator is the name of the specific
structure variable (csc_student), not the name of the
structure definition (student).
.
EXAMPLE
#include <iostream>
using namespace std;

struct
{
char name[50], address[100];
int age;
float salary;
} personnel;

int main()
{
// Staff personnel;

cout << "Enter Full name: ";


cin.get(personnel.name, 50);
cout << "Enter age: ";
cin >> personnel.age;
cout << "Enter salary: ";
cin >> personnel.salary;

cout << "\nDisplaying Information.\n" << endl;

cout << "Name: " << personnel.name << endl;


cout <<"Age: " << personnel.age << endl;
cout << "Salary: " << personnel.salary;

return 0;
}
ARRAY OF STRUCTURES

 To declare an array of structures, you must first define a


structure and then declare an array variable of that type.
 For example, to store addresses of 100 members of staff in a
department, you need to use an array of structure.
struct
{
char name[50], address[100];
int age;
float salary;
} staff[100];
ARRAY OF STRUCTURES

 struct student
{
char name[50];
int age;
int level;
int No_of_Courses;
char Matric_no[15];
} csc_student[100];

OR inside main () as

student csc_student[100];
ACCESSING MEMBERS OF THE ARRAY

 To reference an array of structure you use the array


subscript to access the correct element of the array,
then dot notation to access the correct member field.
 E.g. cout<<csc_student[i].name;
 cin >>csc_student[i].level;
ARRAY OF STRUCTURES

#include <iostream>
cout << "\nYou have entered these
#include <string>
movies:\n";
using namespace std;

struct movies { for (n=0; n<3; n++)


string title, producer ; {
} films [5]; cout<<"\ntitle:"<<films[n].title<<endl;

int main ()
cout<<"\nProducer:"<<films[n].producer
{
<<endl;
int n;
for (n=0; n<3; n++)
{ cout << "Enter title: "; }}
getline (cin, films[n].title);
cout << "\n Enter Producer: ";
getline (cin, films[n].producer);
}
Programming Task

 As a programmer in an insurance company, you are


expected to design a software to be used by the
human resource management department of your
company. However, the actual number of
applicants is not known. You thought of
employing the concept of structure you were
taught in your programming class as an
undergraduate student. Using a C++ program,
illustrate how you intend to implement this.

You might also like