IN C ONLY PLEASE: Write a program that will prompt the user for the number of pl
ID: 3604281 • Letter: I
Question
IN C ONLY PLEASE: Write a program that will prompt the user for the number of players (maximum of 5) prompt the user for the number of cards to generate for each player (maximum of 10) for each card for each player, generate a valid bingo card (5 columns and 5 rows, here are some images of bingo cards provide a menu for the user to select which user/bingo card to display to run a histogram on the bingo cards generated to see how many times each number between 1 and 75 appeared. This is my code an it seems to work, but how do i input the "Free" space in the middle of the bingo board?
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int get_random_number(int min, int max)
{
return min + (rand() % (max - min + 1));
}
int min_for_col(int column_index)
{
return (column_index * 15) + 1;
}
int max_for_col(int column_index)
{
return (column_index + 1) * 15;
}
void print_bingo_card(int cards[5][10][5][5], int player, int card)
{int i, j;
printf("B I N G O ");
for(i = 0; i < 5; ++i) {
for(j = 0; j < 5; ++j) {
if(i == 2 && j ==2) {
printf(" ");
}
else{
printf("%2d ", cards[player][card][i][j]);
}
}
printf(" ");
}
printf(" ");
}
int main () {
srand(time(NULL));
int num_players, num_cards, i, j, k, l, m, num;
int bingo_cards[5][10][5][5];
int count[75];
int num_rows = 5, num_cols = 5;
int already_exists = 0;
printf("Enter number of players:"); //asking for player count
scanf("%d", &num_players);//scanning user input
printf("Enter number of cards per players:");//Asking for number of cards per person.
scanf("%d", &num_cards);//scanning user input
for(i = 0; i < num_players; ++i){
for(j = 0; j < num_cards; ++j){
for (k = 0; k < num_cols; ++k) {
for(l = 0; l < num_rows; ++l){
while(1) {
num = get_random_number(min_for_col(k), max_for_col(k));
already_exists = 0;
for(m = 0; m < 1; ++m){
if(bingo_cards[i][j][m][k] == num){
already_exists = 1;
}
}
if(already_exists == 0){
break;
}
}
bingo_cards[i][j][l][k] = num;
}
}
}
}
//histogram calculations
for( i = 0; i < 75; ++i){
count[i] = 0;
}
for(i = 0; i < num_players; ++i){
for(j = 0; j < num_cards; ++j){
for (k = 0; k <num_rows; ++k) {
for(l = 0; l < num_cols; ++l){
if(!(k ==2 && 1==2)){
count[bingo_cards[i][j][k][l] - 1]++;
}
}
}
}
}
while(1){ //while(1) means while true. so the loop continues to loop until the user presses 3 to exit the loop.
printf("Press 1 to select user bingo card to display ");
printf("Press 2 to show histogram ");
printf("Press 3 to exit ");
printf("Please enter your choice: ");//asking for user input
scanf("%d", &i);//reading user input
switch(i){
case 1:
printf("Enter player number(%d-%d): ", 1, num_players);
scanf("%d", &j);
printf("Enter card number(%d-%d):", 1, num_cards);
scanf("%d", &k);
print_bingo_card(bingo_cards, j - 1, k - 1);
break;
case 2:
for (j = 0; j < 75; ++j) {
printf("%2d: ", j + 1);
for(k = 0; k < count[j]; ++k) {
printf("*"); /* when the histogram option is selected, numbers rangine from 1-75 will be shown. The asterisk will represent
how many times each number has appearead on the cards.*/
}
printf(" ");
}
break;
case 3:
return 0;
default: //in case the user inputs an invalid number they are told to make another selection.
printf("Invalid Choice. Make another selection.");
break;
}
}
return 0;
}
Explanation / Answer
// C code
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
int get_random_number(int min, int max) {
return min + (rand() % (max - min + 1));
}
int min_for_col(int column_index) {
return (column_index * 15) + 1;
}
int max_for_col(int column_index) {
return (column_index + 1) * 15;
}
void print_bingo_card(int cards[5][10][5][5], int player, int card) {
int i, j;
printf(" B I N G O ");
for(i = 0; i < 5; ++i) {
for(j = 0; j < 5; ++j) {
if(i == 2 && j == 2) {
printf(" ");
} else {
printf("%2d ", cards[player][card][i][j]);
}
}
printf(" ");
}
printf(" ");
}
int main() {
srand(time(NULL));
int num_players, num_cards, i, j, k, l, m, num;
int bingo_cards[5][10][5][5];
int count[75];
int num_rows = 5, num_cols = 5;
int already_exists = 0;
printf("Please enter number of players: ");
scanf("%d", &num_players);
printf("Please enter number of cards per player: ");
scanf("%d", &num_cards);
for(i = 0; i < num_players; ++i) {
for(j = 0; j < num_cards; ++j) {
for(k = 0; k < num_cols; ++k) {
for(l = 0; l < num_rows; ++l) {
while(1) {
num = get_random_number(min_for_col(k), max_for_col(k));
already_exists = 0;
for(m = 0; m < l; ++m) {
if(bingo_cards[i][j][m][k] == num) {
already_exists = 1;
}
}
if(already_exists == 0) {
break;
}
}
bingo_cards[i][j][l][k] = num;
}
}
}
}
// calculate histogram
for(i = 0; i < 75; ++i) {
count[i] = 0;
}
for(i = 0; i < num_players; ++i) {
for(j = 0; j < num_cards; ++j) {
for(k = 0; k < num_rows; ++k) {
for(l = 0; l < num_cols; ++l) {
if(!(k == 2 && l == 2)) {
count[bingo_cards[i][j][k][l] - 1]++;
}
}
}
}
}
while(1) {
printf("Please choose an option from the following menu: 1. Display a bingo card ");
printf("2. Run a histogram across all bingo cards generated ");
printf("3. Exit ");
printf("Please enter your choice: ");
scanf("%d", &i);
switch(i) {
case 1:
printf("Please enter player number(%d - %d): ", 1, num_players);
scanf("%d", &j);
printf("Please enter card number(%d - %d): ", 1, num_cards);
scanf("%d", &k);
print_bingo_card(bingo_cards, j - 1, k - 1);
break;
case 2:
for(j = 0; j < 75; ++j) {
printf("%2d: ", j + 1);
for(k = 0; k < count[j]; ++k) {
printf("*");
}
printf(" ");
}
break;
case 3:
return 0;
default:
printf("Invalid choice. Please try again!!");
break;
}
}
return 0;
}