SlideShare a Scribd company logo
1
Programming
Structures
2
P102 Prog. Fundamentals, Structures / Slide 2
Structures
A Structure is a collection of related data items,
possibly of different types.
A structure type in C++ is called struct.
A struct is heterogeneous in that it can be
composed of data of different types.
In contrast, array is homogeneous since it can
contain only data of the same type.
3
P102 Prog. Fundamentals, Structures / Slide 3
Structures
Structures hold data that belong together.
Examples:
Student record: student id, name, major,
gender, start year, …
Bank account: account number, name,
currency, balance, …
Address book: name, address, telephone
number, …
In database applications, structures are called
records.
4
P102 Prog. Fundamentals, Structures / Slide 4
Structures
Individual components of a struct type are
called members (or fields).
Members can be of different types (simple,
array or struct).
A struct is named as a whole while individual
members are named using field identifiers.
Complex data structures can be formed by
defining arrays of structs.
5
P102 Prog. Fundamentals, Structures / Slide 5
struct basics
Definition of a structure:
struct <struct-type>{
<type> <identifier_list>;
<type> <identifier_list>;
...
} ;
Example:
struct Date {
int day;
int month;
int year;
} ;
The “Date” structure
has 3 members,
day, month & year.
Each identifier
defines a member
of the structure.
6
P102 Prog. Fundamentals, Structures / Slide 6
struct examples
Example:
struct StudentInfo{
int Id;
int age;
char Gender;
double CGA;
};
Example:
struct StudentGrade{
char Name[15];
char Course[9];
int Lab[5];
int Homework[3];
int Exam[2];
};
The “StudentGrade”
structure has 5
members of
different array types.
The “StudentInfo”
structure has 4 members
of different types.
7
P102 Prog. Fundamentals, Structures / Slide 7
struct examples
Example:
struct BankAccount{
char Name[15];
int AcountNo[10];
double balance;
Date Birthday;
};
Example:
struct StudentRecord{
char Name[15];
int Id;
char Dept[5];
char Gender;
};
The “StudentRecord”
structure has 4
members.
The “BankAcount”
structure has simple,
array and structure
types as members.
8
P102 Prog. Fundamentals, Structures / Slide 8
struct basics
Declaration of a variable of struct type:
<struct-type> <identifier_list>;
Example:
StudentRecord Student1, Student2;
Student1 and Student2 are variables of
StudentRecord type.
Student1 Student2
Name
Id Gender
Dept
Name
Id Gender
Dept
9
P102 Prog. Fundamentals, Structures / Slide 9
Chan Tai Man
12345 M
COMP
Ex. 1: struct basics
The members of a struct type variable are
accessed with the dot (.) operator:
<struct-variable>.<member_name>;
Example:
strcpy(Student1.Name, "Chan Tai Man");
Student1.Id = 12345;
strcpy(Student1.Dept, "COMP");
Student1.gender = 'M';
cout << "The student is ";
switch (Student1.gender){
case 'F': cout << "Ms. "; break;
case 'M': cout << "Mr. "; break;
}
Student1
Name
Id Gender
Dept
10
102 Prog. Fundamentals, Structures / Slide 10
11
102 Prog. Fundamentals, Structures / Slide 11
Chan Tai Man
12345 M
COMP
Ex. 2: struct-to-struct assignment
The values contained in one struct type
variable can be assigned to another variable
of the same struct type.
Example:
strcpy(Student1.Name,
"Chan Tai Man");
Student1.Id = 12345;
strcpy(Student1.Dept, "COMP");
Student1.gender = 'M';
Student2 = Student1;
Student1
Chan Tai Man
12345 M
COMP
Student2
12
102 Prog. Fundamentals, Structures / Slide 12
13
102 Prog. Fundamentals, Structures / Slide 13
Ex. 3-5: Nested structures
We can nest structures inside structures.
Examples:
struct point{
double x, y;
};
point P;
struct line{
point p1, p2;
};
line L;
struct triangle{
point p1, p2, p3;
};
triangle T;
(P.x, P.y)
(L.p1.x, L.p1.y)
(L.p2.x, L.p2.y)
(T.p2.x, T.p2.y)
(T.p1.x, T.p1.y)
(T.p3.x, T.p3.y)
14
102 Prog. Fundamentals, Structures / Slide 14
Ex. 3-5: Nested structures
We can nest structures inside structures.
struct line{
point p1, p2;
};
line L; (L.p1.x, L.p1.y)
(L.p2.x, L.p2.y)
line
p1 p2
x y x y
15
102 Prog. Fundamentals, Structures / Slide 15
Ex. 3-5: Nested structures
Assign values to the variables P, L, and T
using the picture:
point P;
line L;
triangle T;
(4, 11)
(2, 7)
(10, 9)
(6, 5)
(2, 0)
(8, 3)
Ex. 3: Graph a point
Ex. 4: Graph a line
Ex. 5: Graph a triangle
16
102 Prog. Fundamentals, Structures / Slide 16
Ex. 3-5: Nested structures
point P;
line L;
triangle T;
P.x = 4;
P.y = 11;
(4, 11)
(2, 7)
(10, 9)
(6, 5)
(2, 0)
(8, 3)
L.p1.x = 2;
L.p1.y = 7;
L.p2.x = 10;
L.p2.y = 9;
T.p1.x = 2;
T.p1.y = 0;
T.p2.x = 6;
T.p2.y = 5;
T.p3.x = 8;
T.p3.y = 3;
17
102 Prog. Fundamentals, Structures / Slide 17
Ex. 3: Graphing a Point
x
5
4
3
2
1
1514131211100876543210
y
(x1, y1)
*
struct point {int x, y;};
void user_input(point&);
void graph_point(char grid[NUMBER_ROWS][NUMBER_COLS], point);
void print_grid(char grid[NUMBER_ROWS][NUMBER_COLS]);
void set_background(char grid[][NUMBER_COLS]);
void user_input(point& P){ // pass by reference
// get user input and check that it is on the grid
do{
cout << "Enter column (x<" << NUMBER_COLS << ") & row (y<"
<< NUMBER_ROWS <<") of the 1st point: ";
cin >> P.x >> P.y;
} while ((P.y<0) || (P.y >= NUMBER_ROWS) ||
(P.x<0) || (P.x >= NUMBER_COLS));
}
// Put a point on the grid
void graph_point(char grid[][NUMBER_COLS], point P){
grid[P.y][P.x] = '*';
}
19
102 Prog. Fundamentals, Structures / Slide 19
20
102 Prog. Fundamentals, Structures / Slide 20
Ex. 4: Trajectory Between 2 Points
x
*5
4
3
2
*1
1514131211100876543210
y
• The equation of a line going through two points
(x1, y1) & (x2, y2) can be represented by:
(y-y1)/ (x-x1) = (y2-y1)/(x2-x1)
or y = ((y2-y1)/(x2-x1)) (x-x1) + y1
where (y -y )/(x -x ) is called the slope.
(x1, y1)
(x2, y2)
*
*
*
// use struct to graph line between two points
#include <ctype> // for tolower
#include <iostream>
using namespace std;
int const NUMBER_ROWS = 11;
int const NUMBER_COLS = 31;
struct point {int x, y;};
struct line {point p1, p2;};
void user_input (line&);
void graph_line (char grid[NUMBER_ROWS][NUMBER_COLS], line);
void print_grid(char grid[NUMBER_ROWS][NUMBER_COLS]);
void set_background (char grid[][NUMBER_COLS]);
// Graph line between two points
int main(){
// set an array for the grid
char grid[NUMBER_ROWS][NUMBER_COLS];
line line1;
int row1=0, col1=0, row2=0, col2=0;
char do_another;
// function call to fill background of grid with ‘-’
set_background(grid);
do{ // do-while loop allows multiple lines
// get user input
user_input(line1);
// put ‘*’ into array to graph the line(s) on the grid
graph_line(grid, line1);
// print the grid from the array to the screen
print_grid(grid);
cout << "Do you want to add another line (y/n)? ";
cin >> do_another;
do_another = tolower(do_another);
} while (do_another == 'y');
return 0;
}
23
102 Prog. Fundamentals, Structures / Slide 23
Ex. 4: More on Graphing Line
//A function to get user input and check that it is on the grid.
void user_input(line& line1){
do{
cout << "Enter column (x<" << NUMBER_COLS
<< ") & row (y<" << NUMBER_ROWS
<< ") coordinates of the 1st point: ";
cin >> line1.p1.x >> line1.p1.y;
} while ((line1.p1.y<0) ||
(line1.p1.y>=NUMBER_ROWS) ||
(line1.p1.x<0) ||
(line1.p1.x >= NUMBER_COLS));
// use another do-while loop for the 2nd
point, col2 and row2
}
24
102 Prog. Fundamentals, Structures / Slide 24
Ex. 4: More on Graphing Line
void graph_line(char grid[][NUMBER_COLS], line line1){
int row, col;
double rise, run, slope;
// one point
if((line1.p1.y==line1.p2.y)&&(line1.p1.x==line2.p2.x))
grid[line1.p1.y][line1.p1.x] = '*';
else if(line1.p2.x==line1.p1.x){ // infinite slope
if (line1.p1.y < line1.p2.y){
for(row=line1.p1.y; row <= line1.p2.y; row++)
grid[row][line1.p1.x] = '*';
}
else{
for(row=line1.p1.y; row >= line1.p2.y; row--)
grid[row][line1.p1.x] = '*';
}
}
25
102 Prog. Fundamentals, Structures / Slide 25
Ex. 4: More on Graphing Line
else{
rise=line1.p2.y-line1.p1.y; run=line1.p2.x-line1.p1.x;
slope = (double)rise / run; // run cannot = 0
if (run >0){
for(col = line1.p1.x; col <= line1.p2.x; col++){
// line1.p1.y is offset for starting point
row=(int)(slope*(col–line1.p1.x)+line1.p1.y);
grid[row][col] = '*';
}
}
else{
for(col=line1.p1.x; col >= line1.p2.x; col--){
row=(int)(slope*(col–line1.p1.x)+line1.p1.y);
grid[row][col] = '*';
}
}
}
}
26
102 Prog. Fundamentals, Structures / Slide 26
27
102 Prog. Fundamentals, Structures / Slide 27
Arrays of structures
An ordinary array: One type of data
An array of structs: Multiple types of data in each
array element.
0 1 2 … 98 99
0 1 2 … 98 99
28
102 Prog. Fundamentals, Structures / Slide 28
Arrays of structures
We often use arrays of structures.
Example:
StudentRecord Class[100];
strcpy(Class[98].Name, "Chan Tai Man");
Class[98].Id = 12345;
strcpy(Class[98].Dept, "COMP");
Class[98].gender = 'M';
Class[0] = Class[98];
. . .
0 1 2 … 98 99
Chan Tai Man
12345 M
COMP
29
102 Prog. Fundamentals, Structures / Slide 29
Arrays inside structures
We can use arrays inside structures.
Example:
struct square{
point vertex[4];
};
square Sq;
Assign values to Sq using the given square
(4, 3) (10, 3)
(4, 1) (10, 1)
x y x y x y x y

More Related Content

What's hot

Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder
Vishvesh Jasani
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
Saad Sheikh
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
Tarun Sharma
 
Decision making statements in C programming
Decision making statements in C programmingDecision making statements in C programming
Decision making statements in C programming
Rabin BK
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
tanmaymodi4
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
Haard Shah
 
C if else
C if elseC if else
C if else
Ritwik Das
 
C lecture 3 control statements slideshare
C lecture 3 control statements slideshareC lecture 3 control statements slideshare
C lecture 3 control statements slideshare
Gagan Deep
 
Decision making and branching in c programming
Decision making and branching in c programmingDecision making and branching in c programming
Decision making and branching in c programming
Priyansh Thakar
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements looping
MomenMostafa
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
tanmaymodi4
 
Control and conditional statements
Control and conditional statementsControl and conditional statements
Control and conditional statements
rajshreemuthiah
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
Suhail Akraam
 
Control Structures
Control StructuresControl Structures
Control StructuresGhaffar Khan
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in C
RAJ KUMAR
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
narmadhakin
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programming
Archana Gopinath
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C Programming
Kamal Acharya
 

What's hot (20)

Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
 
Decision making statements in C programming
Decision making statements in C programmingDecision making statements in C programming
Decision making statements in C programming
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
Control structures in C
Control structures in CControl structures in C
Control structures in C
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
 
C if else
C if elseC if else
C if else
 
C lecture 3 control statements slideshare
C lecture 3 control statements slideshareC lecture 3 control statements slideshare
C lecture 3 control statements slideshare
 
Decision making and branching in c programming
Decision making and branching in c programmingDecision making and branching in c programming
Decision making and branching in c programming
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements looping
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
Control and conditional statements
Control and conditional statementsControl and conditional statements
Control and conditional statements
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
 
Control Structures
Control StructuresControl Structures
Control Structures
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in C
 
Ch3 selection
Ch3 selectionCh3 selection
Ch3 selection
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programming
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C Programming
 

Similar to Structure in programming in c or c++ or c# or java

02.adt
02.adt02.adt
Unit 5 (1)
Unit 5 (1)Unit 5 (1)
Unit 5 (1)
psaravanan1985
 
L10
L10L10
L10
lksoo
 
Pf cs102 programming-10 [structs]
Pf cs102 programming-10 [structs]Pf cs102 programming-10 [structs]
Pf cs102 programming-10 [structs]
Abdullah khawar
 
03 structures
03 structures03 structures
03 structures
Rajan Gautam
 
Python cheatsheat.pdf
Python cheatsheat.pdfPython cheatsheat.pdf
Python cheatsheat.pdf
HimoZZZ
 
Structures
StructuresStructures
Structures
archikabhatia
 
Lecture 9_Classes.pptx
Lecture 9_Classes.pptxLecture 9_Classes.pptx
Lecture 9_Classes.pptx
NelyJay
 
VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4
YOGESH SINGH
 
Unit-IV Strings.pptx
Unit-IV Strings.pptxUnit-IV Strings.pptx
Unit-IV Strings.pptx
samdamfa
 
Templates
TemplatesTemplates
Templates
Farwa Ansari
 
Cs1123 12 structures
Cs1123 12 structuresCs1123 12 structures
Cs1123 12 structuresTAlha MAlik
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handling
sanya6900
 
STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING
Gurwinderkaur45
 
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
rehan16091997
 
Apache Flink Training: DataStream API Part 2 Advanced
Apache Flink Training: DataStream API Part 2 Advanced Apache Flink Training: DataStream API Part 2 Advanced
Apache Flink Training: DataStream API Part 2 Advanced
Flink Forward
 
12Structures.pptx
12Structures.pptx12Structures.pptx
12Structures.pptx
Aneeskhan326131
 
Oop lec 3(structures)
Oop lec 3(structures)Oop lec 3(structures)
Oop lec 3(structures)Asfand Hassan
 

Similar to Structure in programming in c or c++ or c# or java (20)

02.adt
02.adt02.adt
02.adt
 
Unit 5 (1)
Unit 5 (1)Unit 5 (1)
Unit 5 (1)
 
L10
L10L10
L10
 
Pf cs102 programming-10 [structs]
Pf cs102 programming-10 [structs]Pf cs102 programming-10 [structs]
Pf cs102 programming-10 [structs]
 
03 structures
03 structures03 structures
03 structures
 
Python cheatsheat.pdf
Python cheatsheat.pdfPython cheatsheat.pdf
Python cheatsheat.pdf
 
Lab 13
Lab 13Lab 13
Lab 13
 
Structures
StructuresStructures
Structures
 
Lecture 9_Classes.pptx
Lecture 9_Classes.pptxLecture 9_Classes.pptx
Lecture 9_Classes.pptx
 
Bc0037
Bc0037Bc0037
Bc0037
 
VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4
 
Unit-IV Strings.pptx
Unit-IV Strings.pptxUnit-IV Strings.pptx
Unit-IV Strings.pptx
 
Templates
TemplatesTemplates
Templates
 
Cs1123 12 structures
Cs1123 12 structuresCs1123 12 structures
Cs1123 12 structures
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handling
 
STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING
 
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
 
Apache Flink Training: DataStream API Part 2 Advanced
Apache Flink Training: DataStream API Part 2 Advanced Apache Flink Training: DataStream API Part 2 Advanced
Apache Flink Training: DataStream API Part 2 Advanced
 
12Structures.pptx
12Structures.pptx12Structures.pptx
12Structures.pptx
 
Oop lec 3(structures)
Oop lec 3(structures)Oop lec 3(structures)
Oop lec 3(structures)
 

More from Samsil Arefin

Transmission Control Protocol and User Datagram protocol
Transmission Control Protocol and User Datagram protocolTransmission Control Protocol and User Datagram protocol
Transmission Control Protocol and User Datagram protocol
Samsil Arefin
 
Evolution Phylogenetic
Evolution PhylogeneticEvolution Phylogenetic
Evolution Phylogenetic
Samsil Arefin
 
Evolution Phylogenetic
Evolution PhylogeneticEvolution Phylogenetic
Evolution Phylogenetic
Samsil Arefin
 
Ego net facebook data analysis
Ego net facebook data analysisEgo net facebook data analysis
Ego net facebook data analysis
Samsil Arefin
 
Augmented Reality (AR)
Augmented Reality (AR)Augmented Reality (AR)
Augmented Reality (AR)
Samsil Arefin
 
Client server chat application
Client server chat applicationClient server chat application
Client server chat application
Samsil Arefin
 
Strings in programming tutorial.
Strings  in programming tutorial.Strings  in programming tutorial.
Strings in programming tutorial.
Samsil Arefin
 
Linked list searching deleting inserting
Linked list searching deleting insertingLinked list searching deleting inserting
Linked list searching deleting inserting
Samsil Arefin
 
Number theory
Number theoryNumber theory
Number theory
Samsil Arefin
 
Program to sort the n names in an alphabetical order
Program to sort the n names in an alphabetical orderProgram to sort the n names in an alphabetical order
Program to sort the n names in an alphabetical order
Samsil Arefin
 
Linked list int_data_fdata
Linked list int_data_fdataLinked list int_data_fdata
Linked list int_data_fdata
Samsil Arefin
 
Linked list Output tracing
Linked list Output tracingLinked list Output tracing
Linked list Output tracing
Samsil Arefin
 
Stack
StackStack
Sorting
SortingSorting
Sorting
Samsil Arefin
 
Fundamentals of-electric-circuit
Fundamentals of-electric-circuitFundamentals of-electric-circuit
Fundamentals of-electric-circuit
Samsil Arefin
 
Cyber security
Cyber securityCyber security
Cyber security
Samsil Arefin
 
C programming
C programmingC programming
C programming
Samsil Arefin
 
Data structure lecture 1
Data structure   lecture 1Data structure   lecture 1
Data structure lecture 1
Samsil Arefin
 
Structure and union
Structure and unionStructure and union
Structure and union
Samsil Arefin
 
String
StringString

More from Samsil Arefin (20)

Transmission Control Protocol and User Datagram protocol
Transmission Control Protocol and User Datagram protocolTransmission Control Protocol and User Datagram protocol
Transmission Control Protocol and User Datagram protocol
 
Evolution Phylogenetic
Evolution PhylogeneticEvolution Phylogenetic
Evolution Phylogenetic
 
Evolution Phylogenetic
Evolution PhylogeneticEvolution Phylogenetic
Evolution Phylogenetic
 
Ego net facebook data analysis
Ego net facebook data analysisEgo net facebook data analysis
Ego net facebook data analysis
 
Augmented Reality (AR)
Augmented Reality (AR)Augmented Reality (AR)
Augmented Reality (AR)
 
Client server chat application
Client server chat applicationClient server chat application
Client server chat application
 
Strings in programming tutorial.
Strings  in programming tutorial.Strings  in programming tutorial.
Strings in programming tutorial.
 
Linked list searching deleting inserting
Linked list searching deleting insertingLinked list searching deleting inserting
Linked list searching deleting inserting
 
Number theory
Number theoryNumber theory
Number theory
 
Program to sort the n names in an alphabetical order
Program to sort the n names in an alphabetical orderProgram to sort the n names in an alphabetical order
Program to sort the n names in an alphabetical order
 
Linked list int_data_fdata
Linked list int_data_fdataLinked list int_data_fdata
Linked list int_data_fdata
 
Linked list Output tracing
Linked list Output tracingLinked list Output tracing
Linked list Output tracing
 
Stack
StackStack
Stack
 
Sorting
SortingSorting
Sorting
 
Fundamentals of-electric-circuit
Fundamentals of-electric-circuitFundamentals of-electric-circuit
Fundamentals of-electric-circuit
 
Cyber security
Cyber securityCyber security
Cyber security
 
C programming
C programmingC programming
C programming
 
Data structure lecture 1
Data structure   lecture 1Data structure   lecture 1
Data structure lecture 1
 
Structure and union
Structure and unionStructure and union
Structure and union
 
String
StringString
String
 

Recently uploaded

AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
abh.arya
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
DuvanRamosGarzon1
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
ShahidSultan24
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
Kamal Acharya
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
Jayaprasanna4
 

Recently uploaded (20)

AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
 

Structure in programming in c or c++ or c# or java

  • 2. 2 P102 Prog. Fundamentals, Structures / Slide 2 Structures A Structure is a collection of related data items, possibly of different types. A structure type in C++ is called struct. A struct is heterogeneous in that it can be composed of data of different types. In contrast, array is homogeneous since it can contain only data of the same type.
  • 3. 3 P102 Prog. Fundamentals, Structures / Slide 3 Structures Structures hold data that belong together. Examples: Student record: student id, name, major, gender, start year, … Bank account: account number, name, currency, balance, … Address book: name, address, telephone number, … In database applications, structures are called records.
  • 4. 4 P102 Prog. Fundamentals, Structures / Slide 4 Structures Individual components of a struct type are called members (or fields). Members can be of different types (simple, array or struct). A struct is named as a whole while individual members are named using field identifiers. Complex data structures can be formed by defining arrays of structs.
  • 5. 5 P102 Prog. Fundamentals, Structures / Slide 5 struct basics Definition of a structure: struct <struct-type>{ <type> <identifier_list>; <type> <identifier_list>; ... } ; Example: struct Date { int day; int month; int year; } ; The “Date” structure has 3 members, day, month & year. Each identifier defines a member of the structure.
  • 6. 6 P102 Prog. Fundamentals, Structures / Slide 6 struct examples Example: struct StudentInfo{ int Id; int age; char Gender; double CGA; }; Example: struct StudentGrade{ char Name[15]; char Course[9]; int Lab[5]; int Homework[3]; int Exam[2]; }; The “StudentGrade” structure has 5 members of different array types. The “StudentInfo” structure has 4 members of different types.
  • 7. 7 P102 Prog. Fundamentals, Structures / Slide 7 struct examples Example: struct BankAccount{ char Name[15]; int AcountNo[10]; double balance; Date Birthday; }; Example: struct StudentRecord{ char Name[15]; int Id; char Dept[5]; char Gender; }; The “StudentRecord” structure has 4 members. The “BankAcount” structure has simple, array and structure types as members.
  • 8. 8 P102 Prog. Fundamentals, Structures / Slide 8 struct basics Declaration of a variable of struct type: <struct-type> <identifier_list>; Example: StudentRecord Student1, Student2; Student1 and Student2 are variables of StudentRecord type. Student1 Student2 Name Id Gender Dept Name Id Gender Dept
  • 9. 9 P102 Prog. Fundamentals, Structures / Slide 9 Chan Tai Man 12345 M COMP Ex. 1: struct basics The members of a struct type variable are accessed with the dot (.) operator: <struct-variable>.<member_name>; Example: strcpy(Student1.Name, "Chan Tai Man"); Student1.Id = 12345; strcpy(Student1.Dept, "COMP"); Student1.gender = 'M'; cout << "The student is "; switch (Student1.gender){ case 'F': cout << "Ms. "; break; case 'M': cout << "Mr. "; break; } Student1 Name Id Gender Dept
  • 10. 10 102 Prog. Fundamentals, Structures / Slide 10
  • 11. 11 102 Prog. Fundamentals, Structures / Slide 11 Chan Tai Man 12345 M COMP Ex. 2: struct-to-struct assignment The values contained in one struct type variable can be assigned to another variable of the same struct type. Example: strcpy(Student1.Name, "Chan Tai Man"); Student1.Id = 12345; strcpy(Student1.Dept, "COMP"); Student1.gender = 'M'; Student2 = Student1; Student1 Chan Tai Man 12345 M COMP Student2
  • 12. 12 102 Prog. Fundamentals, Structures / Slide 12
  • 13. 13 102 Prog. Fundamentals, Structures / Slide 13 Ex. 3-5: Nested structures We can nest structures inside structures. Examples: struct point{ double x, y; }; point P; struct line{ point p1, p2; }; line L; struct triangle{ point p1, p2, p3; }; triangle T; (P.x, P.y) (L.p1.x, L.p1.y) (L.p2.x, L.p2.y) (T.p2.x, T.p2.y) (T.p1.x, T.p1.y) (T.p3.x, T.p3.y)
  • 14. 14 102 Prog. Fundamentals, Structures / Slide 14 Ex. 3-5: Nested structures We can nest structures inside structures. struct line{ point p1, p2; }; line L; (L.p1.x, L.p1.y) (L.p2.x, L.p2.y) line p1 p2 x y x y
  • 15. 15 102 Prog. Fundamentals, Structures / Slide 15 Ex. 3-5: Nested structures Assign values to the variables P, L, and T using the picture: point P; line L; triangle T; (4, 11) (2, 7) (10, 9) (6, 5) (2, 0) (8, 3) Ex. 3: Graph a point Ex. 4: Graph a line Ex. 5: Graph a triangle
  • 16. 16 102 Prog. Fundamentals, Structures / Slide 16 Ex. 3-5: Nested structures point P; line L; triangle T; P.x = 4; P.y = 11; (4, 11) (2, 7) (10, 9) (6, 5) (2, 0) (8, 3) L.p1.x = 2; L.p1.y = 7; L.p2.x = 10; L.p2.y = 9; T.p1.x = 2; T.p1.y = 0; T.p2.x = 6; T.p2.y = 5; T.p3.x = 8; T.p3.y = 3;
  • 17. 17 102 Prog. Fundamentals, Structures / Slide 17 Ex. 3: Graphing a Point x 5 4 3 2 1 1514131211100876543210 y (x1, y1) *
  • 18. struct point {int x, y;}; void user_input(point&); void graph_point(char grid[NUMBER_ROWS][NUMBER_COLS], point); void print_grid(char grid[NUMBER_ROWS][NUMBER_COLS]); void set_background(char grid[][NUMBER_COLS]); void user_input(point& P){ // pass by reference // get user input and check that it is on the grid do{ cout << "Enter column (x<" << NUMBER_COLS << ") & row (y<" << NUMBER_ROWS <<") of the 1st point: "; cin >> P.x >> P.y; } while ((P.y<0) || (P.y >= NUMBER_ROWS) || (P.x<0) || (P.x >= NUMBER_COLS)); } // Put a point on the grid void graph_point(char grid[][NUMBER_COLS], point P){ grid[P.y][P.x] = '*'; }
  • 19. 19 102 Prog. Fundamentals, Structures / Slide 19
  • 20. 20 102 Prog. Fundamentals, Structures / Slide 20 Ex. 4: Trajectory Between 2 Points x *5 4 3 2 *1 1514131211100876543210 y • The equation of a line going through two points (x1, y1) & (x2, y2) can be represented by: (y-y1)/ (x-x1) = (y2-y1)/(x2-x1) or y = ((y2-y1)/(x2-x1)) (x-x1) + y1 where (y -y )/(x -x ) is called the slope. (x1, y1) (x2, y2) * * *
  • 21. // use struct to graph line between two points #include <ctype> // for tolower #include <iostream> using namespace std; int const NUMBER_ROWS = 11; int const NUMBER_COLS = 31; struct point {int x, y;}; struct line {point p1, p2;}; void user_input (line&); void graph_line (char grid[NUMBER_ROWS][NUMBER_COLS], line); void print_grid(char grid[NUMBER_ROWS][NUMBER_COLS]); void set_background (char grid[][NUMBER_COLS]);
  • 22. // Graph line between two points int main(){ // set an array for the grid char grid[NUMBER_ROWS][NUMBER_COLS]; line line1; int row1=0, col1=0, row2=0, col2=0; char do_another; // function call to fill background of grid with ‘-’ set_background(grid); do{ // do-while loop allows multiple lines // get user input user_input(line1); // put ‘*’ into array to graph the line(s) on the grid graph_line(grid, line1); // print the grid from the array to the screen print_grid(grid); cout << "Do you want to add another line (y/n)? "; cin >> do_another; do_another = tolower(do_another); } while (do_another == 'y'); return 0; }
  • 23. 23 102 Prog. Fundamentals, Structures / Slide 23 Ex. 4: More on Graphing Line //A function to get user input and check that it is on the grid. void user_input(line& line1){ do{ cout << "Enter column (x<" << NUMBER_COLS << ") & row (y<" << NUMBER_ROWS << ") coordinates of the 1st point: "; cin >> line1.p1.x >> line1.p1.y; } while ((line1.p1.y<0) || (line1.p1.y>=NUMBER_ROWS) || (line1.p1.x<0) || (line1.p1.x >= NUMBER_COLS)); // use another do-while loop for the 2nd point, col2 and row2 }
  • 24. 24 102 Prog. Fundamentals, Structures / Slide 24 Ex. 4: More on Graphing Line void graph_line(char grid[][NUMBER_COLS], line line1){ int row, col; double rise, run, slope; // one point if((line1.p1.y==line1.p2.y)&&(line1.p1.x==line2.p2.x)) grid[line1.p1.y][line1.p1.x] = '*'; else if(line1.p2.x==line1.p1.x){ // infinite slope if (line1.p1.y < line1.p2.y){ for(row=line1.p1.y; row <= line1.p2.y; row++) grid[row][line1.p1.x] = '*'; } else{ for(row=line1.p1.y; row >= line1.p2.y; row--) grid[row][line1.p1.x] = '*'; } }
  • 25. 25 102 Prog. Fundamentals, Structures / Slide 25 Ex. 4: More on Graphing Line else{ rise=line1.p2.y-line1.p1.y; run=line1.p2.x-line1.p1.x; slope = (double)rise / run; // run cannot = 0 if (run >0){ for(col = line1.p1.x; col <= line1.p2.x; col++){ // line1.p1.y is offset for starting point row=(int)(slope*(col–line1.p1.x)+line1.p1.y); grid[row][col] = '*'; } } else{ for(col=line1.p1.x; col >= line1.p2.x; col--){ row=(int)(slope*(col–line1.p1.x)+line1.p1.y); grid[row][col] = '*'; } } } }
  • 26. 26 102 Prog. Fundamentals, Structures / Slide 26
  • 27. 27 102 Prog. Fundamentals, Structures / Slide 27 Arrays of structures An ordinary array: One type of data An array of structs: Multiple types of data in each array element. 0 1 2 … 98 99 0 1 2 … 98 99
  • 28. 28 102 Prog. Fundamentals, Structures / Slide 28 Arrays of structures We often use arrays of structures. Example: StudentRecord Class[100]; strcpy(Class[98].Name, "Chan Tai Man"); Class[98].Id = 12345; strcpy(Class[98].Dept, "COMP"); Class[98].gender = 'M'; Class[0] = Class[98]; . . . 0 1 2 … 98 99 Chan Tai Man 12345 M COMP
  • 29. 29 102 Prog. Fundamentals, Structures / Slide 29 Arrays inside structures We can use arrays inside structures. Example: struct square{ point vertex[4]; }; square Sq; Assign values to Sq using the given square (4, 3) (10, 3) (4, 1) (10, 1) x y x y x y x y