Programming Structures COMP 102 Prog Fundamentals Structures Slide

  • Slides: 36
Download presentation
Programming Structures

Programming Structures

COMP 102 Prog. Fundamentals, Structures / Slide 2 Structures l A Structure is a

COMP 102 Prog. Fundamentals, Structures / Slide 2 Structures l A Structure is a collection of related data items, possibly of different types. l A structure type in C++ is called struct. l A struct is heterogeneous in that it can be composed of data of different types. l In contrast, array is homogeneous since it can contain only data of the same type.

COMP 102 Prog. Fundamentals, Structures / Slide 3 Defination l A data structure is

COMP 102 Prog. Fundamentals, Structures / Slide 3 Defination l A data structure is a group of data elements grouped together under one name. These data elements, known as members, can have different types and different lengths. Data structures can be declared in C++ using the struct type_name { following syntax: member_type 1 member_name 1; member_type 2 member_name 2; member_type 3 member_name 3; . . } object_names;

COMP 102 Prog. Fundamentals, Structures / Slide 4 Structures l l l Structures hold

COMP 102 Prog. Fundamentals, Structures / Slide 4 Structures l l l Structures hold data that belong together. Examples: n Student record: student id, name, major, gender, start year, … n Bank account: account number, name, currency, balance, … n Address book: name, address, telephone number, … In database applications, structures are called records.

COMP 102 Prog. Fundamentals, Structures / Slide 5 Structures l Individual components of a

COMP 102 Prog. Fundamentals, Structures / Slide 5 Structures l Individual components of a struct type are called members (or fields). l Members can be of different l A struct is named as a whole while individual members are named using field identifiers. l Complex data structures can be formed by defining arrays of structs.

COMP 102 Prog. Fundamentals, Structures / Slide 6 struct basics l Definition of a

COMP 102 Prog. Fundamentals, Structures / Slide 6 struct basics l Definition of a structure: struct <struct-type>{ <type> <identifier_list>; . . . } ; l Each identifier defines a member of the structure. Example: struct Date { int day; int month; int year; } ; The “Date” structure has 3 members, day, month & year.

COMP 102 Prog. Fundamentals, Structures / Slide 7 struct examples l Example: struct Student.

COMP 102 Prog. Fundamentals, Structures / Slide 7 struct examples l Example: struct Student. Info{ int Id; int age; char Gender; double CGA; }; l The “Student. Info” structure has 4 members of different types. Example: struct Student. Grade{ char Name[15]; char Course[9]; int Lab[5]; int Homework[3]; int Exam[2]; }; The “Student. Grade” structure has 5 members of different array types.

COMP 102 Prog. Fundamentals, Structures / Slide 8 struct examples l Example: struct Bank.

COMP 102 Prog. Fundamentals, Structures / Slide 8 struct examples l Example: struct Bank. Account{ char Name[15]; int Acount. No[10]; double balance; Date Birthday; }; l The “Bank. Acount” structure has simple, array and structure types as members. Example: struct Student. Record{ char Name[15]; int Id; char Dept[5]; char Gender; }; The “Student. Record” structure has 4 members.

COMP 102 Prog. Fundamentals, Structures / Slide 9 struct basics l Declaration of a

COMP 102 Prog. Fundamentals, Structures / Slide 9 struct basics l Declaration of a variable of struct type: <struct-type> <identifier_list>; l Example: Student. Record Student 1, Student 2; Name Student 1 Id Dept Name Gender Id Gender Student 2 Dept Student 1 and Student 2 are variables of Student. Record type.

COMP 102 Prog. Fundamentals, Structures / Slide 10 struct basics struct product { int

COMP 102 Prog. Fundamentals, Structures / Slide 10 struct basics struct product { int weight; double price; } ; product apple; product banana, melon; struct product { int weight; double price; } apple, banana, melon;

COMP 102 Prog. Fundamentals, Structures / Slide 11 Structure E. g struct part {

COMP 102 Prog. Fundamentals, Structures / Slide 11 Structure E. g struct part { int modelnumber; int partnumber; float cost; }; int main () { part 1, part 2; part 1. modelnumber = 1111; part 1. partnumber = 111; part 1. cost = 111. 11; Initializing data members

COMP 102 Prog. Fundamentals, Structures / Slide 12 Structure E. g cout<<“Enter Model number

COMP 102 Prog. Fundamentals, Structures / Slide 12 Structure E. g cout<<“Enter Model number = ”; cin>>part 2. modelnumber ; . . … …. . ; cin>>part 2. partnumber ; . . . . ; cin>>part 2. cost ; cout<<"n. Model of Part 1 = "<<part 1. modelnumber; cout<<"n. Part of part 1 = "<<part 1. partnumber; cout<<"n. Cost of part 1 = "<<part 1. cost<<endl; cout<<"n. Model of part 2 = "<<part 2. modelnumber; cout<<"n. Part of part 2 = "<<part 2. partnumber; cout<<"n. Cost of part 2 = "<<part 2. cost<<endl; return 0; }

COMP 102 Prog. Fundamentals, Structures / Slide 13 (Out Put) Model of Part 1

COMP 102 Prog. Fundamentals, Structures / Slide 13 (Out Put) Model of Part 1 = 1111 Part of part 1 = 111 Cost of part 1 = 111. 11 Model of part 2 = 222 Part of part 2 = 2222 Cost of part 2 = 222 Press any key to continue

COMP 102 Prog. Fundamentals, Structures / Slide 14 Keyword Structure definition struct part Structure

COMP 102 Prog. Fundamentals, Structures / Slide 14 Keyword Structure definition struct part Structure name { int modelnumber; int partnumber; float cost; }; Semicolon terminate definition Braces Delimit Structure Members

COMP 102 Prog. Fundamentals, Structures / Slide 15 Structure definition The structure definition serves

COMP 102 Prog. Fundamentals, Structures / Slide 15 Structure definition The structure definition serves only as a blueprint for the creation of variables of type part. It does not itself create any structure variables; that is, it does not set aside any space in memory or even name any variables. This is unlike the definition of a simple variable, which does set aside memory.

COMP 102 Prog. Fundamentals, Structures / Slide 16 The first statement in main() part

COMP 102 Prog. Fundamentals, Structures / Slide 16 The first statement in main() part Structure Name part 1, part 2; Variable Names

COMP 102 Prog. Fundamentals, Structures / Slide 17 Memory defines two variables of type

COMP 102 Prog. Fundamentals, Structures / Slide 17 Memory defines two variables of type structure part. The definition reserve space in memory for part 1 & part 2. How Much Space? Enough to hold all the members of part 1 & part 2. part 1 modelnumber = 4 byte partnumber = 4 byte cos t= 4 byte (Because int and float data type take 4 byte ) part 2 modelnumber = 4 byte partnumber = 4 byte cos t= 4 byte

COMP 102 Prog. Fundamentals, Structures / Slide 18 Accessing Structure members Members of part

COMP 102 Prog. Fundamentals, Structures / Slide 18 Accessing Structure members Members of part 1. modelnumber = 1111; part 1. partnumber = 111; part 1. cost = 111. 11;

COMP 102 Prog. Fundamentals, Structures / Slide 19 Accessing Structure members Members of part

COMP 102 Prog. Fundamentals, Structures / Slide 19 Accessing Structure members Members of part 2 Entered by user part 2. modelnumber = 222; part 2. partnumber = 2222; part 2. cost = 222;

COMP 102 Prog. Fundamentals, Structures / Slide 20 Accessing Structure members l Can access

COMP 102 Prog. Fundamentals, Structures / Slide 20 Accessing Structure members l Can access using “dot operator” l Also know as member “access operator” l The variable name must be used to distinguish one variable from another, such as part 1, part 2.

COMP 102 Prog. Fundamentals, Structures / Slide 21 Initializing Structures Members part 2 =

COMP 102 Prog. Fundamentals, Structures / Slide 21 Initializing Structures Members part 2 = {1980, 321, 22. 22};

COMP 102 Prog. Fundamentals, Structures / Slide 22 struct part { int modelnumber; int

COMP 102 Prog. Fundamentals, Structures / Slide 22 struct part { int modelnumber; int partnumber; float cost; }; int main (){ part 1, part 3; ; part 2 = {222, 21232, 22. 22}; cin>>part 1. modelnumber; cin>>part 1. partnumber; cin>>part 1. cost;

COMP 102 Prog. Fundamentals, Structures / Slide 23 part 3 = part 1; cout<<"n.

COMP 102 Prog. Fundamentals, Structures / Slide 23 part 3 = part 1; cout<<"n. Model of Part 1 = "<<part 1. modelnumber; cout<<"n. Part of part 1 = "<<part 1. partnumber; cout<<"n. Cost $ of part 1 = "<<part 1. cost<<endl; cout<<"n. Initail Model = "<<part 2. modelnumber; cout<<"n. Initial Part = "<<part 2. partnumber; cout<<"n. Initial Cost = "<<part 2. cost<<endl; cout<<"n. Initail Model = "<<part 3. modelnumber; cout<<"n. Initial Part = "<<part 3. partnumber; cout<<"n. Initial Cost = "<<part 3. cost<<endl; return 0; }

COMP 102 Prog. Fundamentals, Structures / Slide 24 Out Put Model of Part 1

COMP 102 Prog. Fundamentals, Structures / Slide 24 Out Put Model of Part 1 = 1111 Part of part 1 = 111 Cost $ of part 1 = 111. 11 Initail Model = 222 Initial Part = 21232 Initial Cost = 22. 22 Initail Model = 1111 Initial Part = 111 Initial Cost = 111. 11 Press any key to continue

COMP 102 Prog. Fundamentals, Structures / Slide 25 Example Write a program in C++

COMP 102 Prog. Fundamentals, Structures / Slide 25 Example Write a program in C++ that shows the area of 3 room's. Using Structure namely "distance". Take input of feet & inches from user for variable d 1 (feet & inches), assign variable d 2 = {10, 5. 25} values. Now add feet and inches of d 1 & d 2 and store in d 3. Display d 1 (feet & inches) d 2 (feet & inches) d 3 (feet & inches) separately. Put Condition if d 1 & d 2 inches increase by 12 it become a foot.

COMP 102 Prog. Fundamentals, Structures / Slide 26 Code struct Distance { int feet;

COMP 102 Prog. Fundamentals, Structures / Slide 26 Code struct Distance { int feet; float inches; }; void main() { Distance d 1, d 3; Distance d 2= { 11, 6. 25 }; cout<<“n Enter feet: ”; cin>>d 1. feet; cout<< “Enter inches: ”; cin>>d 1. inches; d 3. inches= d 1. inches + d 2. inches; d 3. feet=d 1. feet+ d 2. feet; if(d 3. inches>=12. 0) { d 3. inches=d 3. inches – 12. 0; d 3. feet++; } cout<< d 1. feet<<“---”<<d 1. inches<<“ + “; cout<<d 2. feet<<“---”<<d 2. inches<<“ = “; cout<<d 3. feet<<“---”<<d 3. inches; }

COMP 102 Prog. Fundamentals, Structures / Slide 27 Defining of objects struct product {

COMP 102 Prog. Fundamentals, Structures / Slide 27 Defining of objects struct product { int weight; double price; } apple, banana, melon;

COMP 102 Prog. Fundamentals, Structures / Slide 28 cout << "Enter title: "; getline

COMP 102 Prog. Fundamentals, Structures / Slide 28 cout << "Enter title: "; getline (cin, yours. title); cout << "Enter year: "; getline (cin, mystr); stringstream(mystr) >> yours. year; #include <iostream> #include <string> #include <sstream> using namespace std; struct movies_t { string title; int year; } mine, yours; void printmovie (movies_t movie); int main () { string mystr; mine. title = "2001 A Space Odyssey"; mine. year = 1968; cout << "My favorite movie is: n "; printmovie (mine); cout << "And yours is: n "; printmovie (yours); return 0; } void printmovie (movies_t movie) { cout << movie. title; cout << " (" << movie. year << ")n"; }

COMP 102 Prog. Fundamentals, Structures / Slide 29 Arrays of structures l An ordinary

COMP 102 Prog. Fundamentals, Structures / Slide 29 Arrays of structures l An ordinary array: One type of data 0 l 1 2 … 98 99 An array of structs: Multiple types of data in each array element. 0 1 2 … 98 99

COMP 102 Prog. Fundamentals, Structures / Slide 30 Arrays of structures l l We

COMP 102 Prog. Fundamentals, Structures / Slide 30 Arrays of structures l l We often use arrays of structures. Example: Student. Record Class[100]; strcpy(Class[98]. Name, "Chan Tai Man"); Class[98]. Id = 12345; strcpy(Class[98]. Dept, "COMP"); Chan Tai Man Class[98]. gender = 'M'; 12345 M Class[0] = Class[98]; COMP . . . 0 1 2 … 98 99

COMP 102 Prog. Fundamentals, Structures / Slide 31 Arrays inside structures l l We

COMP 102 Prog. Fundamentals, Structures / Slide 31 Arrays inside structures l l We can use arrays inside structures. Example: (4, 3) struct square{ point vertex[4]; }; square Sq; l (4, 1) (10, 3) (10, 1) Assign values to Sq using the given square x y x y

COMP 102 Prog. Fundamentals, Structures / Slide 32 Example Create a structure called emp

COMP 102 Prog. Fundamentals, Structures / Slide 32 Example Create a structure called emp that contains three members, int id, char name[100], float sal. Ask the user to fill in data for three employees and then display information for each employee. Hint l Variable of struct emp will be array l Use while / for loop to control array 32

COMP 102 Prog. Fundamentals, Structures / Slide 33 Que 3 (Code) #include<iostream. h> struct

COMP 102 Prog. Fundamentals, Structures / Slide 33 Que 3 (Code) #include<iostream. h> struct emp { int id; char name[100]; float sal; }; void main () { emp ob 1[3]; int c=0; 33 //Index Variable

COMP 102 Prog. Fundamentals, Structures / Slide 34 Que 3 (Code) while(c<3) { cout<<"Enter

COMP 102 Prog. Fundamentals, Structures / Slide 34 Que 3 (Code) while(c<3) { cout<<"Enter ID of Employee "<<c+1<<" = "; cin>>ob 1[c]. id; cout<<"Enter name of Employee "<<c+1<<" = "; cin>>ob 1[c]. name; cout<<"Enter salary of Employee "<<c+1<<" = "; cin>>ob 1[c]. sal; c++; cout<<"nn"; } 34

COMP 102 Prog. Fundamentals, Structures / Slide 35 Que 3 (Code) c = 0;

COMP 102 Prog. Fundamentals, Structures / Slide 35 Que 3 (Code) c = 0; while (c<3) { cout<<"n. Id of emp "<<c+1<<" = "<<ob 1[c]. id; cout<<"n. Name of emp "<<c+1<<" = "<<ob 1[c]. name; cout<<"n. Salary ofemp "<<c+1<<" = "<<ob 1[c]. sal; c++; cout<<"nn"; } } 35

COMP 102 Prog. Fundamentals, Structures / Slide 36 Que 3 (Out Put) Enter ID

COMP 102 Prog. Fundamentals, Structures / Slide 36 Que 3 (Out Put) Enter ID of Employee 1 = 123 Enter name of Employee 1 = Yasir Enter salary of Employee 1 = 50000 Enter ID of Employee 2 = 124 Enter name of Employee 2 = Ali Enter salary of Employee 2 = 55000 Enter ID of Employee 3 = 125 Enter name of Employee 3 = Aamir Enter salary of Employee 3 = 65000 Id of emp 1 = 123 Name of emp 1 = Yasir Salary ofemp 1 = 50000 Id of emp 2 = 124 Name of emp 2 = Ali Salary ofemp 2 = 55000 Id of emp 3 = 125 Name of emp 3 = Aamir Salary ofemp 3 = 65000 Press any key to continue 36