I am in the middle of a project (in C) where I am programming a bingo game, and there is one last function to make, which is the one to check if there is a bingo or not. I have an array of 5 x 5 generated random numbers, and I generate a random number through user input. How do I make the array change that number (if it is in fact in the array) to 0, and then check if there is a bingo or not through user input?
Here is the code to generate the array
for (int row = 0; row < 5; row++) {
for(int column = 0; column < 5; column++) {
if(row == 2 && column == 2) {
board[row][column] = 0;
} else {
int num = rand() %15 + 1 +(column * 15);
for(int i = 0; i < 75; i++) {
if(num == used[i]) {
num = rand() %15 + 1 +(column * 15);
}
}
board[row][column] = num;
used[used_counter] = num;
used_counter++;
}
}
}
int generate_number(int boneyard[75], int *boneyard_counter) {
int num = rand() %75 + 1;
for (int i = 0; i <75; i++){
if(num == boneyard[i]);
num = rand() %75 + 1;
}
boneyard[*boneyard_counter] = num;
boneyard_counter++;
return num;
}
And here is the code to generate the random number.
You'd do something like this:
int found_match = 0;
for(int row = 0; row < 5; ++row){
for(int column = 0; column < 5; ++column){
if(board[row][column] == num){
board[row][column] = 0;
found_match = 1;
goto loop_end;
}
}
}
loop_end:
if(found_match){
/*
Check all diagonals, rows, and columns to see if any one of them contains only zeroes,
stopping if you find that a diagonal/row/column that does in fact contain only zeroes
and informing the player that they've won.
*/
}
Related
I'm trying to create a function that compares two four digit numbers and
returns the number of similar digits between the two. For example, with a generated number of 4311 and the user entered 1488,
the score should return 2 (4 and 1).
If it was 4311 and the other is 1147,
the score should return three (1, 1 and 4). I don't know why it isn't giving me the right outputs, hope you can help.
int getSameDigitScore(int playerGuess, int generatedNum) {
int score = 0;
int i;
int j;
int k;
int generatedNumArray[4];
int playerGuessArray[4];
// turns playerGuess into an array
while (playerGuess > 0 ) {
i = 0;
playerGuessArray[i] = playerGuess % 10;
i++;
playerGuess /= 10;
}
// turns generatedNum into an array
while (generatedNum > 0) {
i = 0;
generatedNumArray[i] = generatedNum % 10;
i++;
generatedNum /= 10;
}
// compares the two arrays
for (k = 3; k >= 0; k--) {
for (j = 3; j >= 0; j--) {
if (generatedNumArray[k] == playerGuessArray[j]) {
score++;
playerGuessArray[j] = 0;
j = -5;
}
}
}
return score;
}
You are assigning i = 0 inside the while loop while generating the playerGuessArray and generatedNumArray. Due to which the playerGuess and generatedNumArray array will have elements as first digit of your number 0 0 0 .
Move the initialization out of the loop.
int getSameDigitScore(int playerGuess, int generatedNum) {
int score = 0;
int i, j, k, n;
int generatedNumArray[4];
int playerGuessArray[4];
// turns playerGuess into an array
i = 0; // This has been out of while loop
while (playerGuess > 0 ) {
playerGuessArray[i] = playerGuess % 10;
i++;
playerGuess /= 10;
}
// turns generatedNum into an array
int n = 0; // This has been out of the while loop
while (generatedNum > 0) {
generatedNumArray[n] = generatedNum % 10;
n++;
generatedNum /= 10;
}
// compares the two arrays
for (k = 3; k >= 0; k--) {
for (j = 3; j >= 0; j--) {
if (generatedNumArray[k] == playerGuessArray[j]) {
score++;
playerGuessArray[j] = 0;
j = -5;
}
}
}
return score;
}
int main() {
int m;
n = getSameDigitScore(1231, 2342);
printf("Score is: %d\n", m);
}
You're re-initializing increment variable i on every iteration which should be moved out of the while loop. With that moved out the above code works fine.
There are the following issues with the code.
You are initializing the integer i inside the while loop. This needs to be done before the loop for each loop.
You need a separate array to get the output of equal digits. See AnswerArray in code below. Also it is a good design practice to pass this array to the function and clear this array inside the function.
In the last for loop, you should break from the inner loop after getting a match. This is to take care of cases where playerGuess == 1222 and generatedNum = 1111 In the code shown this will result in a score of 1.
See the final code below with some test cases.
int getSameDigitScore(int playerGuess, int generatedNum, int *AnswerArray) {
int score = 0;
int i;
int j;
int k;
int generatedNumArray[4] = {0};
int playerGuessArray[4] = {0};
memset(AnswerArray,0,4*sizeof(int));
// turns playerGuess into an array
i = 0;
while (playerGuess > 0 ) {
playerGuessArray[i] = playerGuess % 10;
i++;
playerGuess /= 10;
}
// turns generatedNum into an array
i = 0;
while (generatedNum > 0) {
generatedNumArray[i] = generatedNum % 10;
i++;
generatedNum /= 10;
}
// compares the two arrays
score=0;
for (k = 3; k >= 0; k--) {
for (j = 3; j >= 0; j--) {
if (generatedNumArray[k] == playerGuessArray[j]) {
AnswerArray[score++] = generatedNumArray[k];
playerGuessArray[j] = -1;
break;
}
}
}
return score;
}
int main(void)
{
int AnswerArray[4],score;
score = getSameDigitScore(4311,1488,AnswerArray);
printf ("\nScore = %d \n Answer Array = ",score);
for (int i=0; i<score; i++)
{
printf ("%d ",AnswerArray[i]);
}
score = getSameDigitScore(4311,1147,AnswerArray);
printf ("\nScore = %d \n Answer Array = ",score);
for (int i=0; i<score; i++)
{
printf ("%d ",AnswerArray[i]);
}
score = getSameDigitScore(1222,1111,AnswerArray);
printf ("\nScore = %d \n Answer Array = ",score);
for (int i=0; i<score; i++)
{
printf ("%d ",AnswerArray[i]);
}
score = getSameDigitScore(1111,1222,AnswerArray);
printf ("\nScore = %d \n Answer Array = ",score);
for (int i=0; i<score; i++)
{
printf ("%d ",AnswerArray[i]);
}
}
The initializing i=0 which you made inside the loop should be outside the loop.
while (playerGuess > 0 ) {
i = 0;
playerGuessArray[i] = playerGuess % 10;
i++;
playerGuess /= 10;
}
If the initialization is inside the looop then,
Everytime playerGuessArray[0] value will be updated.
FYI:
If playerGuess can contain 0 aat the begin of four digit like 0123
For example, playerGuessValue is 0123, Then by using
while (playerGuess > 0 ) {
i = 0;
playerGuessArray[i] = playerGuess % 10;
i++;
playerGuess /= 10;
}
playerGuessArray will contain only [1,2,3] instead of [0,1,2,3].
So, the better solution would be taking two temporary variables and checking last digit one by one.
Like this:
int temp1=playerGuess, temp2=GeneratedNum;
int i=0;
bool flag = true;
while(flag && i < 4){
if(temp1%10 != temp2%10){
flag = false;
}
temp1 /= 10;
temp2 /= 10;
i++;
}
if(flag){
score++;
}
FYI:
Debugging will help you in finding out these little mistakes.So, try to debug your code with multiple inputs and verify your answer.
Here are few reference on how to debug:
https://blog.hartleybrody.com/debugging-code-beginner/
https://www.codementor.io/mattgoldspink/how-to-debug-code-efficiently-and-effectively-du107u9jh%60
Thanks.
I have implemented a basic program which generates 10 random and unique numbers from 1 to 10 as shown below. I have added an extra part in which I want the binary representation for each unique and random number. My program looks like this.
int value=1, loop, loop1, get=0, x, arr[10], array[20], count, i =0, y;
srand(time(NULL));
for (x = 0; x < 10; x++)
{
for (count = 0; count < 10; count++) {
array[count] = rand() % 10 + 1; //generate random number between 1 to 10 and put in array
}
while (i < 10) {
int r = rand() % 10 + 1; // declaring int r
for (x = 0; x < i; x++)
{
if (array[x] == r) { //if integer in array x is equal to the random number generated
break; //break
}
}
if (x == i) { //if x is equal to i then
array[i++] = r; //random number is placed in array[10]
}
}
for (y = 0; y < 10; y++) {
printf("unique random number is %d\n", array[y]);
array[y] = value;
for (loop = 0; loop < 1000; loop++)
{
if (value <= 1) { arr[loop] = 1; break; } //if value is 1 after dividing put 1 in array
if (value % 2 == 0) arr[loop] = 0;
else arr[loop] = 1;
value = value / 2;
}
for (loop1 = loop; loop1 > -1; loop1--)
printf("%d", arr[loop1]);
printf("\n");
}
}
My problem is that The binary value for each random unique number is being given as 1. In this program it is seen that I initialised value=1 and this can be the source for my error, however when I remove this I get an error stating that the local variable is uninitialised.
The first part of my program which generates the unique numbers is working fine, however the second part where I am converting to binary is not.
EDIT: I tested The second part of my program and it works well on it's own. The problem must be the way I am combining the two programs together.
Statement array[y] = value overrides the previously generated random values with constant 1.write
value = array[y];
Here is the commented code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//function to check unique random number
int check(int array[],int count,int val)
{
int i=0;
for( ;i<count ;++i )
{
if (array[i]==val)
return 1;
}
return 0;
}
int main(void)
{
int value,loop, loop1, get, x, arr[10];
int oldRandoms[10]; // array to preserve old random values
srand(time(NULL));
for (x = 0; x < 10; x++)
{
do // do while to get only unique random number
{
get = rand() % 10 + 1;
}while(check(oldRandoms,x,get));
oldRandoms[x]=get; // backup the number
printf("random number is %d \n", get);
value = get;
for (loop = 0; loop < 1000; loop++)
{
if (value <= 1) { arr[loop] = 1; break; } //if value is 1 after dividing put 1 in array
if (value % 2 == 0) arr[loop] = 0;
else arr[loop] = 1;
value = value / 2;
}
for (loop1 = loop; loop1 > -1; loop1--)
printf("%d", arr[loop1]);
printf("\n");
}
}
int main() //8th task
{
int longNum, shortNum, tempNum[5], i;
printf("Please enter 2 numbers (5 digits and 1 digit, ex: 12345 and 5)\n");
scanf("%d%d", &longNum, &shortNum);
for (i = 4; i >= 0; i--)
{
if (longNum % 10 != shortNum)
{
tempNum[i] = longNum % 10;
longNum /= 10;
}
else tempNum[i] = ; // Delete the digit that == shortNum.
}
for (i = 0; i < 5; i++)
{
printf("%d", tempNum[i]);
}
printf("\n");
return 0;
}
This program check if longNum has shortNum in it and suppose to remove the number (and his array slot) from longNum.
I've tried couple of things to make it work with no success.
I'd like to know what is the best way to do it (im not sure what the 'else' should be).
It is possible to skip all shortNum digits in the parsing loop. One more variable is needed to track number of deleted digits:
int n = 5;
for (i = 4; i >= 0; i--)
{
int tmp = longNum % 10;
longNum /= 10;
if (tmp != shortNum)
tempNum[--n] = tmp;
}
// here n is number of deleted digits
for (i = n; i < 5; i++)
{
printf("%d", tempNum[i]);
}
So, actually elements are not deleted from array. They are not written to that array. It is also possible to reverse elements order, so the first array element will be meaningful. Now if some element is skipped the first element of tempNum contains junk.
you need to skip the value that you don't want, and not insert it at all to the array.
int len = 0;
for (i = 4; i >= 0; i--)
{
if (longNum % 10 != shortNum)
{
tempNum[len] = longNum % 10;
len++;
}
longNum /= 10;
}
for (i = 0; i < len; i++)
{
printf("%d", tempNum[i]);
}
else tempNum[i] = ; this part is very wrong. You have to assing something like else tempNum[i] = 0;. And you can't actually delete anything from these arrays - they are not dynamic. I suggest you read up on dynamic arrays.
I have made a version of conways game of life in C, using a 2d array which should wrap around the sides. Unfortunately all that happens is the numbers flick back and forth between 1 and 0 with no clear pattern. Here is the code:
#include <stdio.h>
int main(){
int const WIDTH = 100;
int const HEIGHT = 100;
int const CYCLES = 1000;
int grid[HEIGHT][WIDTH];
int temp[HEIGHT][WIDTH];
int row;
int col;
for(row = 0; row < HEIGHT; row++){
for(col = 0; col < WIDTH; col++){
grid[row][col] = 0;
}
}
int i;
int x;
int y;
int neighbours;
for(i = 0; i < CYCLES; i++){
for(row = 0; row < HEIGHT; row++){
for(col = 0; col < WIDTH; col++){
temp[row][col] = 0;
}
}
for(row = 0; row < HEIGHT; row++){
for(col = 0; col < WIDTH; col++){
neighbours = 0;
for(y = -1; y < 2; y++){
for(x = -1; x < 2; x++){
if(x != 0 && y != 0 && grid[(row + y) % HEIGHT][(col + x) % WIDTH] == 1){
neighbours++;
}
}
}
if(grid[row][col] == 1){
if(neighbours < 2 || neighbours > 3){
temp[row][col] = 0;
}else{
temp[row][col] = 1;
}
}else if(grid[row][col] == 0){
if(neighbours == 3){
temp[row][col] = 0;
}else{
temp[row][col] = 1;
}
}
}
}
for(row = 0; row < HEIGHT; row++){
for(col = 0; col < WIDTH; col++){
grid[row][col] = temp[row][col];
printf("%d", grid[row][col]);
}
printf("\n");
}
printf("\n");
}
}
I do notice one problem.
The 4th rule states that a dead cell should become alive again if it has exactly 3 neighbors. Currently, your code does the opposite
else if(grid[row][col] == 0){
if(neighbours == 3){
temp[row][col] = 0;
}else{
temp[row][col] = 1;
}
}
This will leave the cell dead if there are exactly 3 and make it alive when that is not the case. Switch the 1 and the 0 and it should work.
The way you count your neighbors is false (what about -1%HEIGHT for example???). I suppose that you want to use a torus (leftmost column connected to rightmost column and the same for lines), so you need to make special cases for borders. A trick is to use modulus like the following.
Suppose you have a line of length N, then for each x from 0 to N-1, compute mid=x+N, get neighbors as left=mid-1 and right=mid+1, then count neighbors with grid(left%N), grid(mid%N), grid(right%N) (add second dimension the same way of course). So you will catch the torus property without any special cases...
If you want to be sure that it works as expected, I can suggested you to initialize the grid to a well-known GOL pattern (a simple glider for example).
Also verify that the GOL rules are the right ones.
I have been doing homework yesterday, I have done most of it, but couldn't make the main thing. I don't know why it's not working I have asked other students, but nobody knows what's the problem. Basically this program is a small game, there are 18 players 9 on each team. the program randomly gives players coordinates and directions and they start to move. I have basically done the program, but I have problem with field, It doesn't show the players at all.
I tried many things and when testing noticed that it doesn't print even testing string in the if statement I wrote. when I write this part field[i][j] = &players[k][0]; I have checked if field[i][j] really gets the x and y coordinate and yes it does. but in the print_field class it takes field[][] as null and the field is empty. players is an array of structs. field is an array of pointers that point to players or NULL.
I have tried with all of my knowledge and couldn't make any better.
What is wrong with this code? Why isn't it showing the players on the field?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define LENGTH 25
#define WIDTH 25
enum direction {Right, Left, Up, Down}; /*Right = 0, Left = 1, Up = 2, Down = 3*/
void print_field();
void random_positions();
void playerdirection();
void motion();
void game();
struct player
{
char *dora;
char *team;
char *name; //string?
int x,y; //coordinates
int direction;
};
typedef struct player Player;
struct player *field[WIDTH][LENGTH];
Player players[8][1];
int main()
{
srand (time(NULL));
int i; //players 9 in each team team1 = 0 team2 = 1
players[0][0].name = "A1";
players[1][0].name = "A2";
players[2][0].name = "A3";
players[3][0].name = "A4";
players[4][0].name = "A5";
players[5][0].name = "A6";
players[6][0].name = "A7";
players[7][0].name = "A8";
players[8][0].name = "A9";
players[0][1].name = "B1";
players[1][1].name = "B2";
players[2][1].name = "B3";
players[3][1].name = "B4";
players[4][1].name = "B5";
players[5][1].name = "B6";
players[6][1].name = "B7";
players[7][1].name = "B8";
players[8][1].name = "B9";
for(i = 0; i < 9 ; i++)
{
players[i][0].team = "Team A";
players[i][1].team = "Team B";
players[i][0].dora = "Alive";
players[i][1].dora = "Alive";
}
random_positions();
playerdirection();
print_field();
motion (Player player);
print_field();
game();
return 0;
}
void random_positions()
{
int i,j,k;
int xs[17],ys[17];
for(i= 0; i<9 ; i++)
{
players[i][0].x = rand() % 25;
players[i][0].y = rand() % 25;
players[i][1].x = rand() % 25;
players[i][1].y = rand() % 25;
printf("A%d x = %d y = %d \n",i+1,players[i][0].x,players[i][0].y);
printf("B%d x = %d y = %d \n",i+1,players[i][1].x,players[i][1].y);
}
for(i = 0; i < 9 ; i++)
{
xs[i] = players[i][0].x;
xs[i+8] = players[i][1].x;
ys[i] = players[i][0].y;
ys[i+8] = players[i][1].y;
for(j = 0; j <= i ; j++)
{
//printf("j%d start\n",j);
if(i != j && xs[i] == xs[j])
{
//printf("i%d start\n",j);
if(ys[i] == ys[j])
{
return random_positions();
}
//("j%d done\n",j);
}
//printf("j%d done\n",j);
}
}
for(i = 0; i < 25; i++)
{
for(j = 0; j < 25; j++)
{
for(k = 0; k < 9; k++)
{
if(i == players[k][0].x && j == players[k][0].y)
{
field[i][j] = &players[k][0];
}
if(i == players[k][1].x && j == players[k][1].y)
{
field[i][j] = &players[k][1];
}
else field[i][j] = NULL; //I da J sheidzleba shesacvleli iyos
}
}
}
}
/*this function prints out the given state of the field*/
void print_field(){
int i,j;
printf("\n");
printf("|0 1 2 3 4 5 6 7 8 9 101112131415161718192021222324|\n"); /*just to show easier the allignment*/
for(j=0; j<WIDTH+2; j++) /*This first loop goes through row and creates them each by each*/
{
if(j == 0 || j == WIDTH +1) /*creates the upper and lower part of the field*/
for(i=0; i<LENGTH+2; i++) /*there should be space for frame so I added 2 to LENGTH in the loop*/
{
if(i==0)
printf("-");
else if(i == LENGTH+1)
printf("-\n");
else printf("--"); /*3 decimals*/
}
else
for(i=0; i<LENGTH+2; i++) /*Goes through the columns in this row and creates either frame or puts the nodeid*/
{
if(i==0)printf("|"); /*frame*/
else if(i == LENGTH+1) printf("| %d\n",(j-1)); /*frame*/
else if(field[j-1][i-1] != NULL)
{
printf("aaa");
printf("%-*s",2,(*field[j-1][i-1]).name); /*putting nodeid 3 decimals*/
}
else printf(" ");
}
}
printf("\n");
}
You need Player[9][2] instead of Player[8][1]. You should initialize an array with its length although you could only access index up to length - 1 because arrays are indexed from 0.