CS201- Introduction to Programming- Lecture 16

Preview:

DESCRIPTION

Virtual University Course CS201- Introduction to Programming Lecture No 16 Instructor's Name: Dr. Naveed A. Malik Course Email: cs201@vu.edu.pk

Citation preview

Introduction to Introduction to ProgrammingProgramming

Lecture 16Lecture 16

In Today In Today LectureLecture

Conclude the last discussionConclude the last discussion Multi-dimensional ArraysMulti-dimensional Arrays Pointers to PointersPointers to Pointers

Example 1Example 1

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

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

Multi-dimensional Multi-dimensional ArraysArrays

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

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

Dereferencing array Dereferencing array elementelement

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

*multi *multi

??

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 ;

}}

multi + 3multi + 3

*( multi + 3 )*( multi + 3 )

*( multi + 3 ) + 3*( multi + 3 ) + 3

*(*( multi + 3 ) + 3 )*(*( multi + 3 ) + 3 )

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

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 ; }}}}

Pointer to aPointer to aPointerPointer

Array of PointersArray of Pointers

Array of Array of PointersPointers

char *myArray [ 10 ] ; char *myArray [ 10 ] ;

myArray is an array of 10 pointer to character

InitializationInitialization

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

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 ;

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

Example 4Example 4

main ( int argc , char **argv )main ( int argc , char **argv )

{{

cout << argc << "\n“ << cout << argc << "\n“ << *argv ;*argv ;

}}

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 ) ;

}}

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 ;

}}}}

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

} }

}}

}}

}}

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