C Program Assignment Assignment : Write algorithms and programs to implement a s
ID: 3603857 • Letter: C
Question
C Program Assignment
Assignment: Write algorithms and programs to implement a simulation of a deck of cards for a card game (following assignment 3 specs, except as noted below) - stud poker. Your simulation will use structures to represent each card, not a simple primitive type — your choice on how to represent the deck and hands; but hands should be separate from the deck. This implementation will only allow from 1-7 players (instead of 1-13) and ONLY 5 cards per hand (instead of 1-13) — but use the same command-line interface from assignment 3. During your validation, any integer value entered for the cards per hand may be changed to 5.
Your simulation will perform all the tasks of assignment 3. Additionally, the hands will be sorted (by face value) and displayed after sorting. The hands will then be assigned their poker rank (one pair, two pair, flush, etc.). Finally, one (or more) of the hands will be designated as “the winner”, based upon hand ranking.
Output: Display the original deck, display the shuffled deck, and display each of the hands of cards. Then, display the sorted hands, labeling each hand with its poker rank. Decks, hands and cards should be clean, clear and appropriately labeled - do not simply list as a simple column or row of values. Next, display a list of winner(s), or clearly designate the winner(s) when you display the labeled hands.
After the game is concluded, have your program execute a “test function” that passes a collection of pre-set poker hands, one to each poker-ranking function, in order to validate that each poker-ranking function correctly ranks such a hand. Display each of the pre-set hands and the results of the tests.
Details on poker hand rankings can be found at: https://www.pokerstars.com/poker/games/rules/hand-rankings/.
Input: Accept input via the command-line arguments. Validate command-line input. Input will be the number of cards/hand and the number of hands (players), in that order.
Requirements: Efficiency should always be considered. Always select the most appropriate loop/decision structures and variable/constant types. Functions should focus on a single task. Main() should be high-level tasks only. Use of structures is required. Use of pointers is encouraged but not required. Use of constants and enumerated types is required and will prove useful…
Note: You may NOT use any poker hand ranking “systems” you find on the Internet. Period.
Minimum of two source code files — one for main() and at least one more for other required functions, along with a proper user-defined header file.
Use of a Makefile is required. Include a copy of your Makefile with your submissions, and include in your compile/execute instructions.
Explanation / Answer
#include
#include
#include "io.h"
#include "stats.h"
int main(int argc, char* argv[])
{
int size, i;
char order;
// greet and get the number of stocks
print_greeting();
printf("How many stocks prices would you like to analyze? ");
scanf("%d", &size);
// read the data
float array[size];
read_array(array, size);
// get the stats
float mean = get_average(array, size);
float variance = get_variance(array, size);
float min = get_min(array, size);
float max = get_max(array, size);
float median = get_median(array, size);
// show the results
print_results(array, size, median, min, max, mean, variance);
return 0;
}
Stats.c
#include
#include "stats.h"
// sorts the values of an array according to order
void sort (float output[], const int size, char order)
{
int i, j;
float temp;
if (order == 'a' || order == 'A')
{
for ( i = 0; i < size - 1; ++i )
for ( j = i + 1; j < size; ++j )
if ( output[i] > output[j] )
{
temp = output[i];
output[i] = output[j];
output[j] = temp;
}
}
else if (order == 'd' || order == 'D')
{
for ( i = 0; i < size - 1; ++i )
for ( j = i + 1; j < size; ++j )
if ( output[i] < output[j] )
{
temp = output[i];
output[i] = output[j];
output[j] = temp;
}
}
else
return;
}
// calculates the mean of the elements of an array
float get_average(const float array[], int size)
{
int i;
float sum = 0.0;
for (i = 0; i < size; i++)
sum += array[i];
sum /= size;
return sum;
}
// calculates the variance of the emelemts of an array
// this function calls the get_average to get the mean
float get_variance(const float array[], int size)
{
int i;
float sum = 0.0;
float mean = get_average(array, size);
for (i = 0; i < size; i++)
sum += array[i] * array[i];
sum = sum/size - mean*mean;
return sum;
}
// gets the median of an array after it sorts it
float get_median(const float array[], int size)
{
int i;
float temp_array[size]; // temp array tp be manipulated
float median;
// copy oroginal array to the temp array
for (i = 0; i < size; i++)
temp_array[i] = array[i];
sort(temp_array, size, 'a');
if (size % 2 == 0)
median = (temp_array[size/2] + temp_array[size/2-1])/2.0;
else
median = temp_array[size/2];
return median;
}
// finds the maximum value of the elements of an array
float get_max(const float array[], int size)
{
int i;
float max = array[0];
for (i = 0; i < size; i++)
if (array[i] >= max)
max = array[i];
return max;
}
// finds the minimum value of the elements of an array
float get_min(const float array[], int size)
{
int i;
float min = array[0];
for (i = 0; i < size; i++)
if (array[i] <= min)
min = array[i];
return min;
}
stats.h
#ifndef STATS_H
#define STATS_H
void sort (float output[], int size, char order);
float get_average(const float array[], int size);
float get_variance(const float array[], int size);
float get_median(const float array[], int size);
float get_max(const float array[], int size);
float get_min(const float array[], int size);
#endif
io.c
#include
#include "io.h"
// prompt the user for input and read the values into an array
void read_array(float array[], int size)
{
int i = 0;
for (i = 0; i < size; i++)
{
printf ("Please enter stock #%d: ", i+1);
scanf("%f", &array[i]);
}
}
// say hi to the user
void print_greeting(void)
{
printf("This program asks for user input and gives variance, average, median, min, and max of stock prices. ");
}
// display array values
void print_array(const float array[], int size)
{
int i = 0;
for (i = 0; i < size; i++)
printf("%.2f ", array[i]);
printf(" ");
}
// print the stat results including input data
void print_results(const float array[], int size, float median, float min, float max, float mean, float variance)
{
printf(" Here are the Calculations ");
print_array(array, size);
printf("median: $%.2f ", median);
printf("min: $%.2f ", min);
printf("max: $%.2f ", max);
printf("variance: $%.2f ", variance);
printf("mean: $%.2f ", mean);
}
io.h
#ifndef IO_H
#define IO_H
void read_array(float array[], int size);
void print_greeting(void);
void print_array(const float array[], int size);
void print_results(const float array[], int size, float median, float min, float max, float mean, float variance);
#endif
utils.c
#include
#include
#include
#include "utils.h"
int get_num_tokens(const char str[], char delim)
{
int i = 0,count = 1;
while (str[i] != '')
{
if (str[i++] == delim)
count++;
}
return count;
}
void get_tokens_array(const char str[],float array[], float size, char delim)
{
char *input = const_cast(str);
char *d = &delim;
char *token = strtok(input, d);
int i = 0;
while (token != NULL)
{
printf(" token # %d is %s ", i+1, token);
array[i++] = atof(token);
token = strtok(NULL, d);
}
size = i;
}
Utils.h
#ifndef UTILS_H
#define UTILS_H
int get num tokens(const char str[], char delim);
void get tokens array(const char str[], float array[], float size, char delim);
#endif
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.