Missing output from C Blackjack game's int array - c

So I am trying to write a Blackjack game in C just for the fun of it (something I have never tried before). I have gotten past the first few steps such as how to set up the deck and how to give the cards to the dealer and player. However, the displayed output is not showing what I want it too (showing the dealer only has 1 card when it should be showing 2). Here is a screenshot:
I did run this on my phone (easier to get a screenshot), but I am having the same results on my computer using Code::Blocks. Anyways, I have two functions to handle setting up the deck and to display the current cards. The dealing of the cards I am handling in the main() function using loops. I know that the makeDeck() function is working correctly, so I am assuming that my error is somewhere else. Have a look at my code and let me know what's going on (also, any suggestions for improvement would be appreciated):
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/*Function Prototypes*/
int* makeDeck();
/*Usage - returns an int* to be used for the deck */
void showHands(int[], int[]);
/*Usage - pass dealerHand[], then playerHand[] */
int main() {
srand(time(0)); //Make a new rand() seed value
int x, count = 0, choice = 1;
int* cards = makeDeck();
int dealerCards[12]; //cards in hand will never exceed 11
int playerCards[12]; //4 A's, 4 2's, 3 3's
/*Loop to run the game. One iteration per hand */
while(choice != 0) {
for(x = 0; x < 12; x++) {
dealerCards[x] = 0;
playerCards[x] = 0;
}
//Deal 2 cards to dealer and player
for(x = 0; x < 2; x++) {
dealerCards[x] = cards[count];
count++;
}
for(x = 0; x < 2; x++) {
playerCards[x] = cards[count];
count++;
}
showHands(dealerCards, playerCards);
/*DEBUGGING */
printf("\nEnter 0 to exit loop: ");
scanf("%i", &choice);
}
return 0;
}
//Declare placeholder variable "bunchOfCards" globally
int bunchOfCards[52];
int* makeDeck(){
int* deck = bunchOfCards;
int x = 0,
y = 0,
card = 0;
for(x = 0; x < 52; x++) { //set all cards to 0
deck[x] = 0;
}
for(x = 0; x < 4; x++) { //set up deck
for(y = 1; y < 14; y++) {
card = (rand() % 52);
//check if deck position is already used
while(deck[card] != 0) {
card = (rand() % 52);
}
deck[card] = y;
}
}
/* DEBUGGING
for(x = 0; x < 52; x++) {
printf("%i\t", deck[x]);
} */
return deck;
}
void showHands(int* dealer, int* player) {
int x; char card[3] = { '\0', '\0', '\0' };
puts("The hands are: \n\nDealer:");
//Display dealer cards
for(x = 0; x < 12; x++) {
if(dealer[x] != 0) {
if((dealer[x] < 10) && (dealer[x] != 1)) {
card[0] = (char)(((int)'0') + dealer[x]);
card[1] = '\0';
} else if(dealer[x] == 1) {
card[0] = 'A';
card[1] = '\0';
} else if(dealer[x] == 10) {
card[0] = '1';
card[1] = '0';
} else if(dealer[x] == 11) {
card[0] = 'J';
card[1] = '\0';
} else if(dealer[x] == 12) {
card[0] = 'Q';
card[1] = '\0';
} else if(dealer[x] == 13) {
card[0] = 'K';
card[1] = '\0';
}
printf("\t%s", card);
}
//Display player cards
puts("\nPlayer: ");
for(x = 0; x < 12; x++) {
if(player[x] != 0) {
if((player[x] < 10) && (player[x] != 1)) {
card[0] = (char)(((int)'0') + player[x]);
card[1] = '\0';
} else if(player[x] == 1) {
card[0] = 'A';
card[1] = '\0';
} else if(player[x] == 10) {
card[0] = '1';
card[1] = '0';
} else if(player[x] == 11) {
card[0] = 'J';
card[1] = '\0';
} else if(player[x] == 12) {
card[0] = 'Q';
card[1] = '\0';
} else if(player[x] == 13) {
card[0] = 'K';
card[1] = '\0';
}
printf("\t%s", card);
}
}
}
}

Your for-loops appear to include each other.
for(x = 0; x < 12; x++) {
if(dealer[x] != 0) {
...
}
puts("\nPlayer: ");
for(x = 0; x < 12; x++) {
...
}
}
The first for-loop starts with x = 0, the second for loop goes up to x = 12, then the first for-loop stops before executing a second time because it reached the terminating condition.

Related

Why do I get a segmentation fault accessing a struct?

Edit: added the rest of my code so it's easier to see
I'm receiving a segmentation fault when trying to access certain values of a struct in a header file.
Here's courses.c:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "structs.h"
void createStudents () {
int random, i;
for (i = 0; i < 12; i++) {
students[i].firstName = firstName[i];
students[i].lastName = lastName[i];
random = 10000 + rand() % 89999;
students[i].num.studentNum = random;
printf("%d - %s, %s \n", students[i].num.studentNum, students[i].lastName, students[i].firstName);
}
}
void createCourses () {
int numbers[999];
int numbersLeft = 999;
char courseCode[512];
char courseCode1[512];
char courseCode2[512];
int numCourses = 3;
int random, i, j;
for (i = 0; i < 999; i++) {
numbers[i] = i;
}
for (j = 0; j < 1; j++) {
random = rand() % numbersLeft;
if (random < 10) {
snprintf(courseCode, sizeof courseCode, "CS00%d", random);
courses[0].cCode = courseCode;
}
else if (random < 100 && random > 9) {
snprintf(courseCode, sizeof courseCode, "CS0%d", random);
courses[0].cCode = courseCode;
}
else if (random > 99){
snprintf(courseCode, sizeof courseCode, "CS%d", random);
courses[0].cCode = courseCode;
}
courses[0].cName = courseName[0];
courses[0].cDescription = courseDescription[0];
numbers[random] = numbers[numbersLeft-1];
numbersLeft--;
random = 4 + rand() % 4;
courses[0].maxRegister = random;
}
for (j = 0; j < 1; j++) {
random = rand() % numbersLeft;
if (random < 10) {
snprintf(courseCode1, sizeof courseCode1, "CS00%d", random);
courses[1].cCode = courseCode1;
}
else if (random < 100 && random > 9) {
snprintf(courseCode1, sizeof courseCode1, "CS0%d", random);
courses[1].cCode = courseCode1;
}
else if (random > 99){
snprintf(courseCode1, sizeof courseCode1, "CS%d", random);
courses[1].cCode = courseCode1;
}
courses[1].cName = courseName[1];
courses[1].cDescription = courseDescription[1];
numbers[random] = numbers[numbersLeft-1];
numbersLeft--;
random = 4 + rand() % 4;
courses[1].maxRegister = random;
}
for (j = 0; j < 1; j++) {
random = rand() % numbersLeft;
if (random < 10) {
snprintf(courseCode2, sizeof courseCode2, "CS00%d", random);
courses[2].cCode = courseCode2;
}
else if (random < 100 && random > 9) {
snprintf(courseCode2, sizeof courseCode2, "CS0%d", random);
courses[2].cCode = courseCode2;
}
else if (random > 99){
snprintf(courseCode2, sizeof courseCode2, "CS%d", random);
courses[2].cCode = courseCode2;
}
courses[2].cName = courseName[2];
courses[2].cDescription = courseDescription[2];
numbers[random] = numbers[numbersLeft-1];
numbersLeft--;
random = 4 + rand() % 4;
courses[2].maxRegister = random;
}
}
void regiserStudents () {
int checkSum = 0, checkSum1 = 0, checkTemp = 0, count0 = 0, count1 = 0, count2 = 0;
int wCount0 = 0, wCount1 = 0, wCount2 = 0;
int v, i, j, random, max0, max1, max2;
max0 = courses[0].maxRegister;
max1 = courses[1].maxRegister;
max2 = courses[2].maxRegister;
for (i = 0; i < 2; i++) {
checkTemp = count0;
for (j = 0; j < 12; j++) {
random = rand() % 3;
if (random == 0) {
if (count0 == 0) {
courses[random].registered[count0] = &students[j];
count0++;
}
else {
checkSum1 = students[j].num.studentNum;
for (v = 0; v < checkTemp; v++) {
checkSum = courses[0].registered[v]->num.studentNum;
if (checkSum == checkSum1) {
/*Do Nothing*/
}
else if (count0 == max0) {
courses[random].waitlisted[count0] = &students[j];
wCount0++;
}
else {
courses[random].registered[count0] = &students[j];
count0++;
}
/*}*/
}
}
}
if (random == 1) {
if (count1 == 0) {
courses[random].registered[count1] = &students[j];
count1++;
}
else {
checkSum1 = students[j].num.studentNum;
for (v = 0; v < checkTemp; v++) {
checkSum = courses[1].registered[v]->num.studentNum;
if (checkSum == checkSum1) {
/*Do Nothing*/
}
else if (count1 == max1) {
courses[random].waitlisted[count1] = &students[j];
wCount1++;
}
else {
courses[random].registered[count1] = &students[j];
count1++;
}
}
}
}
if (random == 2) {
if (count2 == 0) {
courses[random].registered[count2] = &students[j];
count2++;
}
else {
checkSum1 = students[j].num.studentNum;
for (v = 0; v < checkTemp; v++) {
checkSum = courses[2].registered[v]->num.studentNum;
if (checkSum == checkSum1) {
/*Do Nothing*/
}
else if (count2 == max2) {
courses[random].waitlisted[count2] = &students[j];
wCount2++;
}
else {
courses[random].registered[count2] = &students[j];
count2++;
}
}
}
}
}
}
courses[0].studentRegistered = count0;
courses[1].studentRegistered = count1;
courses[2].studentRegistered = count2;
courses[0].studentWaitlisted = wCount0;
courses[1].studentWaitlisted = wCount1;
courses[2].studentWaitlisted = wCount2;
}
void printCourses () {
int i;
printf("\n%s - %s: %s\nRegistered Students (%d/%d):\n", courses[0].cCode, courses[0].cName, courses[0].cDescription, courses[0].studentRegistered, courses[0].maxRegister);
for (i = 0; i < courses[0].studentRegistered; i++) {
printf("* %d - %s, %s \n", courses[0].registered[i]->num.studentNum, courses[0].registered[i]->lastName, courses[0].registered[i]->firstName);
}
printf("Waitlisted Students (%d)", courses[0].studentWaitlisted);
if (courses[0].studentWaitlisted == 0) {
printf("\n");
}
else {
for (i = 0; i < courses[0].studentWaitlisted; i++) {
printf("* %d - %s, %s \n", courses[0].waitlisted[i]->num.studentNum, courses[0].waitlisted[i]->lastName, courses[0].waitlisted[i]->firstName);
}
}
printf("\n%s - %s: %s\nRegistered Students (%d/%d):\n", courses[1].cCode, courses[1].cName, courses[1].cDescription, courses[1].studentRegistered, courses[1].maxRegister);
for (i = 0; i < courses[1].studentRegistered; i++) {
printf("* %d - %s, %s \n", courses[1].registered[i]->num.studentNum, courses[1].registered[i]->lastName, courses[1].registered[i]->firstName);
}
printf("\n%s - %s: %s\nRegistered Students (%d/%d):\n", courses[2].cCode, courses[2].cName, courses[2].cDescription, courses[2].studentRegistered, courses[2].maxRegister);
for (i = 0; i < courses[2].studentRegistered; i++) {
printf("* %d - %s, %s \n", courses[2].registered[i]->num.studentNum, courses[2].registered[i]->lastName, courses[2].registered[i]->firstName);
}
}
And here's my header file
structs.h
#ifndef STRUCTS_H_
#define STRUCTS_H_
char *firstName[] = {
"Emma", "Liam", "Olivia",
"Noah", "Ava", "Logan",
"Sophia", "Lucas", "Isabella",
"Mason", "Shaylyn", "Jack"
};
char *lastName[] = {
"Smith", "Johnson", "Williams",
"Brown", "Jones", "Miller",
"Davis", "Garcia", "Rodriguez",
"Wilson", "Seguin", "Loveday"
};
typedef struct{
int studentNum;
}studentNumber;
typedef struct{
char *firstName;
char *lastName;
studentNumber num;
}studentID;
studentID students[12];
char *courseName[] = {"Web Programming", "Technical Communication", "Processor Architecture"};
char *courseDescription[] = {"Learn the language of HTML, and how to create websites.", "Learn the essentials of communication skills, and how to apply them on the job.", "Learn the basics of circuits and Machine Language coding."};
typedef struct {
int maxRegister;
char *cCode;
char *cName;
char *cDescription;
studentID *registered[8];
studentID *waitlisted[12];
int studentRegistered;
int studentWaitlisted;
}course;
course courses[3];
#endif
Printing the values of the registered students works fine, but when I print the waitlisted students I get a segmentation error. I used gdb and found it was on this line but couldn't figure out why:
printf("%d - %s, %s \n", courses[0].waitlisted[i]->num.studentNum, courses[0].waitlisted[i]->lastName, courses[0].waitlisted[i]->firstName);
studentID *waitlisted[12]; That means they will hold the address of variable of type structure studentID. You simply didn't do this but start to access those pointer variables. You have passed them to scanf which invokes undefined behavior due to trying to read an unintialized pointer variable.
So what you need to do here -
courses[i].waitlisted = malloc(sizeof *courses[i].waitlisted * courses[i].studentWaitlisted);
Same goes for the studentID also. Here fistName is a char* you need to allocate some meory to access it otherwise it's UB.
Or
typedef struct{
char firstName[100]; //you are considering that name length would be 100
//at max.
char lastName[100];
studentNumber num;
}studentID;
After OP edited question:
The seg fault you get is because of accessing a local variable after it's lifetime ended.
courses[0].cCode = courseCode;
What you can do is
courses[0].cCode = strdup(courseCode);
Initialize array int numbers[999]={0}; otherwise you might access garbage value as the indexing is result of random number geneartion.
if( random < 999){
numbers[random] = numbers[numbersLeft-1];
numbersLeft--;
}
else{
fprintf(stderr, "%s\n","Error" );
}
Lots of repeated code. You should remove repeated code. And also there is no need for single-iteration-for-loop.

by using scanf stopped working

here is my code a part of a project when i initialize CHAR_1,CHAR_2 it works but when i read them by scanf it stopped working.
int repair_beta(char beta[11][11])
{
int i = 0, j = 0;
int count = 0, n = 0;
int fail = 0;
for(i = 1; i < 11; i++)
{
for(j = 1; j < 11; j++)
{
if(beta[i][j] == stronghold)
{
count++;
}
}
}
if(count == 4)
{
return 1;
}
else if(count < 4)
{
char CHAR_1 = 'A', CHAR_2 = '5';
for(n = 0; n < 3; n++)
{
printf("\n\t\t <<choose to repair>>\n\t\t\t ");
scanf("%c", &CHAR_1);
scanf("\n%c", &CHAR_2);
for(i = 1; i < 11; i ++)
{
if(CHAR_1 == 'A' + i - 1)
{
break;
}
}
for(j = 1; j < 11; j++)
{
if(CHAR_2 == '0' + j - 1)
{
break;
}
}
if(beta[i][j] == LAND || beta[i][j] == land)
{
beta[i][j] = stronghold;
prize_beta--;
printf("\n\t\t\t <<repaired>>");
printf("\n****************************************************************************\n");
return 1;
}
else
{
fail++;
}
}
if(fail == 3)
{
printf("\n\t\t <<you failed to repair>>");
prize_beta;
printf("\n****************************************************************************\n");
}
}
}
it is my firs time i use stackoverflow so excuse me if i ask my question in bad way.

C - Function to see if a game reached the "GameOver" point. (2048 copy)

I'm programing a 2048 game copy in C. But I can't figure out the game over function. I've this struct:
typedef struct struct_BLOCO
{
int valor;
int cor;
int x, y;
} BLOCO;
And this is the function I give the board its coordinates:
void GiveBlocksCoordinates(BLOCO bloco[16])
{
int i, j, cont = 0;
for (i = 0; i < MAX; i++)
{
for (j = 0; j < MAX; j++)
{
bloco[cont].x = (j * 8) + X_INI;
bloco[cont].y = (i * 4) + Y_INI;
cont++;
}
}
}
And just put them on screen using a function using the values bloco[i].[x]/[y].
This is my "GameOver" function, in its current state:
PS: The "quant" integer recivies the 16 value. If a block is empty, it has 0 value in it. If is "Game Over", the function return 1.
int acabouJogo(BLOCO vec[], int quant)
{
int i, j, x, y, cont = 0, BlocosOcupados = 1;
for (i = 0; i < quant; i++)
if (vec[i].valor != 0)
BlocosOcupados++;
if (BlocosOcupados == quant)
{
for (x = 0; x != 16; x = x + 4)
{
while (cont < 3)
{
if (vec[x + cont].valor != vec[(x + cont) + 1].valor)
cont++;
else
return 0;
}
cont = 0;
}
for (x = 0; x < 4; x++)
{
while ( (cont + x) != (12 + x) )
{
if (vec[x + cont].valor != vec[(x + cont) + 4].valor)
cont = cont + 4;
else
return 0;
}
cont = 0;
}
}
else
return 0;
return 1;
}
Can you guys help me?
Sorry for my english.
Thanks.

C algorithm to try out all the possible combinations of 12 knights in a chess board

I have been writing this program that tries to find how to put 12 knights on a chess board so all squares are either taken up by a knight or one of 12 knights can reach them in one move. So far I have created 2 functions: one takes a board with 12 knights and fills all the squares they can reach with 'T' and another that takes a filled chess board and checks if there are left any squares not taken up by a knight nor dominated by it (meaning no knight can reach it in one move).
Now I am dealing with the problem of how can I try out all the possible combinations of knights. My idea is that after every single combination of 12 knights on a board, I will send the board with 12 knights to be filled with 'T's for all the squares they can reach, and then send it to another function to check if all squares are dominated.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
int filling(char array[8][8])
{
int i, j;
for (i = 0;i < 8;i++)
{
for (j = 0; j < 8;j++)
{
if(array[i][j] == 'H')
{
if(i-1>-1 && j+2<8 && array[i-1][j+2]!='H') array[i-1][j+2]='T';
if(i+1<8 && j+2<8 && array[i+1][j+2]!='H') array[i+1][j+2]='T';
if(i-2>-1 && j+1<8 && array[i-2][j+1]!='H') array[i-2][j+1]='T';
if(i+2<8 && j+1<8 && array[i+2][j+1]!='H') array[i+2][j+1]='T';
if(i-2>-1 && j-1>-1 && array[i-2][j-1]!='H') array[i-2][j-1]='T';
if(i+2<8 && j-1>-1 && array[i+2][j-1]!='H') array[i+2][j-1]='T';
if(i-1>-1 && j-2>-1 && array[i-1][j-2]!='H') array[i-1][j-2]='T';
if(i+1<8 && j-2>-1 && array[i+1][j-2]!='H') array[i+1][j-2]='T';
}
}
}
}
int checking(char array[8][8])
{
int i, j;
for(i = 0;i < 8;i++)
{
for(j = 0;j < 8;j++)
{
if(array[i][j] != 'H' && array[i][j] != 'T')
return 0;
}
}
return 1;
}
int main()
{
int i, j;
char board[8][8];
for(i = 0; i < 8; i++)
{
for(j = 0; j < 8; j++)
board[i][j] = '0';
}
// The following lines are here to test if the checking and filling work
/*board[2][1] = 'H';
board[2][2] = 'H';
board[3][2] = 'H';
board[5][2] = 'H';
board[6][2] = 'H';
board[5][3] = 'H';
board[2][4] = 'H';
board[1][5] = 'H';
board[2][5] = 'H';
board[4][5] = 'H';
board[5][5] = 'H';
board[5][6] = 'H'; */
filling(board);
if(checking(board) == 1) printf (" \n Works");
else printf ("\n Doesnt work");
for(i = 0; i < 8; i++)
{
printf("\n");
for(j = 0; j < 8; j++)
printf("%c ", board[i][j]);
}
return 0;
}
What kind of algorithm I could use to try out every combo? Thank you for your answers.
You need these things:
A sorting rule that puts all possible combinations in a well-defined order.
An initialization rule that defines the board's first such combination.
An increment rule that transitions a board from its current combination to the next combination in the well-defined order.
A rule that detects when the board is in the last combination.
Then you just use algorithm 2 to put the board in the first state. Then check with algorithm 4 to see if you're in the last state. If not, use algorithm 3 to go to the next state.
Algorithm 1 is probably the hard one. One simple rule is just to convert the board's position to a binary number with a zero for an empty square and a one for a full square in a well-defined order, say starting from the top left and going across, then moving to the next row.
If you're on decent, or even semi-decent hardware, you've got a 64-bit unsigned integral type available to you. Let's call that uint64_t.
You can store a chessboard with knight positions as a single uint64_t. You can also store a "dominated" mask in the same type. You simply establish a correlation between the 64 bits in the type and the 64 spaces on the chessboard.
Since there are 64 possible locations, you can pre-compute the possible threat masks for each position, storing that as an array of 64 uint64_t values.
You need to set the position of each knight. But you can safely do them in a certain order, so that knight #1 is always the "highest" knight - his bit position is always highest, or lowest, or whatever. So you could write code like this:
for (k0 = 64; k0 > 11; --k0)
for (k1 = k0 - 1; k1 > 10; --k1)
for (k2 = k1 - 1; k2 > 9; --k2)
...
for (k11 = k10 - 1; k11 >= 0; --k11)
/* do stuff */
But that's horrible, because there are a squillion possibilities (squillion = 1573144097507348889600, thanks #jwd!).
That said, the threat mask for each knight can be computed "incrementally" from the masks of the outer knights, so it might be faster to perform all the computations than to try to store/mark/compute the rotations and flips of the board.
Something like this:
for (kXX = kYY - 1; kXX > (11-XX); --kXX) {
threat_XX = threat_YY | pre_computed_threat[kXX];
for (kZZ = KXX - 1; kZZ > (11-ZZ); --kZZ) {
threat_ZZ = threat_XX | pre_computed_threat[kZZ];
The nice thing about this approach is that your objective is total coverage by threats - all 1 bits in the threat_11 map, in other words. You can test just by inverting the bits and comparing with zero.
Well thanks to my questions of today I got myself blocked from asking more until my rep gets better, but for what its worth I managed to solve the problem myself. I had to take 3 positions in each quarter of the board that can only be reached by seperate horses, that way making my 12 for cycles shorter, as they only had to go through specific locations, instead of trying every single of of them. The execution times is just above one second and it finds two different layouts for 12 knights, marking them 'H' and the positions they can reach in one turn 'T'. Here is the code if anyone ever comes to a problem like this:
#include <stdio.h>
#include <stdlib.h>
struct horses
{
int x;
int y;
};
void fill(struct horses arkliai[12], char array[8][8])
{
int i,j;
for(i = 0; i < 8; i++)
{
for(j = 0; j < 8; j++)
array[i][j] = '0';
}
for(i = 0; i < 12; i++)
{
array[arkliai[i].x][arkliai[i].y] = 'H';
}
pildymas(array);
}
void startingpositions(struct horses arkliai[12])
{
int i = 0;
int j = 0;
int a = 0;
for(a = 0; a < 12; a++)
{
if(i > 7)
{
i = 0;
j++;
}
arkliai[a].x = i;
arkliai[a].y = j;
i++;
}
}
void combinacijos(struct horses h[12], char array[8][8])
{
int a,b,c,d,e,f,g,hi,ii,ji,k,l;
for(a = 0; a < 2; a++)
{
if(a == 0)
{
h[0].x = 1;
h[0].y = 2;
}
if(a == 1)
{
h[0].x = 2;
h[0].y = 1;
}
for(b = 0; b < 2; b++)
{
if(b == 0)
{
h[1].x = 5;
h[1].y = 1;
}
if(b == 1)
{
h[1].x = 6;
h[1].y = 2;
}
for(c = 0; c < 2; c++)
{
if(c == 0)
{
h[2].x = 1;
h[2].y = 5;
}
if(c == 1)
{
h[2].x = 2;
h[2].y = 6;
}
for(d = 0; d <2;d++)
{
if(d == 0)
{
h[3].x = 5;
h[3].y = 6;
}
if(d == 1)
{
h[3].x = 6;
h[3].y = 5;
}
for(e = 0; e < 3; e++)
{
if(e == 0)
{
h[4].x = 2;
h[4].y = 0;
}
if(e == 1)
{
h[4].x = 2;
h[4].y = 2;
}
if(e == 2)
{
h[4].x = 1;
h[4].y = 3;
}
for (f = 0; f < 3; f++)
{
if(f == 0)
{
h[5].x = 1;
h[5].y = 4;
}
if(f == 1)
{
h[5].x = 2;
h[5].y = 5;
}
if(f == 2)
{
h[5].x = 2;
h[5].y = 7;
}
for (g = 0; g < 3; g++)
{
if(g == 0)
{
h[6].x = 5;
h[6].y = 7;
}
if(g == 1)
{
h[6].x = 5;
h[6].y = 5;
}
if(g == 2)
{
h[6].x = 6;
h[6].y = 4;
}
for(hi = 0; hi < 3; hi++)
{
if(hi == 0)
{
h[7].x = 5;
h[7].y = 0;
}
if(hi == 1)
{
h[7].x = 5;
h[7].y = 2;
}
if(hi == 2)
{
h[7].x = 6;
h[7].y = 3;
}
for(ii = 0; ii < 4; ii++)
{
if (ii == 0)
{
h[8].x = 3;
h[8].y = 0;
}
if (ii == 1)
{
h[8].x = 3;
h[8].y = 2;
}
if (ii == 2)
{
h[8].x = 0;
h[8].y = 3;
}
if (ii == 3)
{
h[8].x = 2;
h[8].y = 3;
}
for(ji = 0; ji < 4; ji++)
{
if (ji == 0)
{
h[9].x = 3;
h[9].y = 7;
}
if (ji == 1)
{
h[9].x = 3;
h[9].y = 5;
}
if (ji == 2)
{
h[9].x = 2;
h[9].y = 4;
}
if (ji == 3)
{
h[9].x = 0;
h[9].y = 4;
}
for(k = 0; k < 4; k++)
{
if (k == 0)
{
h[10].x = 4;
h[10].y = 7;
}
if (k == 1)
{
h[10].x = 4;
h[10].y = 5;
}
if (k == 2)
{
h[10].x = 5;
h[10].y = 4;
}
if (k == 3)
{
h[10].x = 7;
h[10].y = 4;
}
for(l = 0;l < 64;l++)
{
if(h[11].x == 7)
{
if(h[11].y == 7)
{
h[11].x = 0;
h[11].y = 0;
break;
}
h[11].x = 0;
h[11].y = h[11].y +1;
}
else { h[11].x= h[11].x+1; }
fill(h, array);
}
}
}
}
}
}
}
}
}
}
}
}
}
int pildymas(char array[8][8])
{
int i, j;
for (i = 0;i < 8;i++)
{
for (j = 0; j < 8;j++)
{
if(array[i][j] == 'H')
{
if(i-1>-1 && j+2<8 && array[i-1][j+2]!='H') array[i-1][j+2]='T';
if(i+1<8 && j+2<8 && array[i+1][j+2]!='H') array[i+1][j+2]='T';
if(i-2>-1 && j+1<8 && array[i-2][j+1]!='H') array[i-2][j+1]='T';
if(i+2<8 && j+1<8 && array[i+2][j+1]!='H') array[i+2][j+1]='T';
if(i-2>-1 && j-1>-1 && array[i-2][j-1]!='H') array[i-2][j-1]='T';
if(i+2<8 && j-1>-1 && array[i+2][j-1]!='H') array[i+2][j-1]='T';
if(i-1>-1 && j-2>-1 && array[i-1][j-2]!='H') array[i-1][j-2]='T';
if(i+1<8 && j-2>-1 && array[i+1][j-2]!='H') array[i+1][j-2]='T';
}
}
}
tikrinimas(array);
}
int tikrinimas(char array[8][8])
{
int i, j;
for(i = 0;i < 8;i++)
{
for(j = 0;j < 8;j++)
{
if(array[i][j] != 'H' && array[i][j] != 'T')
return 0;
}
}
printas(array);
}
int printas(char array[8][8])
{
int i,j;
for(j = 0; j <8; j++)
{
printf("\n");
for(i = 0; i <8 ; i++)
printf("%c ", array[i][7-j]);
}
printf("\n");
}
int main()
{
struct horses hr[12];
char board[8][8];
startingpositions(hr);
combinacijos(hr, board);
return 0;
}

Error in a "for" loop, I think memory problems

I'm trying to create an algorithm that loops through the loop 'for' ten times, the ultimate goal is to print out a 50x50 matrix with the sum of the values ​​greater than or equal to 49, 0, and 1 represented as '*', but for some strange reason the loop only does 3 iterations instead of 10. ..
#include <stdio.h>
void printsimulaciodeu(int matlefa[50][50]) {
int ii, jj;
for (ii = 0; ii < 50; ii++) {
for (jj = 0; jj < 50; jj++) {
if(matlefa[ii][jj] == 0) {
printf(" 0 ");
}
else if (matlefa[ii][jj] >= 49) {
printf(" %d ", matlefa[ii][jj]-48);
}
else {
printf(" * ");
}
}
printf("\n");
}
}
int main(){
int tabla[50][50]={
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
};
int its;
int i, j;
int disparos_pos[10] = {1, 5, 14, 22, 37, 48, 43, 40, 28, 19};
char disparos_dir[10]= {'i','i','d','i','d','d','i','d','d','i'};
int mataux[50][50];
for (i = 0; i < 50; i++) {
for (j = 0; j < 50; j++) {
mataux[i][j] = tabla[i][j];
}
}
int matsuma[50][50];
for (its = 0; its < 10; its++) {
int pos = disparos_pos[its];
int dir = disparos_dir[its];
i = 0;
j = pos;
int cont = 49;
while (i < 49) {
if (mataux[i+1][j] == 0) {
mataux[i+1][j] = cont;
++i;
}
else {
if (dir == 'i') {
if (mataux[i+1][j] != 0) {
if (mataux[i][j-1] == 0) {
mataux[i][j-1] = cont;
--j;
}
else {
dir = 'd';
}
}
}
if (dir == 'd') {
if (mataux[i+1][j] != 0) {
if (mataux[i][j+1] == 0) {
mataux[i][j+1] = cont;
++j;
}
else {
dir = 'i';
}
}
}
}
}
if (its == 0) {
int m, n;
for (m = 0; m < 50; m++) {
for (n = 0; n < 50; n++) {
matsuma[m][n] = mataux[m][n];
}
}
}
if (its > 0) {
int p, q;
for (p = 0; p < 50; p++) {
for (q = 0; q < 50; q++) {
if (mataux[p][q] == 49) {
if (matsuma[p][q] >= 49) {
matsuma[p][q] = matsuma[p][q]+1;
}
}
}
}
}
}
printsimulaciodeu(matsuma);
}
I thought that one possibility is that the memory for the process is complete and therefore not iterate correctly, is that possible?
please help, I need it to work well for college work.
After a bit of debugging, I figured out the actual problem: Your while loop will run for INFINITE time in 3rd iteration of for loop.
Explaination
The while loop runs OK until i=33
at i=33 (j = 21, dir = 'i'), the none of the assignment statements (mataux[blah][blah] = blah balh) are executed, as conditons turn out to be false. Ultimate result is that value of 'dir' is toggled between 'i' and 'd', and the loop will run for forever!!
Details
mataux[i+1][j] = mataux[34][21] = 1 != 0 ---> first IF skipped
mataux[i][j-1] = mataux[33][20] = 49 != 0 ---> third IF in first ELSE of WHILE skipped
ELSE --> dir = 'd' (100)
mataux[i][j+1] = mataux[33][22] = 1 != 0 ---> third IF in last IF block skipped
ELSE --> dir = "i" (105)
Hope this helps!

Resources