CSC113 Computer Programming Lecture 10 Structures 1 Structures

  • Slides: 24
Download presentation
CSC-113 Computer Programming Lecture 10 – Structures 1

CSC-113 Computer Programming Lecture 10 – Structures 1

Structures 2

Structures 2

Structure • Structure is another user defined data type which allows you to combine

Structure • Structure is another user defined data type which allows you to combine data items of different kinds. • The variables in a structure can be of different types: Some can be int, some can be float, and so on. • The data items in a structure are called the members of the structure. 3

example • Suppose you want to store data about the book. You might want

example • Suppose you want to store data about the book. You might want to store its name( a string) , its price(a float), and number of pages(an int). • If data about say 3 such books is to be stored, then we can follow two approaches. 1. Construct individual arrays. For price , pages and name. 2. Use a structure variable

Program using arrays #include<iostream> Using namespace std; int main() { char name[3]; float price[3];

Program using arrays #include<iostream> Using namespace std; int main() { char name[3]; float price[3]; int pages[3]; cout<<“names – pages – price”; for(int I =0; i<3; i++) cin>>name[i] >> pages[i] >> price[i]; for(int I =0; i<3; i++) cout<<name[i] << pages[i] << price[i]; return 0; }

Solve using Structures #include<iostream> using namespace std; int main() { struct book { char

Solve using Structures #include<iostream> using namespace std; int main() { struct book { char name[50]; int pages; float price }; book b 1, b 2 , b 3; cin>> b 1. name >> b 1. price>> b 1. pages; cin>> b 2. name >> b 2. price>> b 2. pages; cin>> b 3. name >> b 3. price>> b 3. pages; cout<<b 1. name <<b 1. price <<b 1. pages; cout<< b 2. name << b 2. price<< b 2. pages; cout<< b 3. name << b 3. price << b 3. pages;

Defining the Structure struct part { int modelnumber; int partnumber; float cost; } ;

Defining the Structure struct part { int modelnumber; int partnumber; float cost; } ; 7

Defining a Structure Variable • part 1; • defines a variable, called part 1,

Defining a Structure Variable • part 1; • defines a variable, called part 1, of type structure part. • This definition reserves space in memory for part 1. 8

9

9

Accessing Structure Members • Members can be accessed using something called the dot operator.

Accessing Structure Members • Members can be accessed using something called the dot operator. Here’s how the first member is given a value: • part 1. modelnumber = 6244; • . Is called member access operator 10

Example 1 11

Example 1 11

Initializing Structure Members • Initializer lists part 1= { 6244, 373, 217. 55 };

Initializing Structure Members • Initializer lists part 1= { 6244, 373, 217. 55 }; • Assignment statements part 1; part 1. modelnumber = 6244; part 1. partnumber = 373; Part 1. cost = 217. 55; part 2; //define variable part 2 = part 1; 12

Example 2 13

Example 2 13

Nested Structures can also be nested in such a way that an element of

Nested Structures can also be nested in such a way that an element of a structure is itself another structure: 14

ARRAYS OF STRUCTURES • If we want to store data of 100 books, we

ARRAYS OF STRUCTURES • If we want to store data of 100 books, we would be required to use 100 diff structure variables from b 1 to b 100. . • Which is definitely not convenient. . • A better approach would be to use an array of structures.

int main(){ struct book { string name; float price; int pages; }; book b[100];

int main(){ struct book { string name; float price; int pages; }; book b[100]; for (int I =0 ; i<=99 ; i++) { cout<<“enter name price and pages”; cin>>b[i]. name>>b[i]. pages>> b[i]. price; } for (int i =0 ; i<=99 ; i++) cout<<b[i]. name<<b[i]. pages<<b[i]. price; return 0; }

Enumerations • A different approach to defining your own data type is the enumeration.

Enumerations • A different approach to defining your own data type is the enumeration. • Allowing you to define your own data types, they can simplify and clarify your programming. • Set of integers represented by identifiers • Enumeration constants - like symbolic constants whose values automatically set • Values start at 0 and are incremented by 1 • Values can be set explicitly with = • Need unique constant names • Declare variables as normal • Enumeration variables can only assume their enumeration constant values (not the integer representations) 17

 • Example: enum Months { JAN = 1, FEB, MAR, APR, MAY, JUN,

• Example: enum Months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC }; • Starts at 1, increments by 1

Days of the Week 20

Days of the Week 20

Structures as function arguments • Like an ordinary variable, a structure variable can also

Structures as function arguments • Like an ordinary variable, a structure variable can also be passed to a function. • We may either pass individual structure elements or the entire structure variable at one go.

Passing individual elements void display(char[], char[] , int); int main() { struct book {char

Passing individual elements void display(char[], char[] , int); int main() { struct book {char names[25], char author[25], int callno; } ; struct book b 1 = {“c++”, ”frozen”, 101}; display(b 1. names, b 1. author, b 1. callno); return 0; } void display(char s[], char t[], int n) { cout<<n<<s<<t; } OUTPUT: C++ frozen 101

Passing entire structure variable struct book {char names[25], char author[25], int callno; } ;

Passing entire structure variable struct book {char names[25], char author[25], int callno; } ; void display(struct book); int main() { struct book b 1 = {“c++”, ”frozen”, 101} display(b 1); return 0; } void display(struct book b) { cout<<b. name<<b. author<<b. callno; } OUTPUT: C++ frozen 101

24

24