cs201- introduction to programming- lecture 16
DESCRIPTION
Virtual University Course CS201- Introduction to Programming Lecture No 16 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]TRANSCRIPT
![Page 1: CS201- Introduction to Programming- Lecture 16](https://reader038.vdocuments.site/reader038/viewer/2022103014/5492e777b47959834a8b467e/html5/thumbnails/1.jpg)
Introduction to Introduction to ProgrammingProgramming
Lecture 16Lecture 16
![Page 2: CS201- Introduction to Programming- Lecture 16](https://reader038.vdocuments.site/reader038/viewer/2022103014/5492e777b47959834a8b467e/html5/thumbnails/2.jpg)
In Today In Today LectureLecture
Conclude the last discussionConclude the last discussion Multi-dimensional ArraysMulti-dimensional Arrays Pointers to PointersPointers to Pointers
![Page 3: CS201- Introduction to Programming- Lecture 16](https://reader038.vdocuments.site/reader038/viewer/2022103014/5492e777b47959834a8b467e/html5/thumbnails/3.jpg)
Example 1Example 1
char myName [ ] = “Amir” ;char myName [ ] = “Amir” ;
char *myNamePtr = char *myNamePtr = “Amir” ;“Amir” ;
![Page 4: CS201- Introduction to Programming- Lecture 16](https://reader038.vdocuments.site/reader038/viewer/2022103014/5492e777b47959834a8b467e/html5/thumbnails/4.jpg)
Multi-dimensional Multi-dimensional ArraysArrays
char multi [ 5 ] [ 10 ] ;char multi [ 5 ] [ 10 ] ;
![Page 5: CS201- Introduction to Programming- Lecture 16](https://reader038.vdocuments.site/reader038/viewer/2022103014/5492e777b47959834a8b467e/html5/thumbnails/5.jpg)
Multi-dimensional Array in Multi-dimensional Array in MemoryMemory
11 22 33 44 1100
77 99 1111
1144
1100
1177
2255
3399
4455
5588
Placed sequentially in the memory
1st row 1st col
2nd row1st col
[0] [1] [2] [3] [4] [0] [1] [2] [3] [4] [0] [1] [2] [3] [4]
3rd row1st col
![Page 6: CS201- Introduction to Programming- Lecture 16](https://reader038.vdocuments.site/reader038/viewer/2022103014/5492e777b47959834a8b467e/html5/thumbnails/6.jpg)
Dereferencing array Dereferencing array elementelement
multi [ 2 ] [ 3 ]multi [ 2 ] [ 3 ]
![Page 7: CS201- Introduction to Programming- Lecture 16](https://reader038.vdocuments.site/reader038/viewer/2022103014/5492e777b47959834a8b467e/html5/thumbnails/7.jpg)
*multi *multi
??
![Page 8: CS201- Introduction to Programming- Lecture 16](https://reader038.vdocuments.site/reader038/viewer/2022103014/5492e777b47959834a8b467e/html5/thumbnails/8.jpg)
Example 2Example 2#include<iostream.h>#include<iostream.h>
main ( ) main ( )
{{
char multi [ 5 ] [ 10 ] ;char multi [ 5 ] [ 10 ] ;
cout << multi << endl ;cout << multi << endl ;
cout << *multi ;cout << *multi ;
cout << **multi ;cout << **multi ;
}}
![Page 9: CS201- Introduction to Programming- Lecture 16](https://reader038.vdocuments.site/reader038/viewer/2022103014/5492e777b47959834a8b467e/html5/thumbnails/9.jpg)
multi + 3multi + 3
*( multi + 3 )*( multi + 3 )
*( multi + 3 ) + 3*( multi + 3 ) + 3
*(*( multi + 3 ) + 3 )*(*( multi + 3 ) + 3 )
![Page 10: CS201- Introduction to Programming- Lecture 16](https://reader038.vdocuments.site/reader038/viewer/2022103014/5492e777b47959834a8b467e/html5/thumbnails/10.jpg)
main ( )main ( ){{ int multi [ 5 ] [ 6 ] ; int multi [ 5 ] [ 6 ] ; int row , col ;int row , col ; int *ptr ;int *ptr ; ptr = *multi ;ptr = *multi ; for ( row = 0 ; row < 5 ; row ++ )for ( row = 0 ; row < 5 ; row ++ ) {{ for ( col = 0 ; col < 10 ; col ++ )for ( col = 0 ; col < 10 ; col ++ ) {{ multi [ row ] [ col ] = row * col ;multi [ row ] [ col ] = row * col ; }} }}
Example 3Example 3
![Page 11: CS201- Introduction to Programming- Lecture 16](https://reader038.vdocuments.site/reader038/viewer/2022103014/5492e777b47959834a8b467e/html5/thumbnails/11.jpg)
Example 3Example 3 for ( row = 0 ; row < 5 ; row ++ )for ( row = 0 ; row < 5 ; row ++ ) {{ for ( col = 0 ; col < 10 ; col ++)for ( col = 0 ; col < 10 ; col ++) {{
cout << *( ptr ++ ) << “ ”;cout << *( ptr ++ ) << “ ”; }} cout << endl ;cout << endl ; }}}}
![Page 12: CS201- Introduction to Programming- Lecture 16](https://reader038.vdocuments.site/reader038/viewer/2022103014/5492e777b47959834a8b467e/html5/thumbnails/12.jpg)
Pointer to aPointer to aPointerPointer
![Page 13: CS201- Introduction to Programming- Lecture 16](https://reader038.vdocuments.site/reader038/viewer/2022103014/5492e777b47959834a8b467e/html5/thumbnails/13.jpg)
Array of PointersArray of Pointers
![Page 14: CS201- Introduction to Programming- Lecture 16](https://reader038.vdocuments.site/reader038/viewer/2022103014/5492e777b47959834a8b467e/html5/thumbnails/14.jpg)
Array of Array of PointersPointers
char *myArray [ 10 ] ; char *myArray [ 10 ] ;
myArray is an array of 10 pointer to character
![Page 15: CS201- Introduction to Programming- Lecture 16](https://reader038.vdocuments.site/reader038/viewer/2022103014/5492e777b47959834a8b467e/html5/thumbnails/15.jpg)
InitializationInitialization
char *myArray [ ] = { “Amir ” , “ Jahangir ” } ;char *myArray [ ] = { “Amir ” , “ Jahangir ” } ;
![Page 16: CS201- Introduction to Programming- Lecture 16](https://reader038.vdocuments.site/reader038/viewer/2022103014/5492e777b47959834a8b467e/html5/thumbnails/16.jpg)
Storing Pointers in Array of Storing Pointers in Array of PointersPointers
int *p1 , *p2 , *p3 ;int *p1 , *p2 , *p3 ;
int *b [ 3 ] ;int *b [ 3 ] ;
b [ 0 ] = p1 ;b [ 0 ] = p1 ;
b [ 1 ] = p2 ;b [ 1 ] = p2 ;
b [ 2 ] = p3 ;b [ 2 ] = p3 ;
![Page 17: CS201- Introduction to Programming- Lecture 16](https://reader038.vdocuments.site/reader038/viewer/2022103014/5492e777b47959834a8b467e/html5/thumbnails/17.jpg)
Command Line Command Line ArgumentsArguments
argc argc
argvargv
‘‘argc’ stands for a count of the argc’ stands for a count of the number of argumentsnumber of arguments
‘‘argv’ stands for a vector of argv’ stands for a vector of argumentsarguments
![Page 18: CS201- Introduction to Programming- Lecture 16](https://reader038.vdocuments.site/reader038/viewer/2022103014/5492e777b47959834a8b467e/html5/thumbnails/18.jpg)
Example 4Example 4
main ( int argc , char **argv )main ( int argc , char **argv )
{{
cout << argc << "\n“ << cout << argc << "\n“ << *argv ;*argv ;
}}
![Page 19: CS201- Introduction to Programming- Lecture 16](https://reader038.vdocuments.site/reader038/viewer/2022103014/5492e777b47959834a8b467e/html5/thumbnails/19.jpg)
Example 5Example 5main ( )main ( )
{{const char *suit [ 4 ]= const char *suit [ 4 ]= { "Spades“ , "Hearts“ , { "Spades“ , "Hearts“ ,
"Diamonds“ , "Clubs“ } ;"Diamonds“ , "Clubs“ } ;
const char *face [ 13 ] = const char *face [ 13 ] = { "Ace“ , "Deuce“ , "Three“ , "Four", { "Ace“ , "Deuce“ , "Three“ , "Four",
"Five“ , "Six“ , "Seven“ , "Five“ , "Six“ , "Seven“ , "Eight“ ,"Eight“ ,
"Nine“ , "Ten“ , "Jack“ , "Queen“ , "King" } ;"Nine“ , "Ten“ , "Jack“ , "Queen“ , "King" } ;
int deck [ 4 ] [ 13 ] = { 0 } ;int deck [ 4 ] [ 13 ] = { 0 } ;
srand ( time ( 0 ) ) ;srand ( time ( 0 ) ) ;
shuffle ( deck ) ;shuffle ( deck ) ;deal ( deck , face , suit ) ;deal ( deck , face , suit ) ;
}}
![Page 20: CS201- Introduction to Programming- Lecture 16](https://reader038.vdocuments.site/reader038/viewer/2022103014/5492e777b47959834a8b467e/html5/thumbnails/20.jpg)
Shuffle FunctionsShuffle Functionsvoid shuffle ( int wDeck [ ] [ 13 ] )void shuffle ( int wDeck [ ] [ 13 ] ){{
int row , column , card ;int row , column , card ;
for ( card = 1 ; card <= 52 ; card ++ ) for ( card = 1 ; card <= 52 ; card ++ ) {{ do do
{{row = rand ( ) % 4 ;row = rand ( ) % 4 ;
column = rand ( ) % 13 ;column = rand ( ) % 13 ; } }
while( wDeck[ row ]| column ] != 0 ) ;while( wDeck[ row ]| column ] != 0 ) ;wDeck [ row ] [ column ] = card ;wDeck [ row ] [ column ] = card ;
}}}}
![Page 21: CS201- Introduction to Programming- Lecture 16](https://reader038.vdocuments.site/reader038/viewer/2022103014/5492e777b47959834a8b467e/html5/thumbnails/21.jpg)
Deal FunctionDeal Functionvoid deal ( const int wDeck [ ] [ 13 ] , const char *wFace [ ] , const char *wSuit [ ] )void deal ( const int wDeck [ ] [ 13 ] , const char *wFace [ ] , const char *wSuit [ ] )
{{
int card , row , column ;int card , row , column ;
for ( card = 1 ; card <= 52 ; card ++ )for ( card = 1 ; card <= 52 ; card ++ )
{{
for ( row = 0 ; row <= 3 ; row++ )for ( row = 0 ; row <= 3 ; row++ )
{{
for ( column = 0 ; column <= 12 ; column ++ )for ( column = 0 ; column <= 12 ; column ++ )
{{
if ( wDeck [ row ] [ column ] == card )if ( wDeck [ row ] [ column ] == card )
// Print the face and suit of the card // Print the face and suit of the card
// break out of loops// break out of loops
} }
}}
}}
}}
![Page 22: CS201- Introduction to Programming- Lecture 16](https://reader038.vdocuments.site/reader038/viewer/2022103014/5492e777b47959834a8b467e/html5/thumbnails/22.jpg)
What we have done What we have done todaytoday
Multi-dimensional ArraysMulti-dimensional Arrays Pointers to PointersPointers to Pointers Arrays of PointersArrays of Pointers Command Line Command Line
ArgumentsArguments Comprehensive ExampleComprehensive Example