I'm trying to make when I put some numbers in scan, and if there's mine(*), print boom and if there's no mine, print the number of mines near to it. I can't find a problem with the code, but there's an error. Please verify it if you find the problem.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 10
int main (void)
{
char minefield [N][N];
int i, j;
int k;
int x, y;
int count;
int mine_number;
count = 0;
mine_number = N*N/10;
srand((long)time(NULL));
for (k=1; 0< k < mine_number; k=k+1) {
i = rand() % N;
j = rand() % N;
minefield [i][j] = '*';
}
for (i=0; i < N; i=i+1) {
for (j=0; j < N; j=j+1) {
count = 0;
if (minefield[i][j] != '*') {
if (i == 0) {
if (j == 0) {
if (minefield [i][j+1] == '*') {
count = count + 1;
}
if (minefield [i+1][j+1] == '*') {
count = count + 1;
}
if (minefield [i+1][j] == '*') {
count = count + 1;
}
}
else if (j == N-1) {
if (minefield [i+1][j] == '*') {
count = count + 1;
}
if (minefield [i+1][j-1] == '*') {
count = count + 1;
}
if (minefield [i][j-1] == '*') {
count = count + 1;
}
}
else {
if (minefield [i][j+1] == '*') {
count = count + 1;
}
if (minefield [i+1][j+1] == '*') {
count = count + 1;
}
if (minefield [i+1][j] == '*') {
count = count + 1;
}
if (minefield [i+1][j-1] == '*') {
count = count + 1;
}
if (minefield [i][j-1] == '*') {
count = count + 1;
}
}
}
else if (i == N-1) {
if (j == 0) {
if (minefield [i-1][j] == '*') {
count = count + 1;
}
if (minefield [i-1][j+1] == '*') {
count = count + 1;
}
if (minefield [i][j+1] == '*') {
count = count + 1;
}
}
else if (j == N-1) {
if (minefield [i-1][j] == '*') {
count = count + 1;
}
if (minefield [i-1][j-1] == '*') {
count = count + 1;
}
if (minefield [i][j-1] == '*') {
count = count + 1;
}
}
else {
if (minefield [i][j+1] == '*') {
count = count + 1;
}
if (minefield [i-1][j+1] == '*') {
count = count + 1;
}
if (minefield [i-1][j] == '*') {
count = count + 1;
}
if (minefield [i-1][j-1] == '*') {
count = count + 1;
}
if (minefield [i][j-1] == '*') {
count = count + 1;
}
}
}
else {
if (j == 0) {
if (minefield [i-1][j] == '*') {
count = count + 1;
}
if (minefield [i-1][j+1] == '*') {
count = count + 1;
}
if (minefield [i][j+1] == '*') {
count = count + 1;
}
if (minefield [i+1][j+1] == '*') {
count = count + 1;
}
if (minefield [i+1][j] == '*') {
count = count + 1;
}
}
else if (j == N-1) {
if (minefield [i-1][j] == '*') {
count = count + 1;
}
if (minefield [i-1][j-1] == '*') {
count = count + 1;
}
if (minefield [i][j-1] == '*') {
count = count + 1;
}
if (minefield [i+1][j-1] == '*') {
count = count + 1;
}
if (minefield [i+1][j] == '*') {
count = count + 1;
}
}
else {
if (minefield [i-1][j-1] == '*') {
count = count + 1;
}
if (minefield [i-1][j] == '*') {
count = count + 1;
}
if (minefield [i-1][j+1] == '*') {
count = count + 1;
}
if (minefield [i][j+1] == '*') {
count = count + 1;
}
if (minefield [i][j-1] == '*') {
count = count + 1;
}
if (minefield [i+1][j-1] == '*') {
count = count + 1;
}
if (minefield [i+1][j] == '*') {
count = count + 1;
}
if (minefield [i+1][j+1] == '*') {
count = count + 1;
}
}
}
}
}
}
scanf("%d %d", &x, &y);
if (minefield[x][y] = '*') {
printf("boom");
}
else {
printf("%d", count);
}
return 0;
}
Your basic problem is that you are counting before you read the input, i.e. before you know what x and y is. In other words, currently you are counting "something" on the whole minefield which isn't what you want.
So the first thing to do is to read x and y before counting.
Further it seems to me that your big if statement is hard to read. You could reorganize it like:
if (scanf("%d %d", &x, &y) != 2 || x < 0 || x >= N || y < 0 || y >= N)
{
// Illegal input
exit(1);
}
if (minefield[x][y] == '*') {
printf("boom");
}
else {
count = 0;
if (x-1 >= 0 && y-1 >= 0) count += (minefield[x-1][y-1] == '*');
if (x-1 >= 0) count += (minefield[x-1][y] == '*');
....
.... Add code to cover all 8 combinations (i.e. add the 5 missing combinations)
....
if (x+1 < N && y+1 < N) count += (minefield[x+1][y+1] == '*');
printf("%d", count);
}
Related
I'm getting the wrong output. The black screen is my output and the white screen is the right output. In my output the "V" is always in the right placement but the symbols under that line is always in the wrong placement.
: (hyphen) represents undiscovered locations
! : represents bombs
$ : represents treasure
& : represents both a bomb and treasure
. : represents a visited location that had neither a bomb nor a treasure
Check for BOTH a bomb AND a treasure
Check both the bomb and treasure member arrays to see if a value of 1 is set for both at the same
location
Update the player’s counters accordingly (bomb: reduce lives, treasure: increase treasure counter)
Display an appropriate message (use symbol: [&] to denote a bomb AND treasure, the treasure is
considered a “life insurance payout”)
If there is no bomb or treasure at the location entered by the user
Display an appropriate message (use symbol: [.] to denote nothing found)
This is my code:
do
{
printf(" ");
for (lives = 1; lives < (playerData.currentMove + 1); lives++)
{
if (lives != playerData.currentMove)
{
printf(" ");
}
else if (playerData.currentMove == 0)
{
printf(" ");
}
else
{
printf("%c\n", playerData.playerSym);
}
}
if (gameSettings.bombInput[playerData.currentMove] == 1 && gameSettings.treasureInput[playerData.currentMove] == 0)
{
gameSettings.revealedPosition[playerData.currentMove] = '!';
}
else if (gameSettings.treasureInput[playerData.currentMove] == 1 && gameSettings.bombInput[playerData.currentMove] == 0)
{
gameSettings.revealedPosition[playerData.currentMove] = '$';
}
else if (gameSettings.bombInput[playerData.currentMove] == 1 && gameSettings.treasureInput[playerData.currentMove] == 1)
{
gameSettings.revealedPosition[playerData.currentMove] = '&';
}
else if (gameSettings.bombInput[playerData.currentMove] == 0 && gameSettings.treasureInput[playerData.currentMove] == 0)
{
gameSettings.revealedPosition[playerData.currentMove] = '.';
}
if (playerData.currentMove == 0)
{
for (lives = 0; lives < (gameSettings.gameLength); lives++)
{
printf("%c", hiddenPosition);
gameSettings.revealedPosition[lives] = hiddenPosition;
}
}
else {
printf(" ");
for (lives = 0; lives < (gameSettings.gameLength); lives++)
{
printf("%c", gameSettings.revealedPosition[lives]);
}
}
printf("\n ");
for (lives = 1, z = 1; lives <= gameSettings.gameLength; lives++)
{
if (lives % 10 == 0)
{
printf("%d", z);
z++;
}
else {
printf("|");
}
}
printf("\n ");
for (lives = 1, z = 1; lives <= gameSettings.gameLength; lives++)
{
if (lives % 10 == 0)
{
printf("0");
z = 1;
}
else
{
printf("%d", z);
z++;
}
}
printf("\n");
printf("+---------------------------------------------------+\n");
printf(" Lives: %2d | Treasures: %2d | Moves Remaining: %2d\n", playerData.playerLives, playerData.treasureNum, gameSettings.gameMoves);
printf("+---------------------------------------------------+\n");
do
{
lives = 0;
printf("Next Move [1-%d]: ", gameSettings.gameLength);
scanf("%d", &playerData.currentMove);
if (playerData.currentMove >= 1 && playerData.currentMove <= gameSettings.gameLength)
{
printf("\n");
lives++;
}
else
{
printf(" Out of Range!!!\n");
}
} while (lives < 1);
if (gameSettings.revealedPosition[playerData.currentMove] == hiddenPosition)
{
if (gameSettings.bombInput[playerData.currentMove] == 0 && gameSettings.treasureInput[playerData.currentMove] == 0)
{
printf("===============> [.] ...Nothing found here... [.]\n\n");
gameSettings.gameMoves -= 1;
}
else if (gameSettings.bombInput[playerData.currentMove] == 1 && gameSettings.treasureInput[playerData.currentMove] == 0)
{
printf("===============> [!] !!! BOOOOOM !!! [!]\n\n");
playerData.playerLives -= 1;
gameSettings.gameMoves -= 1;
}
else if (gameSettings.treasureInput[playerData.currentMove] == 1 && gameSettings.bombInput[playerData.currentMove] == 0)
{
printf("===============> [$] $$$ Found Treasure! $$$ [$]\n\n");
playerData.treasureNum += 1;
gameSettings.gameMoves -= 1;
}
else if (gameSettings.bombInput[playerData.currentMove] == 1 && gameSettings.treasureInput[playerData.currentMove] == 1)
{
printf("===============> [&] !!! BOOOOOM !!! [&]\n");
printf("===============> [&] $$$ Life Insurance Payout!!! [&]\n\n");
gameSettings.gameMoves -= 1;
playerData.treasureNum += 1;
playerData.playerLives -= 1;
}
}
else
{
printf("===============> Dope! You've been here before!\n\n");
}
} while (playerData.playerLives != 0 && gameSettings.gameMoves != 0);
printf("No more LIVES remaining!\n\n");
printf(" ");
for (lives = 1; lives < (playerData.currentMove + 1); lives++)
{
if (lives != playerData.currentMove)
{
printf(" ");
}
else if (playerData.currentMove == 0)
{
printf(" ");
}
else
{
printf("%c\n", playerData.playerSym);
}
}
if (gameSettings.bombInput[playerData.currentMove] == 1 && gameSettings.treasureInput[playerData.currentMove] == 0)
{
gameSettings.revealedPosition[playerData.currentMove] = '!';
}
else if (gameSettings.treasureInput[playerData.currentMove] == 1 && gameSettings.bombInput[playerData.currentMove] == 0)
{
gameSettings.revealedPosition[playerData.currentMove] = '$';
}
else if (gameSettings.bombInput[playerData.currentMove] == 1 && gameSettings.treasureInput[playerData.currentMove] == 1)
{
gameSettings.revealedPosition[playerData.currentMove] = '&';
}
else if (gameSettings.bombInput[playerData.currentMove] == 0 && gameSettings.treasureInput[playerData.currentMove] == 0)
{
gameSettings.revealedPosition[playerData.currentMove] = '.';
}
if (playerData.currentMove == 0)
{
for (lives = 1; lives < (gameSettings.gameLength + 1); lives++)
{
printf("%c", hiddenPosition);
gameSettings.revealedPosition[lives] = hiddenPosition;
}
}
else {
printf(" ");
for (lives = 1; lives < (gameSettings.gameLength + 1); lives++)
{
printf("%c", gameSettings.revealedPosition[lives]);
}
}
printf("\n ");
for (lives = 1, z = 1; lives <= gameSettings.gameLength; lives++)
{
if (lives % 10 == 0)
{
printf("%d", z);
z++;
}
else {
printf("|");
}
}
printf("\n ");
for (lives = 1, z = 1; lives <= gameSettings.gameLength; lives++)
{
if (lives % 10 == 0)
{
printf("0");
z = 1;
}
else {
printf("%d", z);
z++;
}
}
printf("\n");
printf("+---------------------------------------------------+\n");
printf(" Lives: %2d | Treasures: %2d | Moves Remaining: %2d\n", playerData.playerLives, playerData.treasureNum, gameSettings.gameMoves);
printf("+---------------------------------------------------+\n\n");
printf("##################\n# Game over! #\n##################\n\n");
printf("You should play again and try to beat your score!!\n");
return 0;
I just started learning C (I'm an absolute beginner) and I'm trying to make a program that translates Roman numbers to Arabic and vice versa.
If I were to type "IX" my program should give me a "9" as an output but instead I get a "1". I tried to find the issue on my own using the debugger and I can see my program entering the first If-Statement
if (userString[localIndex] == 'I')
but then it skips the inner If-Statement
else if (userString[++localIndex] == 'X') {
ARABIC_NUM += 9;
localIndex++;
}
I'm not sure why this is happening. If I type "IV" my program outputs a "4" which is the correct answer but if I type "IVIV" my programs once again outputs a lonely "4" and ignores the rest of my input.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#define NOT_A_NUMBER 0
#define IS_ROMAN 1
#define IS_ARABIC 2
int ARABIC_NUM = 0;
int findStringLength(char* userString) {
int stringLength = 0;
size_t index = 0;
while (userString[index] != '\0')
{
if (userString[index] != '\0') {
stringLength++;
index++;
}
}
return stringLength;
}
void resetString(char* userString)
{
size_t stringLength = findStringLength(userString);
for (size_t index = 0; index < stringLength; index++)
{
userString[index] = '\0';
}
}
void printString(char* userString)
{
size_t stringLength = findStringLength(userString);
for (size_t index = 0; index < stringLength; index++)
{
if (userString[index] != '\0')
printf("~%zu:%c~ ", index, userString[index]);
else
printf("Null character");
}
}
bool ifEnd(char* numberInput, size_t counter) {
bool userEnd = false;
for (size_t index = 0; index < counter; index++)
{
if ((numberInput[index - 2] == 'E' && numberInput[index - 1] == 'N' && numberInput[index] == 'D')) {
userEnd = true;
}
}
return userEnd;
}
int isNumTrue(char userChar) {
int isRoman = NOT_A_NUMBER;
int isArabic = NOT_A_NUMBER;
if (userChar == 'I' || userChar == 'V' || userChar == 'X' ||
userChar == 'L' || userChar == 'C' || userChar == 'D' || userChar == 'M') {
isRoman = IS_ROMAN;
return isRoman;
}
else if (userChar == '0' || userChar == '1' || userChar == '2' || userChar == '3' || userChar == '4' || userChar == '5' || userChar == '6' || userChar == '7' || userChar == '8' || userChar == '9') {
isArabic = IS_ARABIC;
return isArabic;
}
else {
return NOT_A_NUMBER;
}
}
void convertToArabic(char* userString, size_t counter) {
for (size_t localIndex = 0; userString[localIndex] != '\0'; localIndex++) {
// printf("[ %c%s]", userString[localIndex], "-o ");
if (userString[localIndex] == 'I') {
if (userString[++localIndex] == 'V') {
printf("Made it in");
ARABIC_NUM += 4;
localIndex++;
}
else if (userString[++localIndex] == 'X') {
ARABIC_NUM += 9;
localIndex++;
}
else {
ARABIC_NUM += 1;
}
}
else if (userString[localIndex] == 'V') {
ARABIC_NUM += 5;
}
else if (userString[localIndex] == 'X') {
if (userString[localIndex++] == 'L') {
ARABIC_NUM += 40;
localIndex++;
}
else if (userString[localIndex++] == 'C') {
ARABIC_NUM += 90;
localIndex++;
}
else {
ARABIC_NUM += 10;
}
}
else if (userString[localIndex] == 'L') {
ARABIC_NUM += 50;
}
else if (userString[localIndex] == 'C') {
if (userString[localIndex++] == 'D') {
ARABIC_NUM += 400;
localIndex++;
}
else if (userString[localIndex++] == 'M') {
ARABIC_NUM += 900;
localIndex++;
}
else {
ARABIC_NUM += 100;
}
}
else if (userString[localIndex] == 'D') {
ARABIC_NUM += 500;
}
else if (userString[localIndex] == 'M') {
ARABIC_NUM += 1000;
}
else {
printf("Switch default. You shouldn't be seeing this");
}
/* else
{
printf("[ %c%s]", userString[localIndex],"-x ");
}*/
}
printf("%s%d%s", "\n Number was :", ARABIC_NUM, "\n");
}
bool convertToRoman(char* userString, char* romanStringHolder, size_t counter) {
bool isValid = true;
int arabicNum = atoi(userString);
char repetitionLimit = '\0';
for (size_t index = 0; arabicNum != 0; index++) {
/* if ((isNumTrue(userString[index]) == IS_ARABIC || userString[index] == '\n') && index < counter)
{
printf("[ %c%s]", userString[index], "-o ");
}*/
if (arabicNum >= 4000) {
do {
if (romanStringHolder[index - 2] == romanStringHolder[index - 1] == romanStringHolder[index]) {
repetitionLimit = romanStringHolder[index - 2];
}
if (arabicNum / 1000000 >= 1)//&& repetitionLimit != 'M')
{
romanStringHolder[index] = 'M';
arabicNum -= 1000000;
}
else if (arabicNum / 900000 >= 1)// && repetitionLimit != 'M')
{
romanStringHolder[index] = 'C';
romanStringHolder[++index] = 'M';
arabicNum -= 900000;
}
else if (arabicNum / 500000 >= 1)// && repetitionLimit != 'D')
{
romanStringHolder[index] = 'D';
arabicNum -= 500000;
}
else if (arabicNum / 400000 >= 1)//&& repetitionLimit != 'D')
{
romanStringHolder[index] = 'C';
romanStringHolder[++index] = 'D';
arabicNum -= 400000;
}
else if (arabicNum / 100000 >= 1)// && repetitionLimit != 'C')
{
romanStringHolder[index] = 'C';
arabicNum -= 100000;
}
else if (arabicNum / 90000 >= 1)//&& repetitionLimit != 'C')
{
romanStringHolder[index] = 'X';
romanStringHolder[++index] = 'C';
arabicNum -= 90000;
}
else if (arabicNum / 50000 >= 1)// && repetitionLimit != 'L')
{
romanStringHolder[index] = 'L';
arabicNum -= 50000;
}
else if (arabicNum / 40000 >= 1)// && repetitionLimit != 'L')
{
romanStringHolder[index] = 'X';
romanStringHolder[++index] = 'L';
arabicNum -= 40000;
}
else if (arabicNum / 10000 >= 1)//&& repetitionLimit != 'X')
{
romanStringHolder[index] = 'X';
arabicNum -= 10000;
}
else if (arabicNum / 9000 >= 1)// && repetitionLimit != 'X')
{
romanStringHolder[index] = 'I';
romanStringHolder[++index] = 'X';
arabicNum -= 9000;
}
else if (arabicNum / 5000 >= 1)//&& repetitionLimit != 'V')
{
romanStringHolder[index] = 'V';
arabicNum -= 5000;
}
else if (arabicNum / 4000 >= 1)//&& repetitionLimit != 'I')
{
romanStringHolder[index] = 'I';
romanStringHolder[++index] = 'V';
arabicNum -= 4000;
}
else if (arabicNum / 1000 >= 1)// && repetitionLimit != 'I')
{
romanStringHolder[index] = 'I';
arabicNum -= 1000;
}
index++;
} while (arabicNum >= 4000);
romanStringHolder[index] = '_';
index++;
}
if (arabicNum <= 3999) {
if (arabicNum / 1000 >= 1)
{
romanStringHolder[index] = 'M';
arabicNum -= 1000;
}
if (arabicNum / 900 >= 1)
{
romanStringHolder[index] = 'C';
romanStringHolder[++index] = 'M';
arabicNum -= 900;
}
else if (arabicNum / 500 >= 1)
{
romanStringHolder[index] = 'D';
arabicNum -= 500;
}
else if (arabicNum / 400 >= 1)
{
romanStringHolder[index] = 'C';
romanStringHolder[++index] = 'D';
arabicNum -= 400;
}
else if (arabicNum / 100 >= 1)
{
romanStringHolder[index] = 'C';
arabicNum -= 100;
}
else if (arabicNum / 90 >= 1)
{
romanStringHolder[index] = 'X';
romanStringHolder[++index] = 'C';
arabicNum -= 90;
}
else if (arabicNum / 50 >= 1)
{
romanStringHolder[index] = 'L';
arabicNum -= 50;
}
else if (arabicNum / 40 >= 1)
{
romanStringHolder[index] = 'X';
romanStringHolder[++index] = 'L';
arabicNum -= 40;
}
else if (arabicNum / 10 >= 1)
{
romanStringHolder[index] = 'X';
arabicNum -= 10;
}
else if (arabicNum / 9 >= 1)
{
romanStringHolder[index] = 'I';
romanStringHolder[++index] = 'X';
arabicNum -= 9;
}
else if (arabicNum / 5 >= 1)
{
romanStringHolder[index] = 'V';
arabicNum -= 5;
}
else if (arabicNum / 4 >= 1)
{
romanStringHolder[index] = 'I';
romanStringHolder[++index] = 'V';
arabicNum -= 4;
}
else if (arabicNum / 1 >= 1)
{
romanStringHolder[index] = 'I';
arabicNum -= 1;
}
}
}
if (romanStringHolder > 3999999)
printf("\n");
return isValid;
}
int findNumSystem(char* userString, char* toRomanString) {
//printf(" -%zu and %d-", counter, findStringLength(userString));
size_t counter = findStringLength(userString);
int romanNumAmount = 0;
int arabicNumAmount = 0;
int notNumAmount = 0;
for (size_t localIndex = 0; localIndex < counter; localIndex++) {
if (isNumTrue(userString[localIndex]) == IS_ROMAN || ((isNumTrue(userString[localIndex - 1]) == IS_ROMAN) && (userString[localIndex] == '\n')))
{
printf("[ %c%s]", userString[localIndex], "-R ");
romanNumAmount++;
if (romanNumAmount == (findStringLength(userString) - 1)) {
printf("\nAll Numbers are Roman");
convertToArabic(userString, counter);
break;
}
}
else if (isNumTrue(userString[localIndex]) == IS_ARABIC || ((isNumTrue(userString[localIndex - 1]) == IS_ARABIC) && (userString[localIndex] == '\n')))
{
printf("[ %c%s]", userString[localIndex], "-A ");
arabicNumAmount++;
if (arabicNumAmount == (findStringLength(userString) - 1))
{
printf("\nAll numbers are Arabic");
convertToRoman(userString, toRomanString, counter);
printString(toRomanString);
break;
}
}
else if (isNumTrue(userString[localIndex]) == NOT_A_NUMBER)
{
printf("[ %c%s]", userString[localIndex], "-X ");
notNumAmount++;
if (notNumAmount == (findStringLength(userString))) {
printf("\nNone of the characters is a number of either system");
}
}
}
}
#define LENGTH 1000u
int main() {
typedef char user_Input_Stream;
char lol = '\0';
user_Input_Stream arabToRomanString[LENGTH] = { '\0' };
user_Input_Stream numberInput[LENGTH] = { '\0' };
size_t counter = 0;
bool userEnd = false;
while ((lol != EOF) && (userEnd == false))
{
counter = 0;
printString(&numberInput);
resetString(&numberInput);
resetString(&arabToRomanString);
counter = 0;
printString(&numberInput);
ARABIC_NUM = 0;
printf("\n\n||Beta version, remember to not mix number systems yet||\n");
//Repeats until variable lol countains EOF or until boolean holds a true value
while ((lol != EOF) && (lol != '\n') && (userEnd == false))
{
//gets characters, assigns string with them. Gets rid of newline and stores string in array in uppercase
lol = getchar();
numberInput[counter] = toupper(lol);
userEnd = ifEnd(numberInput, counter);
counter++;
}
//TESTING Travels through String and outputs cells contents. Also, sets boolean to True if user writes END
for (size_t i = 0; i < counter; i++) {
if (numberInput[i] == '\n')
{
lol = '\\';
numberInput[i] = toupper(lol);
}
// printf("| [%zu] = %c |", i, numberInput[i]);
}
findNumSystem(numberInput, arabToRomanString);
printf("\n");
}
printf("\n");
return 0;
}
Does anyone have an idea of what the issue could be? (ARABIC_NUM is a global variable, the name is to make it easier for me to find for now.)
Just in case this might help somebody else, here's the solution I came up with using some of the pointers people in the comment section gave me. It was fairly simple:
I was under the impression that expressions within if-statements don't affect any variables they reference, but in reality they do.
For example: if(userString[localIndex] == 'I' && userString[localIndex++] == 'X')
In the line above, I was expecting the program to check both the current index and the next upcoming index, which it did but the expression userString[localIndex++] within the if-statement also permanently incremented my localIndex variable when I wasn't expecting that change to exist outside of the if-statement's parenthesis. So, my program would check the wrong indexes after the first comparison was made and thus why it gave me the wrong output.
To solve this, I created the variable nextIndex and used it to store the value localIndex +1 meaning it will always represent the index after localIndex. So, It now works as intended.
Below is what my program looks like now. (I moved around the if and if-else statements for better readability but the only changed that solved my predicament was the addition of nextIndex)
void convertToArabic(char* userString, size_t counter) {
size_t nextIndex = 0;
for (size_t localIndex = 0; localIndex < findStringLength(userString); localIndex++) {
// printf("[ %c%s]", userString[localIndex], "-o ");
nextIndex = localIndex+1;
if (userString[localIndex] == 'M') {
ARABIC_NUM += 1000;
}
else if (userString[localIndex] == 'C' && userString[nextIndex] == 'M') {
ARABIC_NUM += 900;
localIndex++;
}
else if (userString[localIndex] == 'D') {
ARABIC_NUM += 500;
}
else if (userString[localIndex] == 'C' && userString[nextIndex] == 'D') {
ARABIC_NUM += 400;
localIndex++;
}
else if(userString[localIndex] == 'C'){
ARABIC_NUM += 100;
}
else if (userString[localIndex] == 'X' && userString[nextIndex] == 'C') {
ARABIC_NUM += 90;
localIndex++;
}
else if (userString[localIndex] == 'L') {
ARABIC_NUM += 50;
}
else if (userString[localIndex] == 'X' && userString[nextIndex] == 'L'){
ARABIC_NUM += 40;
localIndex++;
}
else if (userString[localIndex] == 'X') {
ARABIC_NUM += 10;
}
else if ((userString[localIndex] == 'I') && (userString[nextIndex] == 'X')) {
//localIndex--;
ARABIC_NUM += 9;
localIndex++;
}
else if (userString[localIndex] == 'V') {
ARABIC_NUM += 5;
}
else if ((userString[localIndex] == 'I') && (userString[nextIndex] == 'V')) {
//localIndex--;
printf("Made it in");
ARABIC_NUM += 4;
localIndex++;
}
else if (userString[localIndex] == 'I') {
ARABIC_NUM += 1;
}
/* else
{
printf("[ %c%s]", userString[localIndex],"-x ");
}*/
}
printf("%s%d%s", "\n Number was :", ARABIC_NUM, "\n");
}
Given a n × n checkerboard. Each cell of the board has either character 'x', or character 'o'. Is it true that each cell of the board has even number of adjacent cells with 'o'? Two cells of the board are adjacent if they share a side.
Input
The first line contains an integer n (1 ≤ n ≤ 100). Then n lines follow containing the description of the checkerboard. Each of them contains n characters (either 'x' or 'o') without spaces.
Output
Print "YES" or "NO" (without the quotes) depending on the answer to the problem.
I have been pondering quite a while as to what I have done wrong, can someone please help me point out the error in my logic in my code.
#include<stdio.h>
int main () {
int n,flag=0;
scanf("%d",&n);
char arr[n][n];
int arr_counter[n][n];
for (int x=0;x<n;x++) {
for (int y=0;y<n;y++) {
arr_counter[x][y] = 0;
}
}
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
scanf("%c",&arr[i][j]);
}
}
// corners
// top left
if(arr[1][0]=='o') {
arr_counter[0][0] += 1;
}
if(arr[0][1] == 'o') {
arr_counter[0][0] += 1;
}
// top right
if(arr[0][n-2]=='o') {
arr_counter[0][n-1] += 1;
}
if(arr[1][n-1] == 'o') {
arr_counter[0][n-1] += 1;
}
// bottom left
if(arr[n-2][0]=='o') {
arr_counter[n-1][0] += 1;
}
if(arr[n-1][1] == 'o') {
arr_counter[n-1][0] += 1;
}
// bottom right
if(arr[n-2][n-1]=='o') {
arr_counter[n-1][n-1] += 1;
}
if(arr[n-1][n-2] == 'o') {
arr_counter[n-1][n-1] += 1;
}
// edges
for (int a=1;a<n;a++) {
if(arr[0][a+1] == 'o') {
arr_counter[0][a] += 1;
}
if(arr[0][a-1] == 'o') {
arr_counter[0][a] += 1;
}
if(arr[1][a] == 'o') {
arr_counter[0][a] += 1;
}
}
for (int b=1;b<n;b++) {
if(arr[b-1][0] == 'o') {
arr_counter[b][0] += 1;
}
if(arr[b+1][0] == 'o') {
arr_counter[b][0] += 1;
}
if(arr[b][1] == 'o') {
arr_counter[b][0] += 1;
}
}
for (int c=1;c<n;c++) {
if(arr[c-1][n-1] == 'o') {
arr_counter[c][n-1] += 1;
}
if(arr[c+1][n-1] == 'o') {
arr_counter[c][n-1] += 1;
}
if(arr[c][n-2] == 'o') {
arr_counter[c][n-1] += 1;
}
}
for (int d=1;d<n;d++) {
if(arr[n-1][d+1] == 'o') {
arr_counter[n-1][d] += 1;
}
if(arr[n-1][d-1] == 'o') {
arr_counter[n-1][d] += 1;
}
if(arr[n-2][d] == 'o') {
arr_counter[n-1][d] += 1;
}
}
//middle
for (int s=1;s<n-1;s++) {
for (int t=1;t<n-1;t++) {
if(arr[s+1][t] == 'o') {
arr_counter[s][t] += 1;
}
if(arr[s-1][t] == 'o') {
arr_counter[s][t] += 1;
}
if(arr[s][t+1] == 'o') {
arr_counter[s][t] += 1;
}
if(arr[s][t-1] == 'o') {
arr_counter[s][t] += 1;
}
}
}
for (int k=0;k<n;k++) {
for (int l=0;l<n;l++) {
if (arr_counter[k][l]%2 != 0) {
flag = 1;
}
}
}
if (flag == 0) {
printf("YES");
} else if (flag == 1) {
printf("NO");
}
}
/*
Test Case 1
input
3
xxo
xox
oxx
output
YES
Test Case 2
input
4
xxxo
xoxo
oxox
xxxx
output
NO
*/
I am making a maze using Depth-First search algorithm as a school project. The maze is working pretty much as I want but I have a little problem. I have to use # as a wall and . as a path when printing the maze but I used 0 as a wall and 1 as a path at first thinking that it is gonna be easy to change it later but I was wrong. How can I change 0 and 1 into # and .?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void nahodne_suradnice(int *r, int *s, int n)
{
srand(time(NULL));
*r = ((rand() % n) - 1) + 2;
if (*r == 1 || *r == n)
{
*s = (rand() % n) + 2;
}
else
{
if (rand() % 2 == 1)
*s = 1;
else
*s = n;
}
}
int main()
{
int i, j, n, r, s,smer,posledny_smer[1500];
int maze[500][500];
scanf_s("%d", &n);
if (n < 10 || n > 100)
{
return 0;
}
//vynulovanie pola/bludiska
for (i = 1; i < n + 1; i++)
{
for (j = 1; j < n + 1; j++)
{
maze[i][j] = 0;
}
}
//nahodny vyber zaciatku bludiska
nahodne_suradnice(&r, &s, n);
//generovanie bludiska
j = 0;
maze[r][s] = 2;
for (i = 0 ;; i++)
{
//backtracking
if ((maze[r - 1][s] == 1 || maze[r - 2][s] == 1 || r - 2 <=1 || s==n || s==1) && (maze[r][s + 1] == 1 || maze[r][s + 2] == 1 || s + 2 >= n || r == n || r==1) && (maze[r + 1][s] == 1 || maze[r + 2][s] == 1 || r + 2 >= n || s == n || s==1) && (maze[r][s - 1] == 1 || maze[r][s - 2] == 1 || s - 2 <=1 || r == n || r==1))
{
if (posledny_smer[j-1] == 1)
if (maze[r + 1][s] == 1 && maze[r + 2][s] == 1)
{
r += 2;
j--;
continue;
}
else
{
j--;
continue;
}
if (posledny_smer[j-1] == 2)
if (maze[r][s - 1] == 1 && maze[r][s - 2] == 1)
{
s -= 2;
j--;
continue;
}
else
{
j--;
continue;
}
if (posledny_smer[j-1] == 3)
if (maze[r - 1][s] == 1 && maze[r - 2][s] == 1)
{
r -= 2;
j--;
continue;
}
else
{
j--;
continue;
}
if (posledny_smer[j-1] == 4)
if (maze[r][s + 1] == 1 && maze[r][s + 2] == 1)
{
s += 2;
j--;
continue;
}
else
{
j--;
continue;
}
if (j == 0)
{
if (r == n)
{
nahodne_suradnice(&r, &s,n);
maze[1][s] = 3;
maze[2][s] = 3;
}
if (r == 1)
{
nahodne_suradnice(&r, &s, n);
maze[n][s] = 3;
maze[n - 1][s] = 3;
}
if (s == n-2)
{
nahodne_suradnice(&r, &s, n);
maze[r][1] = 3;
maze[r][2] = 3;
}
if (s == 3)
{
nahodne_suradnice(&r, &s, n);
maze[r][n] = 3;
maze[r][n-1] = 3;
}
break;
}
}
//buranie stien
smer = (rand() % 4) + 1;
if (smer == 1)
{
if (r - 2 >1 && s<n && s>1)
{
if (maze[r - 1][s] == 1 || maze[r - 2][s] == 1)
continue;
maze[r - 1][s] = 1;
maze[r - 2][s] = 1;
r -= 2;
posledny_smer[j] = smer;
j++;
continue;
}
}
if (smer == 2)
{
if (s + 2 < n && r < n && r>1)
{
if (maze[r][s+1] == 1 || maze[r][s+2] == 1)
continue;
maze[r][s + 1] = 1;
maze[r][s + 2] = 1;
s += 2;
posledny_smer[j] = smer;
j++;
continue;
}
}
if (smer == 3)
{
if (r + 2 < n && s < n && s>1)
{
if (maze[r + 1][s] == 1 || maze[r + 2][s] == 1)
continue;
maze[r + 1][s] = 1;
maze[r + 2][s] = 1;
r += 2;
posledny_smer[j] = smer;
j++;
continue;
}
}
if (smer == 4)
{
if (s - 2 >1 && r < n && r>1)
{
if (maze[r][s-1] == 1 || maze[r][s-2] == 1)
continue;
maze[r][s - 1] = 1;
maze[r][s - 2] = 1;
s -= 2;
posledny_smer[j] = smer;
j++;
continue;
}
}
}
//vypis bludiska
for (i = 1; i < n + 1; i++)
{
for (j = 1; j < n + 1; j++)
{
printf("%d", maze[i][j]);
}
printf("\n");
}
system("PAUSE");
return 0;
}
`
Thanks.
Change the definition of maze to:
char maze[500][500]
and change all references to use char instead of int. Then return '#' and '.' instead of 0 and 1.
You can change from 0 and 1 to # and . (or any other representation) when printing. For instance you can change:
printf("%d", maze[i][j]);
to
switch(maze[i][j]) {
case 0:
printf("#");
break;
case 1:
printf(".");
break;
default:
printf("X"); /* in case you have some value other than 0 or 1 in the maze */
}
If you do it like this, you can change which letters you want to display the maze as without changing other parts of your code.
In the main file, I loop through each line of input until it hits the words, then I pass the word its searching for to startSearch with puzzleArray, the solvedArray I want to get back, the word as string, size as number of rows, and length as number of columns.
Currently, I keep getting segmentation faults/or endless loops. Any help over my algorithm/code would be greatly appreciated.
void startSearch(char** puzzleArray,char** solvedArray,char* string,int size,int length)
{
char* direction = "";
int solved = 1;
int j = 0;
while( j <= 7 && solved != 0)
{
if(j == 0)
{
direction = "up";
}
else if(j == 1)
{
direction = "upRight";
}
else if(j == 2)
{
direction = "right";
}
else if(j == 3)
{
direction = "downRight";
}
else if(j == 4)
{
direction = "down";
}
else if(j == 5)
{
direction = "downLeft";
}
else if(j == 6)
{
direction = "left";
}
else if(j == 7)
{
direction = "upLeft";
}
solved = recursiveSearch(puzzleArray,solvedArray,string,direction,size,length,0,0,0);
j++;
}
}
int recursiveSearch(char** puzzleArray,char** solvedArray,char* string,char* direction,int sizeOfPuzzle,int lengthOfArrayWithSpaces,int rowPos,int colPos,int stringPosition)
{
int lengthOfWord;
int i = rowPos;
int j = colPos;
int found = 0;
int empty = 1;
char c = string[stringPosition];
int position = stringPosition;
lengthOfWord = lengthOfArray(string);
if(string[position+1] == '\0')
{
return 0;
}
while(empty != 0)
{
if(string[stringPosition] == puzzleArray[i][j])
{
found = 1;
}
else if(rowPos < sizeOfPuzzle && colPos < lengthOfArrayWithSpaces)
{
stringPosition = 0;
for(i = rowPos; i < sizeOfPuzzle && found != 1; i++)
{
for(j = colPos; j < puzzleArray[rowPos][colPos] != '\0' && found != 1; j++)
{
if(string[stringPosition] == puzzleArray[i][j])
{
found = 1;
rowPos = i;
colPos = j;
stringPosition = 0;
}
}
}
if(found == 0)
{
empty = 1;
}
}
if(found == 1)
{
position = stringPosition + 1;
if(rowPos-1 >= 0)
{
//printf("\nString:%cPuzzleArray:%c",string[position],puzzleArray[rowPos-1][colPos]);
if(string[position] == puzzleArray[rowPos-1][colPos] && direction == "up")
{
//printf("UP");
if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos-1,colPos,position+1))
{
solvedArray[rowPos-1][colPos] = puzzleArray[rowPos-1][colPos];
return 0;
}
}
else if(colPos+2 <= lengthOfArrayWithSpaces)
{
if(string[position] == puzzleArray[rowPos-1][colPos+2] && direction == "upRight")
{
if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos-1,colPos+2,position+1))
{
solvedArray[rowPos-1][colPos+2] = puzzleArray[rowPos-1][colPos+2];
return 0;
}
}
}
}
if(colPos+2 <= lengthOfArrayWithSpaces)
{
if(string[position] == puzzleArray[rowPos][colPos+2] && direction == "right")
{
if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos,colPos+2,position+1))
{
solvedArray[rowPos][colPos+2] = puzzleArray[rowPos][colPos+2];
return 0;
}
}
if(rowPos+1 <= lengthOfArrayWithSpaces)
{
if(string[position] == puzzleArray[rowPos+1][colPos+2] && direction == "downRight")
{
if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos+1,colPos+2,position+1))
{
solvedArray[rowPos+1][colPos+2] = puzzleArray[rowPos+1][colPos+2];
return 0;
}
}
}
}
if(rowPos+1 <= sizeOfPuzzle)
{
if(string[position] == puzzleArray[rowPos+1][colPos] && direction == "down")
{
if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos+1,colPos,position+1))
{
solvedArray[rowPos+1][colPos] = puzzleArray[rowPos+1][colPos];
return 0;
}
}
if(rowPos + 1 <= lengthOfArrayWithSpaces)
{
if(string[position] == puzzleArray[rowPos+1][colPos-2] && direction == "downLeft")
{
if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos+1,colPos-2,position+1))
{
solvedArray[rowPos+1][colPos-2] = puzzleArray[rowPos+1][colPos-2];
return 0;
}
}
}
}
if(colPos-2 >= 0)
{
if(string[position] == puzzleArray[rowPos][colPos-2] && direction == "left")
{
if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos+1,colPos+2,position+1))
{
solvedArray[rowPos+1][colPos+2] = puzzleArray[rowPos+1][colPos+2];
return 0;
}
}
if(rowPos - 1 >= 0)
{
if(string[position] == puzzleArray[rowPos-1][colPos-2] && direction == "upLeft")
{
if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos-1,colPos-2,position+1))
{
solvedArray[rowPos-1][colPos-2] = puzzleArray[rowPos-1][colPos-2];
return 0;
}
}
}
}
}
}
return 1;
}
direction == "up"
This is not how you compare two strings to be equal. Use strcmp / strncmp for string comparison. This kind of comparison appears all over your code.
Also:
for(j = colPos; j < puzzleArray[rowPos][colPos] != '\0' && found != 1; j++)
This j < puzzleArray[rowPos][colPos] != '\0' looks dubious, what are you trying to do?