array, pointer and reference ( iv )

Click here to load reader

Post on 02-Jan-2016

21 views

Category:

Documents

1 download

Embed Size (px)

DESCRIPTION

ECE230 Lectures Series. Array, Pointer and Reference ( IV ). Ying Wu Electrical & Computer Engineering Northwestern University [email protected] We’ve got a lot of stuff!. Regular variables and references Pointers Array Function - PowerPoint PPT Presentation

TRANSCRIPT

  • Array, Pointer and Reference ( IV )Ying Wu Electrical & Computer Engineering Northwestern [email protected] Lectures Series

  • Weve got a lot of stuff!Regular variables and referencesPointers ArrayFunction

    Any combination of these leads to a new stuff!Pointer + function passing pointers to a functionRef. + function passing references to a functionArray + function passing an array to a functionPointer + array ??Array + array ??Pointer + pointer ??

  • What to learn today?2D array (or matrix)Array of pointers

  • Arrays of PointersArrays can contain pointersCommonly used to store an array of stringschar *suit[ 4 ] = {"Hearts", "Diamonds", "Clubs", "Spades" };Each element of suit is a pointer to a char * (a string)The strings are not in the array, only pointers to the strings are in the array

    suit array has a fixed size, but strings can be of any sizesuit[3]

    suit[2]

    suit[1]

    suit[0]

    H

    e

    a

    r

    t

    s

    \0

    D

    i

    a

    m

    o

    n

    d

    s

    \0

    C

    l

    u

    b

    s

    \0

    S

    p

    a

    d

    e

    s

    \0

  • Multiple-Subscripted ArraysMultiple subscripts - tables with rows, columnsLike matrices: specify row, then column.

    Initializeint b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };Initializers grouped by row in bracesint b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } };How to reference an element b[row][col], e.g., b[0][1]

  • Multiple-Subscripted ArraysHow is a 2D array stored in memory?int a[4][3];Row-first ordera is an array of pointers, *a[4], **a = ? a + 1 = ?a[0] + 1 = ? *(a[0] + 1) = ?Initializationint b[2][2] = {(1,2},{3,4}};int b[2][2] = {1,2,3,4};Int b[2][2] = {0};How to pass a 2-D array to a function?int myfun(int a[][3], int row, int col); Why?a[0][0]a[0][1]a[0][2]a[1][0]a[2][0]a[1][2]a[1][1]a[2][2]a[2][1]a[3][2]a[3][1]a[3][0]a[0]a[1]a[2]a[3]a

  • 1. Initialize variables

    1.1 Define functions to take double scripted arrays

    1.2 Initialize studentgrades[][]

    2. Call functions minimum, maximum, and average

  • 2. Call functions minimum, maximum, and average

    3. Define functions

  • 3. Define functions

  • Program OutputThe array is: [0] [1] [2] [3]studentGrades[0] 77 68 86 73studentGrades[1] 96 87 89 78studentGrades[2] 70 90 86 81Lowest grade: 68Highest grade: 96The average grade for student 0 is 76.00The average grade for student 1 is 87.50The average grade for student 2 is 81.75

  • Arrays of PointersArrays can contain pointersCommonly used to store an array of stringschar *suit[ 4 ] = {"Hearts", "Diamonds", "Clubs", "Spades" };Each element of suit is a pointer to a char * (a string)The strings are not in the array, only pointers to the strings are in the array

    suit array has a fixed size, but strings can be of any sizesuit[3]

    suit[2]

    suit[1]

    suit[0]

    H

    e

    a

    r

    t

    s

    \0

    D

    i

    a

    m

    o

    n

    d

    s

    \0

    C

    l

    u

    b

    s

    \0

    S

    p

    a

    d

    e

    s

    \0

  • Card Shuffling/Dealing SimulationCard shuffling programUse an array of pointers to strings, to store suit namesUse a double scripted array (suit by value)

    Place 1-52 into the array to specify the order in which the cards are dealtdeck[2][12] represents the King of Clubs

  • Card Shuffling/Dealing SimulationPseudocode for shuffling and dealing simulation

    For each of the 52 cards Place card number in randomly selected unoccupied slot of deck For each of the 52 cards Find card number in deck array and print face and suit of card Choose slot of deck randomly

    While chosen slot of deck has been previously chosen Choose slot of deck randomlyPlace card number in chosen slot of deck For each slot of the deck array If slot contains card number Print the face and suit of the card Second refinementThird refinementFirst refinementInitialize the suit arrayInitialize the face arrayInitialize the deck array

    Shuffle the deck

    Deal 52 cards Note: This design is bad. WHY? Can you do better?

  • 1. Initialize suit and face arrays

    1.1 Initialize deck array

    2. Call function shuffle

    2.1 Call function deal

  • 3. Define functions

    Note: This is not an efficient design! Why? Can you do better?

  • Program OutputSix of Clubs Seven of Diamonds Ace of Spades Ace of Diamonds Ace of Hearts Queen of DiamondsQueen of Clubs Seven of Hearts Ten of Hearts Deuce of Clubs Ten of Spades Three of Spades Ten of Diamonds Four of Spades Four of Diamonds Ten of Clubs Six of Diamonds Six of SpadesEight of Hearts Three of Diamonds Nine of Hearts Three of HeartsDeuce of Spades Six of Hearts Five of Clubs Eight of ClubsDeuce of Diamonds Eight of Spades Five of Spades King of Clubs King of Diamonds Jack of SpadesDeuce of Hearts Queen of Hearts Ace of Clubs King of SpadesThree of Clubs King of Hearts Nine of Clubs Nine of Spades Four of Hearts Queen of SpadesEight of Diamonds Nine of Diamonds Jack of Diamonds Seven of Clubs Five of Hearts Five of Diamonds Four of Clubs Jack of Hearts Jack of Clubs Seven of Spades

  • Name DatabaseTask: to create a name database, in which each entry is a name of a variable, e.g., var_1, a, How to search an entry given a name?

  • A Toy Examplebool Search(const char*, int&);char *g_nameDB[4];

    void main(){ g_nameDB[0] = a; g_nameDB[1] = var_1; g_nameDB[2] = xyz; g_nameDB[4] = temp; char query[100]; cin.getline(query,100); int ind; if(Search(query, ind)){ cout

  • Question to think about?To create a Variable DBTo hold the names/values of variablesGiven the name of a variable, search the DB to find the corresponding value?

  • Real stuff: VarDB#defineSIZE_DB 100double gVarDB_Value[SIZE_DB];char* gVarDB_Name[SIZE_DB];int gVarDB_Size;// the size of the DB

    // Initialize the system variable DBvoid VarDB_Init();

    // set name to one in the DBbool VarDB_SetVarName(const int& index, const char* name);

    // search variable according to its namebool VarDB_Search(const char* name, int& index);

    // create a new varible given a namebool VarDB_CreateANewVar(const char*name, int& index);

    // dump the DBvoid VarDB_Dump();

  • void VarDB_Init(){// the first one is reserved for "ans"gVarDB_Size = 1;for(int i=0; i
  • bool VarDB_Search(const char* name, int& index){boolcode = false;for(int i=0; i
  • void VarDB_Dump(){cout.setf(ios::left, ios::adjustfield);for(int i=0; i