22
Introduction to Introduction to Programming Programming Lecture 16 Lecture 16

CS201- Introduction to Programming- Lecture 16

Embed Size (px)

DESCRIPTION

Virtual University Course CS201- Introduction to Programming Lecture No 16 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]

Citation preview

Page 1: CS201- Introduction to Programming- Lecture 16

Introduction to Introduction to ProgrammingProgramming

Lecture 16Lecture 16

Page 2: CS201- Introduction to Programming- Lecture 16

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

Example 1Example 1

char myName [ ] = “Amir” ;char myName [ ] = “Amir” ;

char *myNamePtr = char *myNamePtr = “Amir” ;“Amir” ;

Page 4: CS201- Introduction to Programming- Lecture 16

Multi-dimensional Multi-dimensional ArraysArrays

char multi [ 5 ] [ 10 ] ;char multi [ 5 ] [ 10 ] ;

Page 5: CS201- Introduction to Programming- Lecture 16

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

Dereferencing array Dereferencing array elementelement

multi [ 2 ] [ 3 ]multi [ 2 ] [ 3 ]

Page 7: CS201- Introduction to Programming- Lecture 16

*multi *multi

??

Page 8: CS201- Introduction to Programming- Lecture 16

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

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

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

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

Pointer to aPointer to aPointerPointer

Page 13: CS201- Introduction to Programming- Lecture 16

Array of PointersArray of Pointers

Page 14: CS201- Introduction to Programming- Lecture 16

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

InitializationInitialization

char *myArray [ ] = { “Amir ” , “ Jahangir ” } ;char *myArray [ ] = { “Amir ” , “ Jahangir ” } ;

Page 16: CS201- Introduction to Programming- Lecture 16

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

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

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

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

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

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

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