Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structures.

Similar presentations


Presentation on theme: "Structures."— Presentation transcript:

1 Structures

2 Introduction Structures are used to define related data in one construct. An example of a structure: typedef struct{ char name[20]; double avg; int ID; } student; student s;

3 Introduction (cont.) Another way to define a structure:
struct student{ char name[20]; double avg; int ID; }; student s;

4 Structure I/O student s; cin>>s.name; cin>>s.avg;
cin>>s.ID; cout<<s.name; cout<<s.avg; cout<<s.ID;

5 Structure Assignment student s1={“ali“,70.5,2222}, s2;
s2.name=s1.name; //compiler error strcpy(s2.name,s1.name); s2.avg=s1.avg; s2.ID=s1.ID; //or s2=s1; //ok structures cannot be compared: if (s1==s2) { //compiler error }

6 Structure Comparison structures cannot be compared: student s={…},t;
if (s1==s2) { //compiler error } if (strcmp(s.name,t.name)==0 && s.avg==t.avg && s.ID==t.ID){ // ok

7 Arrays of structures struct student{char name[20]; int avg;};
student A[50]; //Read the data from the key board //Display the name of the student having maximum score //Print names starting with ‘a’ //Sort the records in ascending order of the average // Sort the records according to alphabetical order of names

8 Pointers to Structures
student s={“ali”,22222,90}; student * sptr; sptr=&s; cout<<(*sptr).name; cout<<sptr->name; sptr=new student; cin>>sptr->name; cin>>sptr->avg; cin>>sptr->ID;

9 Arrays of Pointers to Structures
student * A[100]; for (int i=0; i<=99; i++){ A[i]=new student; } //Read the data from the key board //Display the name of the student having maximum score //Print names starting with ‘a’ //Sort the records in ascending order of the average and then according to //alphabetical order of names. Exchange structures //alphabetical order of names. Exchange pointers.

10 Nested Structures A structure can contain another structure: Example:
struct address{ char street[20]; char city[20]; char country[20] }; struct employee{ address a; char name[20]; int ID; float salary; employee e; // nested structure cin>>e.a.street;

11 Passing Structure by Value
struct point{double x; double y}; point midpoint(point p1, point p2){ point mid; mid.x=(p1.x+p2.x)/2.0; mid.y=(p1.y+p2.y)/2.0; return mid; } point a={0.0,0.0}, b={5.0,10.0}, m; m=midpoint(a,b);

12 Passing Structures by Address
void set_midpoint(point p1, point p2, point * mid){ mid->x=(p1.x+p2.x)/2.0; mid->y=(p1.y+p2.y)/2.0; } point a={0.0,0.0}, b={5.0,10.0}, m; set_midpoint(a,b,&m);

13 Passing Structures by Reference
void set_midpoint(point p1, point p2, point & mid){ mid.x=(p1.x+p2.x)/2.0; mid.y=(p1.y+p2.y)/2.0; } point a={0.0,0.0}, b={5.0,10.0}, m; set_midpoint(a,b,m);

14 Exercizes Complete the following functions to find the student with the highest average and give an example on calling each of these. student highest (student A[ ], int size){…} student * highest(student A[ ], int size){…} student highest(student *A[ ], int size){…} void highest(student A[ ], int size, student& h){…}


Download ppt "Structures."

Similar presentations


Ads by Google