Chapter 1 - Intro To DATA STRUCT
Chapter 1 - Intro To DATA STRUCT
INTRODUCTION TO
DATA STRUCTURE
Learning Outcome:
Find Solution:
Solve the
world
problem
Programmer
NoKP Umur
Nama
Data type of C++
JENIS DATA
JENIS DATA MUDAH
BERSTRUKTUR
• int • Struktur storan (storage
• char structure)
• Float • Struktur keadaan
• double (Circumstance Structure)
• Struktur pautan (linking
structure)
• Struktur hubungan (relation
structure)
Declaring and Defining a Structure
• Data structure is mathematically way for storage of data in the
computer memory (storing and accessing)
• Its defines new data type and can have more than 1 members.
Struct Declaration
Data structures are declared in C++ using the following syntax:
struct structure_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object_names;
Declaration in C++ :
Keyword struct is in C++ library
struct structure_name {
member_type1 member_name1;
member_type2 member_name2; Members
member_type3 member_name3;
.
.
};
structure_name object_name;
Struct Declaration: Example
Example 1:
Keyword struct is in C++ library structure_name
struct product{
int weight;
Members
float price;
}; object_name
product apple;
structure_name
Structure Declaration
Like database records
• Structs are like database records - a row in a table, where the field names are like
the column names.
Rekod Pelajar
Rekod Pekerja Setiap REKOD mempunyai
Rekod Pelanggan MEDAN/LOKASI data yang
Rekod Penumpang tersendiri
Rekod Inventori
Rekod Pekerja
Dalam Bahasa C/C++ -No Pekerja
Rekod = Struktur -Nama Penuh
Medan/Lokasi = ahli data (p/u) -Jawatan
-Gaji Pokok
Example
Rekod Pelajar Variable Declaration
Medan :
No KP char NoKP[15];
Nama Penuh char NamaPenuh[35];
Kursus char Kursus[15];
Semester int Semester;
HPNM float HPNM;
Takrifan Struktur
struct RekodPelajar Structure Tag
{
char NoKP[15];
char NamaPenuh[35];
char Kursus[15]; Members
int Semester;
float HPNM;
} pelajar_JTMK; Structure Variable
Structure Declaration &
Pembolehubah Struktur Secara
Berasingan
struct RekodPelajar
{
char NoKP[15];
char NamaPenuh[35];
char Kursus[15];
int Semester;
float HPNM;
};
struct RekodPelajar pelajar_JTMK;
Pengisytiharan Struktur &
Pembolehubah Struktur Secara Serentak
struct RekodPelajar
{
char NoKP[15];
char NamaPenuh[35];
char Kursus[15];
int Semester;
float HPNM;
} pelajar_JTMK;
Pengisytiharan Pembolehubah
pelajar_JTMK Tanpa Nama Struktur
struct
{
char NoKP[15];
char NamaPenuh[35];
char Kursus[15];
int Semester;
float HPNM;
} Pelajar_JTMK;
Exercise
• Based on the exercise one, declare a name for another
object of the type student as student2.
struct Student{
string matric_num;
string gred; Members
double cpa;
}; Continue..
Declare multiple data structure.
Example code 3: Structure name
struct Subject{
string subject_code;
Members
string subject_name;
};
Student student1;
Object name
Subject subject1;
Structure name
Continue..
Initialize data structure.
Example code 5:
#include<iostream>
using namespace std;
struct Quiz{
double quiz1, quiz2, quiz3, quiz4, quiz5;
};
struct Test{
double test1,test2;
}; Continue..
Initialize data structure.
Example code 4:
Quiz quiz;
Object name
Test test;
void main(){
quiz.quiz1=2.30;
quiz.quiz2=3.30; Initialize data
structure
quiz.quiz3=2.50;
quiz.quiz4=3.40;
quiz.quiz5=3.20; Continue..
Initialize data structure.
Example code 4:
test.test1=15.00; Initialize data
structure
test.test2=13.00;
cout<<"Quiz 1\t:"<<quiz.quiz1<<"\n";
cout<<"Quiz 2\t:"<<quiz.quiz2<<"\n";
cout<<"Quiz 3\t:"<<quiz.quiz3<<"\n";
cout<<"Quiz 4\t:"<<quiz.quiz4<<"\n";
cout<<"Quiz 5\t:"<<quiz.quiz5<<"\n";
Continue..
Initialize data structure.
Example code 5:
void main(){
Structure
Quiz quiz={2.30,3.30,2.50,3.4,3.2};
Initialize data structure
name
Test test={15.00, 13.00};
Object name
Continue..
Exercise
The formula to calculate is as below:
TTest 1 + TTest 2 + A1+A2 + A3
* 100
50
In your program :
i. Declare TWO structure data and give the name for the
structure name as TheoryTest and Assignment
ii. Will receive the values for each marks from student
iii. Will display the values for each mark from student
iv. Will display the total mark for a student at the end of the
semester.
Understand Array Data
Structure
Difference Between
Data Structure and Array
Data Structure Array
• Example : • Example :
struct product{ int matric_num[10];
int weight;
float price;
};
product apple;
Describe array as basic data
Example code 1:
structure
#include <iostream>
using namespace std;
struct STUDENT{
int matric_num;
float test;
float final;
};
STUDENT S1[5];
S1 0 S1 1 S1 2 S1 3 S1 4
Continue..
Identify the disadvantages of arrays ???