Introduction to Programming - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Introduction to Programming

Description:

Introduction to Programming Lecture 16 In Today Lecture Conclude the last discussion Multi-dimensional Arrays Pointers to Pointers Example 1 char myName [ ] = Amir ... – PowerPoint PPT presentation

Number of Views:106
Avg rating:3.0/5.0
Slides: 23
Provided by: Umer4
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Programming


1
Introduction to Programming
  • Lecture 16

2
In Today Lecture
  • Conclude the last discussion
  • Multi-dimensional Arrays
  • Pointers to Pointers

3
Example 1
  • char myName Amir
  • char myNamePtr Amir

4
Multi-dimensional Arrays
  • char multi 5 10

5
Multi-dimensional Array in Memory
Placed sequentially in the memory
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
1 2 3 4 10
7 9 11 14 10
17 25 39 45 58
1st row 1st col
2nd row 1st col
3rd row 1st col
6
Dereferencing array element
  • multi 2 3

7
  • multi
  • ?

8
Example 2
  • includeltiostream.hgt
  • main ( )
  • char multi 5 10
  • cout ltlt multi ltlt endl
  • cout ltlt multi
  • cout ltlt multi

9
  • multi 3
  • ( multi 3 )
  • ( multi 3 ) 3
  • (( multi 3 ) 3 )

10
Example 3
  • main ( )
  • int multi 5 6
  • int row , col
  • int ptr
  • ptr multi
  • for ( row 0 row lt 5 row )
  • for ( col 0 col lt 10 col )
  • multi row col row col

11
Example 3
  • for ( row 0 row lt 5 row )
  • for ( col 0 col lt 10 col )
  • cout ltlt ( ptr ) ltlt
  • cout ltlt endl

12
  • Pointer to a
  • Pointer

13
  • Array of Pointers

14
Array of Pointers
  • char myArray 10

myArray is an array of 10 pointer to character
15
Initialization
  • char myArray Amir , Jahangir

16
Storing Pointers in Array of Pointers
  • int p1 , p2 , p3
  • int b 3
  • b 0 p1
  • b 1 p2
  • b 2 p3

17
Command Line Arguments
  • argc
  • argv
  • argc stands for a count of the number of
    arguments
  • argv stands for a vector of arguments

18
Example 4
  • main ( int argc , char argv )
  • cout ltlt argc ltlt "\n ltlt argv

19
Example 5
  • main ( )
  • const char suit 4 "Spades , "Hearts ,
    "Diamonds , "Clubs
  • const char face 13 "Ace , "Deuce ,
    "Three , "Four",
  • "Five , "Six ,
    "Seven , "Eight ,
  • "Nine , "Ten , "Jack ,
    "Queen , "King"
  • int deck 4 13 0
  • srand ( time ( 0 ) )
  • shuffle ( deck )
  • deal ( deck , face , suit )

20
Shuffle Functions
  • void shuffle ( int wDeck 13 )
  • int row , column , card
  • for ( card 1 card lt 52 card )
  • do
  • row rand ( ) 4
  • column rand ( ) 13
  • while( wDeck row column ! 0 )
  • wDeck row column card

21
Deal Function
  • void deal ( const int wDeck 13 , const
    char wFace , const char wSuit )
  • int card , row , column
  • for ( card 1 card lt 52 card )
  • for ( row 0 row lt 3 row )
  • for ( column 0 column lt 12 column
    )
  • if ( wDeck row column card )
  • // Print the face and suit of the card
  • // break out of loops

22
What we have done today
  • Multi-dimensional Arrays
  • Pointers to Pointers
  • Arrays of Pointers
  • Command Line Arguments
  • Comprehensive Example
Write a Comment
User Comments (0)
About PowerShow.com