Function and loop trouble in C - c
This program is expecting the user to input a 7-digit number (except 1 and 0), and any digit has a corresponding set of letters
(2=ABC, 3=DEF, 4=GHI, 5=JKL, 6=MNO, 7=PRS, 8=TUV, 9=XYZ, as found on phones in the USA). Finally, it should output all 2187 possible sequences of letters.
ex: input 2345678
output should be ADGJMPT ADGJMPU ADGJMPV ADGJMRT ADGJMRU ADGJMRV ADGJMST..........CFILOSV
but my output always is AAAAAAA AAAAAAB AAAAAAC..........CCCCCCC
(I also have trouble in checking number. I first set a loop and array, if (che[1] != 1 && che[0] != 1) break; but sometimes it doesn't break.)
Can you explain what's wrong?
#include<stdio.h>
int main(){
int che[50] = { 0 };
int a, b, c, d, e, f, g, i, r, q, number, check;
char word2[7];
char word1[8][3] = {
{ 'A', 'B', 'C' },
{ 'D', 'E', 'F' },
{ 'G', 'H', 'I' },
{ 'J', 'K', 'L' },
{ 'M', 'N', 'O' },
{ 'P', 'R', 'S' },
{ 'T', 'U', 'V' },
{ 'W', 'X', 'Y' } };
while (1)
{
printf("Enter seven digit(except 0 and 1):");
scanf("%d", &number);
check = number;
for (; number != 0; number /= 10)
{
q = number % 10;
che[q] = 1;
}
if (che[1] != 1 && che[0] != 1) break;
}
number = check;
for (i = 6; number == 0; i--)
{
r = number % 10;
if (r == 2){ word2[i] = 0; }
if (r == 3){ word2[i] = 1; }
if (r == 4){ word2[i] = 2; }
if (r == 5){ word2[i] = 3; }
if (r == 6){ word2[i] = 4; }
if (r == 7){ word2[i] = 5; }
if (r == 8){ word2[i] = 6; }
if (r == 9){ word2[i] = 7; }
number /= 10;
}
for (a = 0; a < 3; a++){
for (b = 0; b < 3; b++){
for (c = 0; c < 3; c++){
for (d = 0; d < 3; d++){
for (e = 0; e < 3; e++){
for (f = 0; f < 3; f++){
for (g = 0; g < 3; g++){
printf("%c%c%c%c%c%c%c ",word1[word2[0]][a], word1[word2[1]][b], word1[word2[2]][c], word1[word2[3]][d], word1[word2[4]][e], word1[word2[5]][f], word1[word2[6]][g]);
}
}
}
}
}
}
}
return 0;
}
Main problem is here:
for (i = 6; number == 0; i--)
The loop condition is the opposite of the one it should be. You want to keep iterating on the number until you reach 0 (by successively dividing it by 10).
It should be
for (i = 6; number != 0; i--)
or
for (i = 6; i >= 0; i--)
In addition mind that
if (r == 2){ word2[i] = 0; }
if (r == 3){ word2[i] = 1; }
if (r == 4){ word2[i] = 2; }
if (r == 5){ word2[i] = 3; }
if (r == 6){ word2[i] = 4; }
if (r == 7){ word2[i] = 5; }
if (r == 8){ word2[i] = 6; }
if (r == 9){ word2[i] = 7; }
is equivalent to
if (r >= 2 && r <= 9)
word2[i] = r - 2;
I think this works as you expected:
For some convenience in debugging, I changed some IO format:
Input separated number 2-9 divided by space and end with -1.
The input sequence can be of any length, just end with -1.
For example, input:2 3 4 2 -1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char word1[8][3] = {
{ 'A', 'B', 'C' },
{ 'D', 'E', 'F' },
{ 'G', 'H', 'I' },
{ 'J', 'K', 'L' },
{ 'M', 'N', 'O' },
{ 'P', 'R', 'S' },
{ 'T', 'U', 'V' },
{ 'W', 'X', 'Y' } };
void translate(char * headStr,int * pattern,int pos_to_do)
{
if(pattern[pos_to_do]<0)
{
printf("%s\n",headStr);
return;
}
char str[3][20];
int i;
for(i=0;i<3;i++)
{
strcpy(str[i],headStr);
char str_this[2];
str_this[0]=word1[ pattern[pos_to_do] ][i];
str_this[1]='\0';
strcat(str[i],str_this);
translate(str[i],pattern,pos_to_do+1);
}
return;
}
int main(){
int che[20];
int number, check,len;
while (1)
{
printf("Enter digits(except 0 and 1):");
len=0;
scanf(" %d", &number);
while(number>=2)
{
che[len]=number-2;
len++;
scanf("%d", &number);
}
che[len]=-1;
char str_start[]="";
translate(str_start,che,0);
}
return 0;
}
Test output:
Enter digits(except 0 and 1):2 3 4 -1
ADG
ADH
ADI
AEG
AEH
AEI
AFG
AFH
AFI
BDG
BDH
BDI
BEG
BEH
BEI
BFG
BFH
BFI
CDG
CDH
CDI
CEG
CEH
CEI
CFG
CFH
CFI
Enter digits(except 0 and 1):
Related
Tic Tac Toe game
I was working on learning c code and was making a tic-tac-toe game. The Boolean issue was fixed. Now the issue is that it is looping the printf("There is no empty space!"); and prinf("Invalid !!!"); after it take the player1 name. I also wanted to know if the line where I printed the array with the grid is correct or not. #include <stdio.h> #include <stdlib.h> #include <strings.h> char space[3][3] = { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}, }; int row; int column; char token = 'x'; bool tie = false; char n1[256]; char n2[256]; void functionboard() { char space[3][3] = { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}, }; printf(" | | \n"); printf(" ", space[0][0], "| ", space[0][1], "| ", space[0][2], " \n"); printf("______|________|_____\n"); printf(" | | \n"); printf(" ", space[1][0], " | ", space[1][1], " | ", space[1][2], " \n"); printf("______|________|_____\n"); printf(" | | \n"); printf(" ", space[2][0], " | ", space[2][1], " | ", space[2][2], " \n"); printf(" | | \n"); } void functionOne() { int dight; if (token == 'x') { printf(n1, "please enter"); scanf("&d", &dight); } if (token == '0') { printf(n2, "please enter"); scanf("&d", &dight); } if (dight == 1) { row = 0; column = 0; } if (dight == 2) { row = 0; column = 1; } if (dight == 3) { row = 0; column = 2; } if (dight == 4) { row = 1; column = 0; } if (dight == 5) { row = 1; column = 1; } if (dight == 6) { row = 1; column = 2; } if (dight == 7) { row = 2; column = 0; } if (dight == 8) { row = 2; column = 1; } if (dight == 9) { row = 2; column = 2; } else if (dight < 1 || dight > 9) { prinf("Invalid !!!"); } if (token == 'x' && space[row][column] != 'x' && space[row][column] != '0') { space[row][column] = 'x'; token = '0'; } else if (token == '0' && space[row][column] != 'x' && space[row][column] != '0') { space[row][column] = '0'; token = 'x'; } else { printf("There is no empty space!"); functionboard(); } functionOne(); } bool functionDraw() { for (int i = 0; i < 3; i++) { if (space[i][0] == space[i][1] && space[i][0] == space[i][2] || space[0][i] == space[1][i] && space[0][i] == space[2][i]) return true; } if (space[0][0] == space[1][1] && space[1][1] == space[2][2] || space[0][2] == space[1][1] && space[1][1] == space[2][0]) { return true; } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (space[i][j] != 'x' && space[i][j] != '0') { return false; } } } tie = true; return false; } int main() { printf("Enter the name of the first player : \n"); scanf("%c", n1); printf("Enter the name of the second player : \n"); scanf("%c", n2); printf("%c is player1 so he/she will play first \n", n1); printf("%c is player2 so he/she will play first \n", n2); while (!functionDraw()) { functionboard(); functionOne(); functionDraw(); } if (token == 'x' && tie == false) { printf("%c Wins!!\n", n2); } else if (token == '0' && tie == false) { printf("%c Wins!!\n", n1); } else { printf("its a draw!!"); } }
If the error you're getting is about the type bool and not the variable itself, see this question: you need to also include <stdbool.h> to be able to use the bool type.
There's no built-in bool type in classic C. You can use it though by using #include <stdbool.h>. The other solution is replacing it with an int since it can be used like a bool
Movement for game character doesn't work at all
Here is the .c code from start to finish. I suspect it's an order of operations issue or something else. The code is supposed to have a title screen where you can exit or play. If you hit play it renders a new char array in which you can move either left, right, or down (a/d/s). You get points for going down a row each time as well as going over 'P' spots on the array. Once you hit y-coordinate 202 on the array as well as have positive points, you win and it renders a congrats char array. If you go negative in points (by hitting the '#' paces or the 'L' wall that goes down a row every 1/2 second) then you will lose and be told you lost in a new char array as well as be asked if you want to exit or play again. And as mentioned before, the movement for the 'L's and the play character 'O' simply doesn't move. #include <stdlib.h> #include <stdio.h> #include <time.h> #include <fcntl.h> #include <unistd.h> char quickchar(float delay); void render(); void titleRender(); void renderLoss(); void renderWin(); int score(); char titlescreen[20][59] = { }; char diffdiff[20][46] = { (Grid removed due to char limit) }; char gameover[20][50] = { (Grid removed due to char limit) }; char gameboard[203][26] = { (Grid removed due to char limit) }; int playerx; int playery; int pts; void entryRender(); void gameRender(); int main() { int input; char replay; int lx; int ly; char walkin; int gaming; char titlechoice; int diff; int running; lx = 0; ly = 0; running = 1; playerx = 13; playery = 5; pts = 25; gaming = 0; while (gaming == 0) { titleRender(); printf("\033[1;32m"); printf("\nPress 'p' to play or 'x' to exit!"); printf("\033[0m"); scanf(" %c", &titlechoice); if (titlechoice == 'p') { gaming += 4; } if (titlechoice == 'x') { return 0; } } while (gaming == 4) { system("stty -echo raw"); render(); printf("\033[1;35m"); printf("Choose a direction: s/Down, a/Left, d/Right.\n\r"); printf("\033[0m"); walkin = quickchar(0.5); scanf("%c", &walkin); //obj behaviour if (walkin == 's' && gameboard[playery +1][playerx] != '#' && gameboard[playery + 1][playerx] != '+' && gameboard[playery + 1][playerx] != '|' && gameboard[playery + 1][playerx] != '-') { playery++; pts += 5; if (gameboard[playery + 1][playerx] == '#') { pts -= 25; } if (gameboard[playery + 1][playerx] == 'P') { printf("Points increased!\n\r"); pts += 50; gameboard[playery + 1][playerx] = '_'; } } else if (walkin == 'a' && gameboard[playery][playerx - 1] != '#' && gameboard[playery][playerx - 1] != '+' && gameboard[playery][playerx - 1] != '|' && gameboard[playery][playerx - 1] != '-') { playerx--; if (gameboard[playery][playerx - 1] == '#') { pts -= 25; } if (gameboard[playery][playerx - 1] == 'P') { printf("Points increased!\n\r"); pts += 50; gameboard[playery][playerx - 1] = '_'; } } else if (walkin == 'd' && gameboard[playery][playerx + 1] != '#' && gameboard[playery][playerx + 1] != '+' && gameboard[playery][playerx + 1] != '|' && gameboard[playery][playerx + 1] != '-') { playerx++; if (gameboard[playery][playerx + 1] == '#') { pts -= 25; } if (gameboard[playery + 1][playerx] == 'P) { printf("Points increased!\n\r"); pts += 50; gameboard[playery][playerx + 1] = '_'; } } if (walkin == 'a' || walkin == 's' || walkin == 'd' || walkin != 'a' || walkin != ' ' || walkin != 'd' ) { for (ly = 0; ly = 202; ly++) { for (lx = 1; lx = 25; lx++) { gameboard[ly][lx] = 'L'; } } } if (pts <= 0) { system("clear"); renderLoss(); scanf("%c", replay); if (replay == 'Y') { gaming = 3; } if (replay == 'N') { gaming = 5; } } if (playery == 202) { renderWin(); printf(""); printf("Congrats! You got a score of %d !", pts); printf(""); scanf("%c", &replay); if (replay == 'Y') { gaming = 4; } if (replay == 'N') { gaming = 5; } } //player postion == 203y, then congrta screen //ask player to set gaming to 4(replay) or none (exit program) } system("stty echo -raw"); return 0; } void render() { int y,x,k; system("clear"); printf("\033[01;33m"); printf("||POINTS: %d||\r\n", pts); printf("\033[0m"); for (y = 0; y < 203; y++) { for (x = 0; x < 26; x++) { if (y == playery && x == playerx) { printf("\033[0;32m"); printf("O"); printf("\033[0m"); } else { printf("\033[1;36m"); printf("%c", gameboard[y][x]); printf("\033[0m"); } } printf("\r\n"); } } void titleRender() { int y, x; system("clear"); for (y = 0; y < 20; y++) { for (x = 0; x < 59; x++) { printf("\033[0;32m"); printf(" %c", titlescreen[y][x]); printf("\033[0m"); } printf("\r\n"); } } void renderLoss() { int y, x; system("clear"); for (y = 0; y < 26; y++) { for (x = 0; x < 50; x++) { printf("\033[0;31m"); printf(" %c", gameover[y][x]); printf("\033[0m"); } printf("\r\n"); } } void renderWin() { int y, x; system("clear"); for (y = 0; y < 20; y++) { for (x = 0; x < 46; x++) { printf("\033[0;33m"); printf(" %c", diffdiff); printf("\033[0m"); } printf("\r\n"); } } char quickchar(float delay) { int flags; char c; usleep((int)(delay*1000000)); flags = fcntl(0, F_GETFL, 0); fcntl(0, F_SETFL, flags | O_NONBLOCK); c = getchar(); fcntl(0, F_SETFL, flags ^ O_NONBLOCK); return c; }
Tic Tac Toe AI error
char checkwinner(char ttt[][3]) { char winner = 0; for (int p = 0; winner == 0 && p < 2; p++) { char XO = p == 0 ? 'x' : 'o'; for (int i = 0; winner == 0 && i < 3; i++) { if (ttt[i][0] == XO && ttt[i][1] == XO && ttt[i][2] == XO) winner = XO; else if (ttt[0][i] == XO && ttt[1][i] == XO && ttt[2][i] == XO) winner = XO; } if (winner == 0 && ttt[0][0] == XO && ttt[1][1] == XO && ttt[2][2] == XO) winner = XO; else if (winner == 0 && ttt[0][2] == XO && ttt[1][1] == XO && ttt[2][0] == XO) winner = XO; } return winner; } int main (void) { char b[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}}; int turn = 0, i, j, loc, type, counter = 0; char XO, AL; char theChar = 'A'; char junk ='A'; printf("Type in 1 to play with AL, or 2 to play with another human\n"); scanf("%d", &type); while (turn <= 9) { char winner = checkwinner(b); if (winner == 'x' || winner == 'o') { printf("%c has won! You get 100 million DOGECOINS! Just enter your debt card information to our website, as well as your checking account and social security number. Go to www.THIS_IS_NOT_A_SCAM.com to get your dogecoins today!\n", winner); return (0); } else if (((b[0][0] == 'x') || (b[0][0] == 'o')) && ((b[0][1] == 'x') || (b[0][1] == 'o')) && ((b[0][2] == 'x') || (b[0][2] == 'o')) && ((b[1][0] == 'x') || (b[1][0] == 'o')) && ((b[1][1] == 'x') || (b[1][1] == 'o')) && ((b[1][2] == 'x') || (b[1][2] == 'o')) && ((b[2][0] == 'x') || (b[2][0] == 'o')) && ((b[2][1] == 'x') || (b[2][1] == 'o')) && ((b[2][2] == 'x') || (b[2][2] == 'o'))) { printf("This is a tie.\n"); return (0); } //<---- missing brace! if (type == 1) { counter = counter + 1; if (counter == 1) { XO = 'x'; } if (counter == 2) { XO = 'o'; } if (counter == 3) { XO = 'x'; } if (counter == 4) { XO = 'o'; } if (counter == 5) { XO = 'x'; } if (counter == 6) { XO = 'o'; } if (counter == 7) { XO = 'x'; } if (counter == 8) { XO = 'o'; } if (counter == 9) { XO = 'x'; } } if (type == 2) { XO = 'x'; AL = 'o'; for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { b[i][j] = AL; printArray(b); } } } theChar = getchar(); putchar(theChar); printf("Enter number: "); scanf("%d", &loc); j = (loc - 1) % 3; i = (loc - 1) / 3; if ((b[i][j] == 'x') || (b[i][j] == 'o')) { printf("That spot has been taken!\n"); return (0); } putchar('\n'); junk = getchar(); b[i][j] = XO; printArray(b); } } void printArray(char thearray[3][3]) { int i, j; for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf("%c ", thearray[i][j]); } printf("\n"); } } Right now I'm trying to finish the AI mode. Rather than just going and finding the first empty space and printing a single 'o' my game prints 9 boards like this: o 2 3 4 5 6 7 8 9 o o 3 4 5 6 7 8 9 o o o 4 5 6 7 8 9 o o o o 5 6 7 8 9 o o o o o 6 7 8 9 o o o o o o 7 8 9 o o o o o o o 8 9 o o o o o o o o 9 o o o o o o o o o How do I get the game to stop after printing one 'o', and then letting 'x' pick a move? All help is appreciated.
Your code to find the place for AL to play is incorrect: if (type == 2) { XO = 'x'; AL = 'o'; for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { b[i][j] = AL; printArray(b); } } } You just play every spot on the grid and print it! Try this instead: if (type == 2) { XO = 'x'; AL = 'o'; for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { if (b[i][j] != 'x' && b[i][j] != 'o') { b[i][j] = AL; i = j = 2; /* ugly way to exit the nested loops */ } } } printArray(b); } But this gaming strategy will be easy to beat! Also you forget to increment turn, but if you did, you could replace the tedious test for a tie with a simple if (turn == 9). EDIT: exiting from this nested loop requires more logic... it is better to move the AL game play to a separate function. Furthermore, your logic is flawed: you let AL play if type is 2 instead of 1... Here is a simplified and corrected version that can play both sides: #include <stdio.h> char checkwinner(char b[3][3]) { for (int i = 0; i < 3; i++) { if (b[i][0] == b[i][1] && b[i][1] == b[i][2]) return b[i][0]; if (b[0][i] == b[1][i] && b[1][i] == b[2][i]) return b[0][i]; } if ((b[0][0] == b[1][1] && b[1][1] == b[2][2]) || (b[0][2] == b[1][1] && b[1][1] == b[2][0])) { return b[1][1]; } return 0; } void printArray(char b[3][3]) { for (int i = 0; i < 3; i++) { printf("%c %c %c\n", b[i][0], b[i][1], b[i][2]); } printf("\n"); } void playAl(char b[3][3], char AL) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (b[i][j] != 'x' && b[i][j] != 'o') { b[i][j] = AL; return; } } } } int main (void) { char board[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}}; int turn, i, j, loc, type; printf("What does AL play? (0:none, 1:x, 2:o, 3:both) "); scanf("%d", &type); for (turn = 0;; turn++) { printArray(board); char winner = checkwinner(board); if (winner == 'x' || winner == 'o') { printf("%c has won! Game over\n", winner); return 0; } if (turn == 9) { printf("This is a tie.\n"); return 0; } char XO = "xo"[turn % 2]; if (type & (1 << (turn % 2))) { /* AL's turn to play */ playAl(board, XO); continue; } for (;;) { printf("Enter number: "); if (scanf("%d", &loc) != 1) return 1; j = (loc - 1) % 3; i = (loc - 1) / 3 % 3; if (board[i][j] != 'x' && board[i][j] != 'o') break; printf("This spot is already taken!, try again\n"); } board[i][j] = XO; } }
Certain statements printing twice
In a hangman program I'm writing, words with two of the same letter, such as "eel" or "bee," upon the user entering "e" the first time and then trying to enter "e" for the second prompt, will display "You've already guessed this letter" twice. How can I fix this? #include <stdlib.h> #include <stdio.h> #include <stdbool.h> int main(){ char w[13][3] = { { 'c', 'a', 't' }, //0 { 'd', 'o', 'g' }, //1 { 'r', 'a', 't' }, //2 { 'e', 'e', 'l' }, //3 { 'c', 'o', 'w' }, //4 { 'o', 'w', 'l' }, //5 { 'e', 'm', 'u' }, //6 { 'b', 'a', 't' }, //7 { 'e', 'l', 'k' }, //8 { 'p', 'i', 'g' }, //9 { 'b', 'e', 'e' }, //10 { 'h', 'e', 'n' }, //11 { 'f', 'o', 'x' }, //12 }; char u, newline, dis[16]; int random, guesses = 3, finish = 0; _Bool successfulGuess = false; srand(time(NULL)); random = rand() % 13; printf("Animal %d\n", random); //check random number printf("---------\n\n"); printf("Enter a letter: "); u = getchar(); newline = getchar(); for (int i = 0; i < 3; i++){ if (w[random][i] == u){ successfulGuess = true; dis[i] = u; } else { dis[i] = '_'; } } for (int j = 0; j < 3; j++){ dis[j] = dis[j]; } printf("\n"); for (int i = 0; i < 3; i++){ printf("%c", dis[i]); } if (successfulGuess == false){ --guesses; } printf("\n\nGuesses left: %d", guesses); printf("\n\n"); while (guesses > 0){ finish = 0; successfulGuess = false; printf("Enter a letter: "); u = getchar(); newline = getchar(); for (int i = 0; i < 3; i++){ if (u == dis[i]){ successfulGuess = true; printf("\nYou already guessed this letter.\n"); printf("\ninput = dis[i]\nGuesses left: %d\n\n", guesses); } else if (w[random][i] == u){ successfulGuess = true; dis[i] = u; printf("\ninput = array char\nGuesses left: %d\n\n", guesses); } } for (int i = 0; i < 3; i++){ printf("%c", dis[i]); } if (successfulGuess == false){ guesses--; printf("\n\nbool statement\nGuesses left: %d\n\n", guesses); } if (guesses == 0){ printf("Sorry, you've run out of guesses."); } for (int i = 0; i < 3; i++) { if (dis[i] != '_') { finish++; } if (finish == 3){ printf("\n\nYou guessed the word!"); guesses = 0; } else{ continue; } } printf("\n\n"); } system("pause"); }
You are looping i three times even if you have already shown the message. I think it should break ather the message: for (int i = 0; i < 3; i++){ if (u == dis[i]){ successfulGuess = true; printf("\nYou already guessed this letter.\n"); printf("\ninput = dis[i]\nGuesses left: %d\n\n", guesses); /* BREAK HERE */ break; }
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,0,1,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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,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!