STRUCTURES IN C LANGAUGE Setting By A L
STRUCTURES IN C++ LANGAUGE Setting By : A. L. Waleed Rasheed
Structures: ﺍﻟﺴﺠﻼﺕ ﺍﻟﻘﻴﻮﺩ ﺍﻭ Syntax Struct [struct type name]{ [type variable_name; ] : } [structures variables]; Note : use struct to group variables into single record. Example: my_stud Struct my_struct { name avr Char name[30]; Float avr; char* float } my_stud; To access elements in a structure use a record selector(. ), like this: my_stud. name or my_stud. avr And to declare additional variables from structures write this: struct my_stud[100];
- Sample Example : #include <iostream. h> stu void main(void) { struct { int id; float avr; } stu; cin>>stu. id; cin>>stu. avr; cout<<"Student No. "<<stu. id<<endl; cout<<"average="<<stu. avr; } id avr int float
EX//read 10 record to students contains (id, avr ) fields? #include <iostream. h> void main() { struct { int id; float avr; } stu[10]; for(int i=0; i<10; i++) { cin>>stu[i]. id; cin>>stu[i]. avr; } for(i=0; i<10; i++) { cout<<"Student No. "<<stu[i]. id<<endl; cout<<"average="<<stu[i]. avr<<endl; } } stu[10] id avr 1 44 2 66 3 88 4 77 5 44 6 60 7 90 8 65 9 77 10 87
EX//W. P. section to read 10 record to students contains (id, avr ) fields and find id to largest average? larg=stu[0]. avr; for (i=0; i<10; i++) if (stu[i]. avr>larg) { larg=stu[i]. avr; loc=i; } cout<<stu[loc]. id<<stu[loc]. avr; } id avr 1 44 2 66 3 88 4 77 5 44 6 60 7 90 8 65 9 77 10 87
EX//W. P. to read 10 record to students contains (name , avr ) fields and find count of succ. student? #include<iostream. h> #include<stdio. h> void main() { struct { char name[80]; float avr; } stu[10]; for (int i=0; i<10; i++) { gets(stu[i]. name); cin>>stu[i]. avr; } int k=0; for (int i=0; i<10; i++) if (stu[i]. avr>=50) k++; cout<<"count of succ. student = "<<k; } stu[10] name avr ali 44 alaa 66 raad 88 rana 77 zahraa 44 salam 60 ahmed 90 walaa 65 maha 77 wasam 87
Q//An example about Nested structure: #include<iostream. h> struct student { char name[80]; int age; struct birth_date { int year; int month; int day; } ddd; }; void main() { student stud; cin>>stud. name; cin>>stud. age; cin>>stud. ddd. year; cin>>stud. ddd. month; cin>>stud. ddd. day; if (stud. age>60) cout<<"finshed"<<endl; }
Homework Q 1 / W. P. section to read 40 record to students contains (name, avr ) fields and find count of succ. and fail? Q 2/ W. P. section to read 40 record to students contains (id, avr ) fields and apply descending sort depending on average?
- Slides: 8