In: Computer Science
Learning Objectives
After the successful completion of this learning unit, you will be able to:
Array Practice
I recommend that before you begin the assignment you write as many of these small ungraded programming challenges as you can. You should at least write 2 or 3 of them. They are a good way to gradually build up your confidence and skills. Of course, you'll have to write a program to test each function as well. Note that none of these functions should include any input or output!
Write a function named noNegatives(). It should accept an array of integers and a size argument. It should return true if none of the values are negative. If any of the values are negative it should return false
bool noNegatives(const int array[], int size);
Write a function named absoluteValues(). It should accept an array of integers and a size argument. It should replace any negative values with the corresponding positive value.
void absoluteValues(int array[], int size);
Write a function named eCount. It should accept an array of chars and a size argument. It should return the number of times that the character 'e' shows up in the array.
int eCount(const char array[], int size);
Write a function named charCount. It should be similar to eCount, but instead of counting 'e's it should accept a third argument, a target char. The function should return the number of times the target char shows up in the array.
int charCount(const char array[], int size, char targetChar);
Write a method named isSorted. It should accept an array of integers and a size argument. It should return true if the values are sorted in ascending order. False if they are not.
bool isSorted(const int array[], int size);
Write a method named equalNeighbors. It should accept an array of chars and a size argument. It should return true if there are two adjacent elements in the array with equal values. If there are not, it should return false.
bool equalNeighbors(const char array[], int size);
This is not a homework assignment, so feel free to post your code to one of these (not more than one) to the forum at any time.
For Credit
Assignment 4.1 [45 points]
Write a program that reads five (or more) cards from the user, then analyzes the cards and prints out the category of hand that they represent.
Poker hands are categorized according to the following labels: Straight flush, four of a kind, full house, straight, flush, three of a kind, two pairs, pair, high card.
To simplify the program we will ignore card suits, and face cards. The values that the user inputs will be integer values from 2 to 9. When your program runs it should start by collecting five integer values from the user and placing the integers into an array that has 5 elements. It might look like this:
Enter five numeric cards, no face cards. Use 2 - 9. Card 1: 8 Card 2: 7 Card 3: 8 Card 4: 2 Card 5: 3
(This is a pair, since there are two eights)
No input validation is required for this assignment. You can assume that the user will always enter valid data (numbers between 2 and 9).
Since we are ignoring card suits there won't be any flushes. Your program should be able to recognize the following hand categories, listed from least valuable to most valuable:
Hand Type | Description | Example |
High Card | There are no matching cards, and the hand is not a straight | 2, 5, 3, 8, 7 |
Pair | Two of the cards are identical | 2, 5, 3, 5, 7 |
Two Pair | Two different pairs | 2, 5, 3, 5, 3 |
Three of a kind | Three matching cards | 5, 5, 3, 5, 7 |
Straight | 5 consecutive cards | 3, 5, 6, 4, 7 |
Full House | A pair and three of a kind | 5, 7, 5, 7, 7 |
Four of a kind | Four matching cards | 2, 5, 5, 5, 5 |
(A note on straights: a hand is a straight regardless of the order. So the values 3, 4, 5, 6, 7 represent a straight, but so do the values 7, 4, 5, 6, 3).
Your program should read in five values and then print out the appropriate hand type. If a hand matches more than one description, the program should print out the most valuable hand type.
Here are three sample runs of the program:
Enter five numeric cards, no face cards. Use 2 - 9. Card 1: 8 Card 2: 7 Card 3: 8 Card 4: 2 Card 5: 7 Two Pair!
Enter five numeric cards, no face cards. Use 2 - 9. Card 1: 8 Card 2: 7 Card 3: 4 Card 4: 6 Card 5: 5 Straight!
Enter five numeric cards, no face cards. Use 2 - 9. Card 1: 9 Card 2: 2 Card 3: 3 Card 4: 4 Card 5: 5 High Card!
Additional Requirements
1) You must write a function for each hand type. Each function must accept a const int array that contains five integers, each representing one of the 5 cards in the hand, and must return "true" if the hand contains the cards indicated by the name of the function, "false" if it does not. The functions should have the following signatures.
bool containsPair(const int hand[]) bool containsTwoPair(const int hand[]) bool containsThreeOfaKind(const int hand[]) bool containsStraight(const int hand[]) bool containsFullHouse(const int hand[]) bool containsFourOfaKind(const int hand[])
Note that there are some interesting questions regarding what some of these should return if the hand contains the target hand-type and also contains a higher hand-type. For example, should containsPair() return true for the hand [2, 2, 2, 3, 4]? Should it return true for [2, 2, 3, 3, 4]? [2, 2, 3, 3, 3]? I will leave these decisions up to you.
2) Of course, as a matter of good practice, you should use a constant to represent the number of cards in the hand, and everything in your code should still work if the number of cards in the hand is changed to 4 or 6 or 11 (for example). Writing your code so that it does not easily generalize to more than 5 cards allows you to avoid the objectives of this assignment (such as traversing an array using a loop). If you do this, you will receive a 0 on the assignment.
3) You do not need to write a containsHighCard function. All hands contain a highest card. If you determine that a particular hand is not one of the better hand types, then you know that it is a High Card hand.
4) Do not sort the cards in the hand. Also, do not make a copy of the hand and then sort that.
5) An important objective of this assignment is to have you practice creating excellent decomposition. Don't worry about efficiency on this assignment. Focus on excellent decomposition, which results in readable code. This is one of those programs where you can rush and get it done but end up with code that is really difficult to read, debug, modify, and re-use. If you think about it hard, you can think of really helpful ways in which to combine the tasks that the various functions are performing. 5 extra credit points on this assignment will be awarded based on the following criteria: no function may have nested loops in it. If you need nested loops, the inner loop must be turned into a separate function, hopefully in a way that makes sense and so that the separate function is general enough to be re-used by the other functions. Also, no function other than main() may have more than 5 lines of code. (This is counting declarations, but not counting the function header, blank lines, or lines that have only a curly brace on them.) In my solution I was able to create just 3 helper functions, 2 of which are used repeatedly by the various functions.
These additional criteria are intended as an extra challenge and may be difficult for many of you. If you can't figure it out, give it your best shot, but don't be too discouraged. It's just 5 points. And be sure to study the posted solution carefully.
Suggestions
Test these functions independently. Once you are sure that they all work, the program logic for the complete program will be fairly straightforward.
Here is code that tests a containsPair function:
int main() { int hand[] = {2, 5, 3, 2, 9}; if (containsPair(hand)) { cout << "contains a pair" << endl; } }
Submit Your Work
Name your source code file according to the assignment number (a1_1.cpp, a4_2.cpp, etc.). Execute the program and copy/paste the output that is produced by your program into the bottom of the source code file, making it into a comment. Use the Assignment Submission link to submit the source file. When you submit your assignment there will be a text field in which you can add a note to me (called a "comment", but don't confuse it with a C++ comment). In this "comments" section of the submission page let me know whether the program works as required.
Keep in mind that if your code does not compile you will receive a 0.
Question 1
ScreenshotProgram
//Header file for I/O
#include <iostream>
using namespace std;
//Function prototypes
bool noNegatives(const int array[], int size);
void absoluteValues(int array[], int size);
bool isSorted(const int array[], int size);
int eCount(const char array[], int size);
int charCount(const char array[], int size, char targetChar);
bool equalNeighbors(const char array[], int size);
int main()
{
//Create an Integer array for test
int testArray[] = { -1, 2, 3, 4, 5 };
cout << "Test Array:" << endl;
for (int i = 0; i < 5; i++) {
cout << testArray[i] <<
" ";
}
cout << endl;
//Non negative check
if (noNegatives(testArray, 5)) {
cout << "\nCheck for non
negative = True" << endl;
}
else {
cout << "\nCheck for non
negative = False" << endl;
}
//Absolute array
absoluteValues(testArray, 5);
cout << "\nAbsolute Array:" << endl;
for (int i = 0; i < 5; i++) {
cout << testArray[i] <<
" ";
}
cout << endl;
//Check sorted or not
if (isSorted(testArray, 5)) {
cout << "\nGiven array is
sorted" << endl;
}
else {
cout << "\nGiven array is not
sorted" << endl;
}
//Create an character array for test
char testCArray[] = { 'a', 'e', 'b', 'e', 'e' };
cout << "\nTest charcter Array:" <<
endl;
for (int i = 0; i < 5; i++) {
cout << testCArray[i]
<< " ";
}
cout << endl;
//Call ecount method
cout << "\nCount of 'e' in array is " <<
eCount(testCArray, 5) << endl;
//Call charcount method
cout << "\nCount of 'a' in array is " <<
charCount(testCArray, 5,'a') << endl;
//Check equal neighbors
if (equalNeighbors(testCArray, 5)) {
cout << "\nThe given array
has equal neighbors" << endl;
}
else {
cout << "\nThe given array
has no equal neighbors" << endl;
}
}
/*
Function to check array is non negative
Parameter In:array[],size
Parameter out: true/false
Description: Check the elements of array and if found negative
return false
Otherwise return true;
*/
bool noNegatives(const int array[], int size) {
for (int i = 0; i < size; i++) {
if (array[i] < 0) {
return
false;
}
}
return true;
}
/*
Function to absolute values of elements in the array
Parameter In:array[],size
Parameter out: None
Description: Check the elements of array and if found negative
convert into positive
*/
void absoluteValues(int array[], int size) {
for (int i = 0; i < size; i++) {
if (array[i] < 0) {
array[i] = -1 *
array[i];
}
}
}
/*
Function to check e count in a character
array
Parameter In:array[],size
Parameter out: count of e's
Description: Loop through array elements
Check whether it is 'e'
then increment count
*/
int eCount(const char array[], int size) {
int cnt = 0;
for (int i = 0; i < size; i++) {
if (array[i] == 'e') {
cnt++;
}
}
return cnt;
}
/*
Function to check given character count in a character
array
Parameter In:array[],size,target
Parameter out: count of target
Description: Loop through array elements
Check whether it is target
then increment count
*/
int charCount(const char array[], int size,char targetChar) {
int cnt = 0;
for (int i = 0; i < size; i++) {
if (array[i] == targetChar) {
cnt++;
}
}
return cnt;
}
/*
Function to check the given array sorted or not
Parmeter In: array[],size
Parameter Out: True/False
*/
bool isSorted(const int array[], int size) {
for (int i = 0; i < size - 1; i++) {
if (array[i] > array[i + 1])
{
return
false;
}
}
return true;
}
/*
Function to check a char array contains equal neighbors
Parameter In: array[],size
parameter Out:True/False
*/
bool equalNeighbors(const char array[], int size) {
for (int i = 0; i < size - 1; i++) {
if (array[i] == array[i + 1])
{
return
true;
}
}
return false;
}
------------------------------------------------------------------------------------
output
Test Array:
-1 2 3 4 5
Check for non negative = False
Absolute Array:
1 2 3 4 5
Given array is sorted
Test charcter Array:
a e b e e
Count of 'e' in array is 3
Count of 'a' in array is 1
The given array has equal neighbors
----------------------------------------------------------------------------------------------
Question 2
Screenshot
Program
#include <iostream>
using namespace std;
//Constants
const int SIZE = 5;
//Function prototypes
bool containsPair(const int hand[]);
bool containsTwoPair(const int hand[]);
bool containsThreeOfaKind(const int hand[]);
bool containsStraight(const int hand[]);
bool containsFullHouse(const int hand[]);
bool containsFourOfaKind(const int hand[]);
bool checkElement(const int hand[], int element,int index);
int countElement(const int hand[], int element, int index);
int findSmallest(const int hand[]);
int main()
{
//Array for hands
int hand[5];
//Prompt for input
cout << "Enter five numeric cards, no face
cards.Use 2 - 9." << endl;
for (int i = 0; i < 5; i++) {
cout << "Card " << (i +
1) << " : ";
cin >> hand[i];
}
//Display result
if (containsFullHouse(hand)) {
cout << "Contains Full house" <<
endl;
}
else if (containsTwoPair(hand)) {
cout << "Contains Two pair"
<< endl;
}
else if (containsFourOfaKind(hand)) {
cout << "Contains Four of a
kind" << endl;
}
else if (containsStraight(hand)) {
cout << "Contains Straight"
<< endl;
}
else if (containsThreeOfaKind(hand)) {
cout << "Contains Three of a
kind" << endl;
}
else if (containsPair(hand)) {
cout << "Contains Pair"
<< endl;
}
}
//Function return true if element present otherwise false
bool checkElement(const int hand[], int element,int index) {
for (int i = index; i < SIZE; i++) {
if (hand[i] == element) {
return
true;
}
}
return false;
}
//Function to check an elements count in array
int countElement(const int hand[], int element, int index) {
int cnt = 0;
for (int i = index; i < SIZE; i++) {
if (hand[i] == element) {
cnt++;
}
}
return cnt;
}
//Function to check the given hand contains a pair
bool containsPair(const int hand[]) {
for (int i = 0; i < SIZE - 1;i++) {
if (checkElement(hand,hand[i],i+1))
{
return
true;
}
}
return false;
}
//function to check array contains 2 pairs
bool containsTwoPair(const int hand[]) {
int cnt = 0, checker=-1;
for (int i = 0; i < SIZE - 1; i++) {
if (checkElement(hand, hand[i], i +
1) && checker!= hand[i]) {
checker =
hand[i];
cnt++;
}
}
if (cnt >= 2) {
return true;
}
return false;
}
//Function to check a hand contains 3 of same cards
bool containsThreeOfaKind(const int hand[]) {
for (int i = 0; i < SIZE - 1; i++) {
if (countElement(hand, hand[i], i +
1)==2) {
return
true;
}
}
return false;
}
//Get smallest element from an array
int findSmallest(const int hand[]) {
int smallest = hand[0];
for (int i = 1; i < SIZE; i++) {
if (smallest > hand[i]) {
smallest =
hand[i];
}
}
return smallest;
}
//Function to find the passes value present in the array
bool checkLarget(const int hand[], int val) {
for (int i = 0; i < SIZE; i++) {
if (hand[i] == val) {
return
true;
}
}
return false;
}
//Function to check a straight hand
//Means consecutive cards
bool containsStraight(const int hand[]) {
int cnt = 0,smallest=findSmallest(hand);
for (int i = 0; i < SIZE; i++) {
if (checkLarget(hand,smallest+1))
{
cnt++;
smallest =
smallest + 1;
}
}
if (cnt == SIZE-1) {
return true;
}
return false;
}
//pair and three of kind
bool containsFullHouse(const int hand[]) {
if (containsTwoPair(hand) &&
containsThreeOfaKind(hand)) {
return true;
}
return false;
}
//Four matching cards
bool containsFourOfaKind(const int hand[]) {
for (int i = 0; i < SIZE - 1; i++) {
if (countElement(hand, hand[i], i +
1) == SIZE-1) {
return
true;
}
}
return false;
}
------------------------------------------------------------
Output
Enter five numeric cards, no face cards.Use 2 - 9.
Card 1 : 8
Card 2 : 7
Card 3 : 8
Card 4 : 2
Card 5 : 7
Contains Two pair