cs201- introduction to programming- lecture 20
DESCRIPTION
Virtual University Course CS201- Introduction to Programming Lecture No 20 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]TRANSCRIPT
![Page 1: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/1.jpg)
Introduction to ProgrammingIntroduction to Programming
Lecture 20Lecture 20
![Page 2: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/2.jpg)
In the Last Two In the Last Two LecturesLectures
File HandlingFile Handling– Sequential FilesSequential Files
– Random Access FilesRandom Access Files
![Page 3: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/3.jpg)
Today’s LectureToday’s Lecture
StructuresStructures UnionsUnions
![Page 4: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/4.jpg)
StructureStructure
??
![Page 5: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/5.jpg)
Structure Structure definitiondefinition
struct Studentstruct Student
{{
char name [ 60 ] ;char name [ 60 ] ;char address [ 100 ] ; char address [ 100 ] ; Data of the structure Data of the structure
double gpa ;double gpa ;
} ;} ;
Structure name
}Keyword
![Page 6: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/6.jpg)
Student s1 , s2 , s3 ;Student s1 , s2 , s3 ;
Structure Name Variable Names}
![Page 7: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/7.jpg)
StructureStructurestruct addressstruct address{{
char streetAddress char streetAddress [ 100 ] ;[ 100 ] ;char city [ 30 ] ;char city [ 30 ] ;char country [ 30 ] ;char country [ 30 ] ;
}}
![Page 8: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/8.jpg)
StructureStructurestruct Studentstruct Student
{{
char name [ 60 ] ;char name [ 60 ] ;
address add ;address add ;
double gpa ;double gpa ;
} s1 , s2 , s3 ;} s1 , s2 , s3 ;
![Page 9: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/9.jpg)
Example 1Example 1struct Cardstruct Card
{{
char *suit ;char *suit ;
char *value ;char *value ;
} ;} ;
![Page 10: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/10.jpg)
StructuresStructures
Variables of type structureVariables of type structure
Pointers to StructurePointers to Structure
Array of StructureArray of Structure
![Page 11: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/11.jpg)
Student s [ 100 ] ;Student s [ 100 ] ;Student *sPtr ;Student *sPtr ;
StructuresStructures
![Page 12: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/12.jpg)
Student stdnt1 , stdnt2 ;Student stdnt1 , stdnt2 ;
stdnt1 + stdnt2 ; stdnt1 + stdnt2 ; //Wrong//Wrong
StructuresStructures
![Page 13: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/13.jpg)
Student s1 , s2 ;Student s1 , s2 ;
s1 = s2 ;s1 = s2 ;
StructuresStructures
![Page 14: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/14.jpg)
Example 2Example 2struct Studentstruct Student{{
char name [ 64 ] ;char name [ 64 ] ;char course [ 128 ] ;char course [ 128 ] ;int age ;int age ;int year ;int year ;
} ;} ;
![Page 15: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/15.jpg)
Initializing Initializing StructuresStructures
Student s1 = { “Amara”,“cs201”,“19”, Student s1 = { “Amara”,“cs201”,“19”, “2002” } ;“2002” } ;
Name
Course name
ageyear
![Page 16: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/16.jpg)
Initializing Initializing StructuresStructures
s1.name ;s1.name ;
s1.course ;s1.course ;
s1.age ;s1.age ;
s1.year ;s1.year ;
![Page 17: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/17.jpg)
Initializing Initializing StructuresStructures
s1.age = 20 ;s1.age = 20 ;
s1.name = “Abbas Ali ” ; s1.name = “Abbas Ali ” ; //Wrong//Wrong
strcpy ( s1.name , “Abbas Ali ” ) ;strcpy ( s1.name , “Abbas Ali ” ) ;
![Page 18: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/18.jpg)
Accessing structure Accessing structure membersmembers
cout << s1.name ;cout << s1.name ;
cout << s1.course ;cout << s1.course ;
cout << s1.age ;cout << s1.age ;
cout << s1.year ;cout << s1.year ;
![Page 19: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/19.jpg)
Assignment of Assignment of structuresstructures
Student s1 , s2 ;Student s1 , s2 ;
s2 = s1 ;s2 = s1 ;
![Page 20: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/20.jpg)
Passing Structures Passing Structures to Functionsto Functions
![Page 21: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/21.jpg)
Call by valueCall by value
Call by Reference Call by Reference XX
Passing Structures to Functions
![Page 22: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/22.jpg)
int Square ( int int Square ( int x ) ;x ) ;
![Page 23: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/23.jpg)
x = Square ( 10 ) ;x = Square ( 10 ) ;
![Page 24: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/24.jpg)
StructuresStructures Simple Variable of type Simple Variable of type
StructureStructure Pointer to StructurePointer to Structure Arrays of StructuresArrays of Structures Function that returns a Function that returns a
StructureStructure Pass the Structure to functionsPass the Structure to functions
![Page 25: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/25.jpg)
Pointers to Pointers to StructureStructure
![Page 26: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/26.jpg)
Pointers to Pointers to StructureStructure
Student *sPtr , s1 ;Student *sPtr , s1 ;
sPtr = &s1 ;sPtr = &s1 ;
![Page 27: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/27.jpg)
Pointers to Pointers to StructureStructure
sPtrsPtr.name ;.name ;
WrongWrong
![Page 28: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/28.jpg)
**sPtrsPtr.name ;.name ;
Pointers to Pointers to StructureStructure
![Page 29: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/29.jpg)
*( sPtr ).name ;*( sPtr ).name ;
Pointers to StructurePointers to Structure
![Page 30: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/30.jpg)
*sPtr *sPtr ->name ;->name ;
Same asSame as
s1.names1.name
Pointers to StructurePointers to Structure
![Page 31: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/31.jpg)
Arrays of StructuresArrays of Structures
![Page 32: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/32.jpg)
Student s [ 100 ] ;Student s [ 100 ] ;s [ 0 ].name ;s [ 0 ].name ;s [ 1 ].name ; s [ 1 ].name ; s [ 2 ].name ;s [ 2 ].name ;
..
..
..s [ 99 ].name ;s [ 99 ].name ;
Arrays of Arrays of StructureStructure
![Page 33: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/33.jpg)
sizeof ( s1 ) ;sizeof ( s1 ) ;
![Page 34: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/34.jpg)
Example 3Example 3struct Studentstruct Student{{
char firstName [ 30 ] ;char firstName [ 30 ] ;char lastName [ 30 ] ;char lastName [ 30 ] ;char course [ 15 ] ;char course [ 15 ] ;char rollNo [ 10 ] ;char rollNo [ 10 ] ;int age ;int age ;float gpa ;float gpa ;
} ;} ;
![Page 35: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/35.jpg)
Student s [ 10 ] ; Student s [ 10 ] ; for ( int i = 0 ; i < 10 ; i ++ )for ( int i = 0 ; i < 10 ; i ++ ){{
cout << “Please enter the student's last name : " ;cout << “Please enter the student's last name : " ; cin >> s [ i ].lastName ;cin >> s [ i ].lastName ; cout << “Please enter the student's first name : cout << “Please enter the student's first name :
" ;" ; cin >> s [ i ].firstName ;cin >> s [ i ].firstName ; cout << " Please enter the student's course : " ;cout << " Please enter the student's course : " ; cin >> s [ i ].course ;cin >> s [ i ].course ; cout << " Please enter the student's Roll No. : " ;cout << " Please enter the student's Roll No. : " ; cin >> s [ i ].rollNo ;cin >> s [ i ].rollNo ; cout << " Please enter the student's grade : " ;cout << " Please enter the student's grade : " ; cin >> s [ i ].grade ;cin >> s [ i ].grade ; cout << " Please enter the student's age : " ;cout << " Please enter the student's age : " ; cin >> s [ i ].age ;cin >> s [ i ].age ; cout << " Please enter the student's GPA : " ;cout << " Please enter the student's GPA : " ; cin >> s [ i ].gpa ;cin >> s [ i ].gpa ;}}
Example 3
![Page 36: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/36.jpg)
Student getData ( ) Student getData ( ) ;;
Return Type
Function Name
Example 4
![Page 37: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/37.jpg)
Example 4Example 4ifstream inFile ifstream inFile
( “myText.txt” ) ;( “myText.txt” ) ;
void main ( )void main ( )
{{
Student s1 ;Student s1 ;
s1 = getData ( ) ;s1 = getData ( ) ;
inFile.close ( ) ;inFile.close ( ) ;
}}
![Page 38: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/38.jpg)
Example 4Example 4Student getData ( )Student getData ( )
{{
Student tempStudent ;Student tempStudent ;
inFile >> tempStudent.firstName ;inFile >> tempStudent.firstName ;
inFile >> tempStudent.lastName ;inFile >> tempStudent.lastName ;
inFile >> tempStudent.course ;inFile >> tempStudent.course ;
inFile >> tempStudent.rollNo ;inFile >> tempStudent.rollNo ;
inFile >> tempStudent.age ;inFile >> tempStudent.age ;
inFile >> tempStudent.gpa ;inFile >> tempStudent.gpa ;
return tempStudent ;return tempStudent ;
}}
![Page 39: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/39.jpg)
UnionUnion
![Page 40: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/40.jpg)
UnionUnionunion intOrCharunion intOrChar
{{
int i ;int i ;
char c ;char c ;
} ;} ;
![Page 41: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/41.jpg)
UnionUnion
C
iBoth Variable occupy the same space in memory
Memory
![Page 42: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/42.jpg)
UnionUnionunion intOrDoubleunion intOrDouble
{{
int ival ;int ival ;
double dval ;double dval ;
} ;} ;
![Page 43: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/43.jpg)
UnionUnion
iVal
dValBoth Variable occupy the same space in memory
Memory
![Page 44: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/44.jpg)
UnionUnion
intOrDoubleintOrDouble u1 ; u1 ;
u1.ival = 10 ;u1.ival = 10 ;
cout << u1.ival ;cout << u1.ival ;cout << u1.dval cout << u1.dval ; ; incorrect incorrect
valuevalue
![Page 45: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/45.jpg)
UnionUnion
u1.dval = 100.0 ;u1.dval = 100.0 ;cout << u1.ival ; cout << u1.ival ; incorrect incorrect
valuevalue
![Page 46: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/46.jpg)
Example 5Example 5char c ;char c ;int x ;int x ;x = ‘a’ ;x = ‘a’ ;cout << x ;cout << x ;x = x * 256x = x * 256cout << x cout << x ;;
::
![Page 47: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/47.jpg)
Example 5Example 5 x = x +’b’ ;x = x +’b’ ; cout << x ;cout << x ;
![Page 48: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/48.jpg)
Example 5Example 5
Union iandcUnion iandc
{{
char c [ 4 ] ;char c [ 4 ] ;
int x ;int x ;
} ;} ;
![Page 49: CS201- Introduction to Programming- Lecture 20](https://reader035.vdocuments.net/reader035/viewer/2022081414/5492e76eb47959ad258b47fc/html5/thumbnails/49.jpg)
Today we Today we studiedstudied
StructuresStructures UnionsUnions