How to Represent the Deck of Cards Using Array in C++? Last Updated : 06 Jun, 2024 Comments Improve Suggest changes Like Article Like Report A deck of cards is a collection of 52 playing cards in which each card is uniquely identified by its suit and rank, there are four suits: heart, diamond, club and spade each having 13 ranks: Ace, 2 to 10, jack, queen, and king. In this article, we will learn how to represent a deck of cards using a C++ array. Representing Deck of Cards in C++ ArraysIn C++, we can make the use of classes to represent a deck of cards as it provides a structured and efficient way to represent a deck of cards. This arrangement allows for easy access, manipulation, shuffling, and allotment of cards to players, as the deck internally uses array the user defined data type card. ApproachDefine a Card class with suit and rank as members.Create a Deck class to manage the deck of cards. Inside it, we create an array of cards to store the deck of card.Create and Initialize the suits and ranks arrays with the names of the suits and ranks.Initialize the deck of cards in a nested loop.For each combination of suit and rank, create a card and add it to the deck.Finally, using a loop, print each card in the deck.C++ Program that Represents a Deck of CardsBelow is the C++ program that represents a deck of cards using an array C++ // C++ program for representing the deck of cards in arrays #include <iostream> #include <string> using namespace std; // class representing a card class Card { private: string suit; string rank; public: // Default constructor Card() : suit("") , rank("") { } // Parameterized constructor Card(string s, string r) : suit(s) , rank(r) { } // Setter for suit void setSuit(string s) { suit = s; } // Setter for rank void setRank(string r) { rank = r; } // Getter for suit string getSuit() const { return suit; } // Getter for rank string getRank() const { return rank; } // Method to print card details void printCard() const { cout << suit << " " << rank; } }; // class representing a deck of cards class Deck { private: Card deck[52]; // data for initialization string suits[4] = { "hearts", "diamonds", "clubs", "spades" }; string ranks[13] = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" }; public: // Constructor to initialize and populate the deck Deck() { for (int i = 0; i < 4; i++) { for (int j = 0; j < 13; j++) { deck[i * 13 + j].setSuit(suits[i]); deck[i * 13 + j].setRank(ranks[j]); } } } // Method to print the entire deck void printDeck() const { int suitChange = 0; for (int i = 0; i < 52; i++) { deck[i].printCard(); suitChange++; if (suitChange < 13) cout << " | "; else { suitChange = 0; cout << endl; } } } }; // Driver code int main() { Deck deck; // Print the deck of cards deck.printDeck(); return 0; } Output hearts Ace | hearts 2 | hearts 3 | hearts 4 | hearts 5 | hearts 6 | hearts 7 | hearts 8 | hearts 9 | hearts 10 | hearts Jack | hearts Queen | hearts King diamonds Ace | diamonds 2 | diamonds 3 | diamonds 4 | diamonds 5 | diamonds 6 | diamonds 7 | diamonds 8 | diamonds 9 | diamonds 10 | diamonds Jack | diamonds Queen | diamonds King clubs Ace | clubs 2 | clubs 3 | clubs 4 | clubs 5 | clubs 6 | clubs 7 | clubs 8 | clubs 9 | clubs 10 | clubs Jack | clubs Queen | clubs King spades Ace | spades 2 | spades 3 | spades 4 | spades 5 | spades 6 | spades 7 | spades 8 | spades 9 | spades 10 | spades Jack | spades Queen | spades KingTime Complexity: O(1), It is constant because the deck size is fixed (52).Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Represent the Deck of Cards Using Array in C++? R rohitpant4532 Follow Improve Article Tags : C++ Programs C++ cpp-array C++ Array Programs CPP Examples +1 More Practice Tags : CPP Similar Reads How to Create a Set of Arrays in C++? In C++, the set container represents a collection of unique, sorted elements, and an array is a collection of items stored at contiguous memory locations. In this article, we will learn about how to create a set of arrays in C++. Set of Arrays in C++A set of arrays refers to a collection of arrays w 2 min read How to Calculate the Size of a Static Array in C++? In C++, static arrays are the type of arrays whose size is fixed, and memory for them is allocated during the compile time. In this article, we will learn how to calculate the size of a static array in C++. Example: Input:myArray = {1, 2, 3, 4, 5}Output:The size of the array is 5.Size of a Static Ar 2 min read How to Create an Array of Structs in C++? In C++, a struct is a user-defined data type that allows us to combine data of different types and an array of structs is an array in which each element is of the struct type. In this article, we will learn how to create an array of structs in C++. Creating an Array of Structs in C++To create an arr 2 min read How to Create a Set of Vectors in C++? In C++, a set is a type of associative container in which duplicate elements are not allowed and a vector is a dynamic array in which duplicate elements are allowed. In this article, we will learn how to create a set of vectors in C++. For Example, Input:vector<int> vec1={1, 2, 3};vector<in 2 min read How to Create a Stack of Arrays in C++? In C++, the std::stack is a container that follows the LIFO (Last In, First Out) rule, whereas std::array is a sequence container that stores elements in contiguous memory. In this article, we will learn how to create a stack of an array in C++. Example: Input: arr1 = {1, 2, 3}; arr2 = {4, 5, 6}; ar 2 min read Represent Tree using graphics in C/C++ Prerequisite: graphics.h, How to include graphics.h? In C/C++ there is graphics.h header file which is used to create the object like line, circle, etc. Given an array arr[] of N integers, the task is to write C++ program to create the Tree using graphics.h. Approach: To run the program we have the 3 min read How to Create a Vector of Arrays in C++? In C++, an array is a collection of elements of a single type while vectors are dynamic arrays as they can change their size during the insertion and deletion of elements. In this article, we will learn how to create a vector of arrays in C++. Example: Input: arr1 = {1, 2, 3}; arr2 = {4, 5, 6}; arr3 2 min read How to Find the Unique Elements in an Array in C++? In C++, an array is a data structure that is used to store multiple values of similar data types in a contiguous memory location. In this article, we will learn how to find the unique elements in an array in C++. Example: Input: array = {1, 2, 1, 2, 2, 3, 4} Output: Unique elements in the array: 1 2 2 min read How to Resize an Array of Strings in C++? In C++, the array of strings is useful for storing many strings in the same container. Sometimes, we need to change the size of this array. In this article, we will look at how to resize the array of strings in C++. Resize String Array in C++There is no way to directly resize the previously allocate 2 min read How to Create a Vector of Pairs in C++? In C++, std::pair is the data type that stores the data as keys and values. On the other hand, std::vector is an STL container that stores the collection of data of similar type in the contiguous memory location. In this article, we will learn how to combine these two to create a vector of pairs in 5 min read Like