init_struct

init_struct - Programming Enumeration, Initializing Data of...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Programming Enumeration, Initializing Data of Struct Type
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Prog. Fundamentals: initialize struct type/ Slide 2 Enumerated Type The enumerated type, enum , is derived from the integer type. In an enumerated type, each integer value is given an identifier called an enumeration constant . Syntax: enum {enumeration constants}; enum type_name {enumeration constants}; type_name variable_name;
Background image of page 2
2 Prog. Fundamentals: initialize struct type/ Slide 3 Ex. 6: Enumerated Type Examples: enum {one, two, three}; // const int one = 0; const int two = 1; enum {club, diamond, heart, spade}; enum {off, on}; enum months {jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec} ; months month_of_year; month_of_year = jan; // OK month_of_year = 0; // ERROR month_of_year = (months) 0; // OK
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Prog. Fundamentals: initialize struct type/ Slide 4 Example: Shuffling Cards Ex. 9: Shuffle the cards using enum and struct Use structure with members of suit and value Print out 4 hands 1 1 1 1 2 2 2 2
Background image of page 4
2 Prog. Fundamentals: initialize struct type/ Slide 5 Ex. 9: enum and Struct #include <iostream.h> #include <stdlib.h> #include <iomanip.h> #include <ctime> const char heart_shape = ( char ) 003; const char diamond_shape = ( char ) 004; const char club_shape = ( char ) 005; const char spade_shape = ( char ) 006; const int NUM_CARD = 52; enum suit {club, diamond, heart, spade}; struct CARD{ suit cardSuit; int value; };
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
int main(){ CARD deck[NUM_CARD], temp ; int countSuit, countCds, index, target; srand(time(NULL)); //Initialize the deck for (countSuit = 0; countSuit< 4; countSuit++) for (countCds = 0; countCds < 13; countCds++){ deck[countSuit*13+countCds].cardSuit=(suit)countSuit; deck[countSuit*13+countCds].value=countCds+1; } for (index=0; index<NUM_CARD; index++){ //Shuffle deck target = rand()%NUM_CARD; temp = deck[index]; deck[index] = deck[target]; deck[target]= temp; } //print out 4 hands print_cards(deck); return 0; }
Background image of page 6
void print_cards(CARD deck[]){ int hand, index; for (hand = 0; hand < 4; hand++){ cout << “\nhand "<< hand+1 << ": "; for (index = 0; index < NUM_CARD; index ++){ if (index %4 == hand){ switch (deck[index].cardSuit){ case club: cout << club_shape; break ; case diamond: cout << diamond_shape; break ; case heart: cout << heart_shape; break ; case spade: cout << spade_shape; break ; } cout <<setw(2)<< deck[index].value << ends; } } cout << endl; } }
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Prog. Fundamentals: initialize struct type/ Slide 8
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 27

init_struct - Programming Enumeration, Initializing Data of...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online