init_struct - Programming Enumeration Initializing Data of...

Info icon This 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
Image of page 1

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

View Full Document Right 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;
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
Image of page 3

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

View Full Document Right 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
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; };
Image of page 5

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

View Full Document Right 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; }
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; } }
Image of page 7

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern