I am making a simple command-line Candy Crush game. This is my code below. I'm writing it in xCode then running it in Terminal using pico. The gameboard prints out correctly, but it doesn't put Xs on the board. Instead the board is printed and then a segmentation fault occurs.
//
// Assignment 3
//
#include <stdio.h>`enter code here`
//function headers
void displayOriginal (int img[10][10], int col, int row);
void removeOriginal (int img[10][10], int col, int row);
int main(){
//declare variables and array
int col = 0;
int row = 0;
int img[10][10];
//call functions
displayOriginal (img, col,row);
removeOriginal (img, col, row);
displayOriginal (img,col,row);
return 0;
}
//function for displaying gameboard
void displayOriginal (int img[10][10], int col, int row){
for(row = 0; row < 10; row++){//to create rows 0-9
for(col = 0; col < 10; col++){//to create columns 0-9
scanf("%d", &img[row][col]);
}
}
printf(" 0 1 2 3 4 5 6 7 8 9\n");//adds labels to x axis
for(row = 0; row < 10; row++){
printf("%d", row);//add labels to y axis
for(col = 0; col < 10; col++){
if (img[row][col] == 1){ //red
printf("\x1b[41m ");
} else if (img[row][col] == 2){//green
printf("\x1b[42m ");
} else if (img[row][col] == 3){//purple
printf("\x1b[43m ");
} else if (img[row][col] == 4){//blue
printf("\x1b[44m ");
} else if (img[row][col] == 5){//magenta
printf("\x1b[45m ");
} else if (img[row][col] == 0){
printf("XX");
}
printf("\x1b[m"); //white
}
printf("\n");
}
}
//function to label where the X's go
void removeOriginal (int img[10][10], int col, int row){
//variables
int previous = -10;
int temp;
int tally = 1;
for(row = 0; row < 10; row++){
for(col = 0; col < 10; col++){
if (previous == img[row][col]){//if previous block =current then add 1
tally++;
} else {
tally = 1;//return to 1 if previous does not equal current
}
if (tally >= 3){
previous = img[row][col];
for (temp = (tally-1); temp >= 0; temp--){
img [row-temp][col] = 0;
}
} else {
previous = img [row][col];
}
printf("Row %d Col %d Tally %d", row, col, tally);
}
}
}
Are you certain that row >= temp in this section? If it isn't, img[-1][col] will result in a segmentation fault.
if (tally >= 3)
{
previous = img[row][col];
for (temp = (tally-1); temp >= 0; temp--)
{
img [row-temp][col] = 0;
}
}
Related
Have a sudoku solver method that check row/col location for solution and backtracks after testing if a number is not correct. It is printing out No Solution followed by Abort Trap Error: 6. I am using a test case that should return TRUE. Row and Col are initially being passed in parameter as 0
What might be the issue? I'm thinking it could be how I am incrementing row and col. It reads in a single line file and translates to 2D grid
Here is test input: 4.....8.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......
#include <stdio.h>
#include <stdlib.h>
#define BLANK '.'
#define TRUE 1
#define FALSE 0
#define BLANK_SPACE '.'
int findBlankSpace(int grid2D[9][9]);
int valid(int grid2D[9][9], int rowIndex, int colIndex, int num);
void prettyPrint(int grid2D[9][9]);
int readLine(int grid2D[9][9]);
int findSolution(int grid2D[9][9],int row, int col);
int UsedInRow(int grid2D[9][9], int row, int num);
int UsedInCol(int grid2D[9][9], int col, int num);
int UsedInBox(int grid2D[9][9], int boxStartRow, int boxStartCol, int num);
int findSolution(int grid2D[9][9], int row, int col)
{
row = 0;
if (findBlankSpace(grid2D) == FALSE)
return TRUE;
if (row == 9) {
row = 0;
}
for (int num = 1; num <= 9; num++)
{
if (valid(grid2D, row, col, num))
{
grid2D[row][col] = num;
if (findSolution(grid2D, row, ++col))
{
return TRUE;
}
}
grid2D[row][col] = 0;
row++;
}
return FALSE;
}
int findBlankSpace(int grid2D[9][9])
{
int row;
int col;
for (row = 0; row < 9; row++)
for (col = 0; col < 9; col++)
if (grid2D[row][col] == 0)
return TRUE;
return FALSE;
}
int UsedInRow(int grid2D[9][9], int row, int num)
{
for (int col = 0; col < 9; col++)
if (grid2D[row][col] == num)
return TRUE;
return FALSE;
}
int UsedInCol(int grid2D[9][9], int col, int num)
{
for (int row = 0; row < 9; row++)
if (grid2D[row][col] == num)
return TRUE;
return FALSE;
}
int UsedInBox(int grid2D[9][9], int row1, int col1, int num)
{
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
if (grid2D[row+row1][col+col1] == num)
return TRUE;
return FALSE;
}
int valid(int grid2D[9][9], int row, int col, int num)
{
return !UsedInRow(grid2D, row, num) &&
!UsedInCol(grid2D, col, num) &&
!UsedInBox(grid2D, row - row % 3, col - col % 3, num);
}
int readLine(int grid2D[9][9])
{
int c;
int row = 0;
int col = 0;
while((c = getchar()) != EOF)
{
if(c == '.')
{
grid2D[row][col] = 0;
}
else
{
grid2D[row][col] = c - '0';
col++;
}
if(col%9 == 0)
{
row++;
col = 0;
}
}
return 0;
}
void prettyPrint(int grid2D[9][9])
{
int count = 0;
int row;
int col;
printf("solution:\n");
/* Use nested for-loops to iterate through 2-D array */
for(row = 1; row <= 9; row++)
{
for(col = 1; col<=9; col++)
{
/* After every 3rd character, print out "|", forming the board */
if((col%3==0)&&(col%9!=0))
{
printf("%d| ", grid2D[row - 1][col - 1]);
}
else
printf("%d ", grid2D[row - 1][col - 1]);
count++;
}
/* After every 3rd row, print out horizontal separations */
if((row%3==0)&&(row!=9))
{
/* Advance to the next line prior to printing separation to give space */
printf("\n");
printf("------+-------+------");
}
printf("\n");
}
}
int main()
{
int grid2D[9][9];
readLine(grid2D);
if(findSolution(grid2D, 0, 0)==TRUE)
{
prettyPrint(grid2D);
}
else
{
printf("No Solution Found!\n");
}
return 0;
}
in the recursive function: findSolution(), eventually the 'col' parameter will be 9 (or greater)! When that happens, the code will be accessing outside the bounds of the current row in the array. This results in undefined behavior and can lead to a seg fault event.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define N_MAX 10
#define N_MIN 2
#define TEMPSIZE 1024
float RowSum(float **matrix, int sizearray,int row) {
float sum;
for (int j=0 ; j <= row; j++) {
sum = 0;
for (int i=0 ; i < sizearray; i++) {
sum = sum + matrix[j][i];
}
}
return sum;
}
float ColSum(float **matrix, int sizearray, int col) {
float sum;
for (int j = 0; j <= col; j++) {
sum = 0;
for (int i = 0; i < sizearray; i++) {
sum = sum + matrix[i][j];
}
}
return sum;
}
int RepNum(int arraysize, float **matrix) {
int i, j, counter = 0;
int temparray[N_MAX*N_MAX];
for (i = 0; i < N_MAX*N_MAX; i++) {
temparray[i] = 0;
}
for (i = 0; i < arraysize; i++) {
for (j = 0; j < arraysize; j++) {
temparray[(int)matrix[i][j]]++;
}
}
for (i = 1; i < arraysize*arraysize; i++) {
if (temparray[i] > 1)
counter++;
}
return counter;
}
void PrintArray(float **matrix, int arraysize) {
for (int i = 0; i<arraysize; i++)
{
for (int j = 0; j<arraysize; j++)
printf("%3d ", (int)matrix[i][j]);
printf("\n");
}
}
int CheckInt(float **matrix, int arraysize) {
int counter = 0;
for (int i = 0; i < arraysize; i++) {
for (int j = 0; j < arraysize; j++) {
if (((int)matrix[i][j]) != matrix[i][j])
counter++;
}
}
return counter;
}
void main() {
printf("Hello! this program will help you to find out if you have a magic square!\nPlease enter your matrix order and following it the numbers you'd like to check: \n");
float **matrix;
char input[TEMPSIZE];
int sizear = 0;
float correctsum = 0.0;
int counter = 0, row, column;
fgets(input, TEMPSIZE, stdin);
char* str = strstr(input, " ");
if (str == 0)
return;
str[0] = 0;
str++;
sizear = atof(input);
if (sizear > N_MAX || sizear < N_MIN)
{
printf("ERROR: The matrix size cannot be %d size. \n", sizear);
exit(0);
}
matrix = (float**)calloc(1, sizear * sizeof(float*));
for (column = 0; column < sizear; column++)
{
matrix[column] = (float*)calloc(1, sizear * sizeof(float));
}
for (row = 0; row < sizear; row++)
{
for (column = 0; column < sizear; column++)
{
char* temp = strstr(str, " ");
if (temp == 0) /*gets the last number*/
{
++counter;
matrix[row][column] = atof(str);
goto end;
}
if (atof(temp) <= sizear*sizear && atof(temp) > 0) { /*puts all numbers in matrix*/
temp[0] = 0;
matrix[row][column] = atof(str);
str = temp + 1;
++counter;
}
else {
printf("you cannot enter the number %.3f \n", atof(temp));
exit(0);
}
}
}
end:
if (counter > sizear*sizear) {
printf("you've entered %d numbers, while you should've enter %d numners \n", counter, sizear*sizear);
}
else if (counter < sizear*sizear) {
printf("you've entered %d numbers, while you should've enter %d numners \n", counter, sizear*sizear);
}
else if (counter == sizear*sizear) {
correctsum = (float)((sizear*(sizear*sizear + 1)) / 2);
float row = RowSum(matrix, sizear, 0);
float coul = ColSum(matrix, sizear, 0);
if (row == coul && row== correctsum && coul==correctsum && RepNum(sizear, matrix) ==0 && CheckInt(matrix,sizear)==0) {
printf("It's a magic matrix!\n");
PrintArray(matrix, sizear);
}
else {
printf("Not a magic square:\n");
if (row != coul && row != correctsum && coul != correctsum) {
printf("* Coloums and rows sum do not match.\n");
}
if (RepNum(sizear, matrix) != 0) {
printf("* There are repeating numbers.\n");
}
if (CheckInt(matrix, sizear) != 0) {
printf("* One of the numbers or more you've entered isn't integer.\n");
}
}
}
for (column = 0; column < sizear; column++)
{
free(matrix[column]);
}
free(matrix);
}
When I hit more than sizearrayXsizearray numbers it doesn't add into the counter and stops at sizearrayXsizearray counting. Can anyone point out why it doesn't count it?
The exercise asked to check if the input is a magic square (sum rows=columns=diagonals), making sure there are no duplicates, only int numbers and that I don't enter more or less than sizearrayXsizearray numbers. The input from the user should look like 3 1 2 3 4 5 6 7 8 9 where the first number (here it is 3) is the array size and the rest are the numbers that would be checked as a magic square.
When I hit more than sizearrayXsizearray numbers it doesn't add into the counter and stops at sizearrayXsizearray counting. Can anyone point out why it doesn't count it?
Sure, it's simple: the counter is incremented in the loops
for (row = 0; row < sizear; row++)
{
for (column = 0; column < sizear; column++)
{
…
}
}
- the inner statements are run through (at most) sizear × sizear times, so the counter, starting from 0, cannot reach a higher value.
I have a program that is getting a segmentation fault. The program is a magic square program and asks users for the size of a square (a matrix) and then for the numbers of the square by row. I am using a pointer to point to the array in the declareArray function to reference where it is declared in the main function. I want to keep the pointer in order to practice using them, even though I know I can make the program work without pointers. I think the pointer is the problem here but I cant find what I am doing wrong with it.
here is the code:
int main(void)
{
int *arr[SIZE][SIZE] = "\0";
declareArray(&arr);
declareArray();
return 0;
}
//main logic
int declareArray(int *arr[SIZE][SIZE])
{
//variables
int rowNumber = 0;
int dimension = 0;
//int arr[SIZE][SIZE] = {0};
int row, col;
int sum, sum1, sum2;
int flag = 0;
//ask for input of squares
printf("Please enter the dimension of the square: ");
//makes sure the size is between 1 and 15 for the dimension of the square
if(scanf("%d", &dimension) != 1 || dimension >= 15 || dimension < 1)
{
printf("invalid input\n");
return 1;
}
//enter the data
//array rows
for(row = 0; row < dimension; ++row)
{
printf("Please enter the data for row %d: ", ++rowNumber);
//array columns
for(col = 0; col < dimension; ++col)
{
//store the user input
scanf("%2d", &*arr[row][col]);
}
}
printf("\n");
printf("Here is the square");
printf("\n");
//print the square
//array rows
for(row = 0; row < dimension; ++row)
{
//array columns
for(col = 0; col < dimension; ++col)
{
printf("%d", *arr[row][col]);
}
printf("\n");
}
//Checks Sum of diagonal elements
sum = 0;
for (row = 0; row < dimension; row++) {
for (col = 0; col < dimension; col++) {
if (row == col)
sum = sum + *arr[row][col];
}
}
//Checks Sum of Rows
for (row = 0; row < dimension; row++) {
sum1 = 0;
for (col = 0; col < dimension; col++) {
sum1 = sum1 + *arr[row][col];
}
if (sum == sum1)
flag = 1;
else {
flag = 0;
break;
}
}
//Checks sum of Columns
for (row = 0; row < dimension; row++) {
sum2 = 0;
for (col = 0; col < dimension; col++) {
sum2 = sum2 + *arr[col][row];
}
if (sum == sum2)
flag = 1;
else {
flag = 0;
break;
}
}
//if the sums match, it will print a success message with the constant
//if the sums dont match, a fail message will appear
if (flag == 1)
printf("\nTHE SQUARE IS A MAGIC SQUARE WITH A CONSTANT OF %d \n", sum);
else
printf("\nThe Square is not a magic square \n");
return 0;
}
I can see few issues here
int *arr[SIZE][SIZE] = "\0"; // this wont compile
declareArray(&arr); // needless
declareArray();
---
int arr[SIZE][SIZE] = {};
declareArray(arr);
//declareArray();
Declare function
int declareArray(int *arr[SIZE][SIZE]) // because function call changed
---
int declareArray(int arr[SIZE][SIZE])
finally in printf and scanf remove the * operator not needed anymore
as in
scanf("%2d", &*arr[row][col]); // remove *
---
scanf("%2d", &arr[row][col]);
printf("%d", *arr[row][col]); // remove *
---
printf("%d", arr[row][col]);
sum = sum + *arr[row][col]; // remove *
---
sum = sum + arr[row][col];
Note that when you declare an array, the name of the array is a pointer to the first element of the array:
&arr[0] == arr.
The argument passed to the declareArray function is an array of pointers to integers, therefore the space needed for the pointers was allocated yet the space for the actual ints was not, so when you are trying to scan an integer to the address pointed to by arr[row][col], you are trying to write to the address that it holds, 0 in your case , and address 0 is most likely out of the data segment, hence the segment_fault.
What should you do then?
Allocate the space needed with malloc(), assign the address returned to arr[row][col] and then scanf() as shown below, or alternatively, and quite simpler and better use an int array and simply assign the integer to arr[row][col] as shown in the answer above
#include <stdio.h>
#include <stdlib.h>
#define SIZE 15
int declareArray(int * arr[SIZE][SIZE]);
int main(void)
{
int * arr[SIZE][SIZE] = {0};
declareArray(arr);
return 0;
}
//main logic
int declareArray(int * arr[SIZE][SIZE])
{
//variables
int rowNumber = 0;
int dimension = 0;
int row, col;
int sum, sum1, sum2;
int flag = 0;
int * myVal;
//ask for input of squares
printf("Please enter the dimension of the square: ");
//makes sure the size is between 1 and 15 for the dimension of the square
if(scanf("%d", &dimension) != 1 || dimension >= 15 || dimension < 1)
{
printf("invalid input\n");
return 1;
}
//enter the data
//array rows
for(row = 0; row < dimension; ++row)
{
printf("Please enter the data for row %d: ", ++rowNumber);
//array columns
for(col = 0; col < dimension; ++col)
{
printf("insert data to row %d col %d: ", rowNumber, col+1);
arr[row][col] = (int *) malloc(sizeof(int));
scanf("%2d", arr[row][col] );
}
}
printf("\n");
printf("Here is the square");
printf("\n");
//print the square
//array rows
for(row = 0; row < dimension; ++row)
{
//array columns
for(col = 0; col < dimension; ++col)
{
printf("%d", *arr[row][col]);
}
printf("\n");
}
//Checks Sum of diagonal elements
sum = 0;
for (row = 0; row < dimension; row++) {
for (col = 0; col < dimension; col++) {
if (row == col)
sum = sum + *arr[row][col];
}
}
//Checks Sum of Rows
for (row = 0; row < dimension; row++) {
sum1 = 0;
for (col = 0; col < dimension; col++) {
sum1 = sum1 + *arr[row][col];
}
if (sum == sum1)
flag = 1;
else {
flag = 0;
break;
}
}
//Checks sum of Columns
for (row = 0; row < dimension; row++) {
sum2 = 0;
for (col = 0; col < dimension; col++) {
sum2 = sum2 + *arr[col][row];
}
if (sum == sum2)
flag = 1;
else {
flag = 0;
break;
}
}
//if the sums match, it will print a success message with the constant
//if the sums dont match, a fail message will appear
if (flag == 1)
printf("\nTHE SQUARE IS A MAGIC SQUARE WITH A CONSTANT OF %d \n", sum);
else
printf("\nThe Square is not a magic square \n");
return 0;
}
this program is trying to read in a 2D array (5x10) of char, and compress them. Such that (aaacccfeee -> a3c3f1e3). I have some trouble with my output, and I can't figure out where's the problem.
Here's the code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define SIZE1 5
#define SIZE2 10
void compress(char data[SIZE1][SIZE2]);
int main ()
{
int row, col;
char qn2[SIZE1][SIZE2];
printf("\nEnter your data (5x10 of characters): \n");
for (row = 0; row < SIZE1; ++row)
for (col = 0; col < SIZE2; ++col)
scanf("%c", &qn2[row][col]);
fflush(stdin);
compress(qn2);
return 0;
}
void compress(char data[SIZE1][SIZE2])
{
int row, col, count = 0;
char tempChar = data[0][0];
printf("\nThe compression output: \n");
for (row = 0; row < SIZE1; row++)
{
for (col = 0; col < SIZE2; col++)
{
if (data[row][col] == tempChar)
{
count ++;
}
else
{
printf("%c%d", tempChar, count);
tempChar = data[row][col];
count = 1;
}
} printf("\n");
}
}
Expected output:
Enter your data (5x10 of characters):
aaacccdeee
fffggghiii
jjjkkklmmm
nnnooopqqq
rrrssstuuu
The compression output:
a3c3d1e3
f3g3h1i3
j3k3l1m3
n3o3p1q3
r3s3t1u3
Output I got instead:
The compression output:
a3c3d1
e3f3g3h1
i3j3k3l1
m3n3o3p1
q3r3s3t1u3
Try this:
for (row = 0; row < SIZE1; row++)
{
for (col = 0; col < SIZE2; col++)
{
if (data[row][col] == tempChar)
{
count ++;
}
else
{
printf("%c", tempChar);
printf("%d", count);
tempChar = data[row][col];
count = 1;
}
}
}
if(count!=0) //need to print the last sequence of repeated characters.
{
printf("%c", tempChar);
printf("%d", count);
}
I'm trying to create the game Chomp. I am halfway through but pretty stuck.
The game will have 5 different functions. Pointers and structs are not allowed.
a) initialize() Initialize every position in the matrix to 'O'. No parameters or return value.
b) print_board() Prints the matrix. No parameters or return value.
c) get_move() Scan a move from the player (row and col) and "return" it with an array. Parameters: Information about whos turn it is (Player 1 or Player 2), and an array with two elements where the move coordinates will be stored. No return value.
d) check_move() Controls if a move is legal (not outside the matrix and not a position that has already been eaten). Parameters: The move that is going to be checked (row and col). Return value: Result of the control. <------ ???????
e) update_board() Updates the matrix. Parameters: The new move (row and col). No return value.
This is how far I have come and I am stuck at the check_move() function. I don't understand what I am going to return from that function.
#include <stdio.h>
int height = 4;
int width = 10;
char matrix[4][10];
void initialize()
{
for(int row = 0; row < height; row++)
for(int col = 0; col < width; col++)
matrix[row][col] = 'O';
}
void print_board()
{
for(int row = 0; row < height; row++)
{
for(int col = 0; col < width; col++)
{
printf("%c", matrix[row][col]);
}
printf("\n");
}
printf("\n");
}
void get_move(int player, int input[])
{
printf("Player %d, make your move: ", player);
scanf("%d %d", &input[0], &input[1]);
}
int check_move(int position[])
{
int row = position[0];
int col = position[1];
if(row <= height && col <= width)
{
for(row; row <= height; row++)
{
for(col; col <= width; col++)
{
if(matrix[row][col] == ' ');
printf("Invalid move! \n");
}
}
}
}
void update_board(int x, int y)
{
for(int xi = x; xi <= 10; ++xi)
{
for(int yi = y; yi <= 10; ++yi)
matrix[xi-1][yi-1] = ' ';
}
}
int main(void)
{
int player = 1;
int position[2];
initialize();
print_board();
get_move(player, position);
check_move(position);
update_board(position[0], position[1]);
print_board();
getchar();
getchar();
getchar();
return 0;
}
You should return a value from check_move, because it is prototyped that way, I assume you want to use the exit status somewhere? :
int check_move(int position[])
and you have an unnecessary ; at the end of your if:
int check_move(int position[])
{
int row = position[0];
int col = position[1];
int status = 1;
if(row <= height && col <= width)
{
for(row; row <= height; row++)
{
for(col; col <= width; col++)
{
if(matrix[row][col] == ' ') //removed ";" from end of line, otherwise,
{ //printf will always be called
printf("Invalid move! \n");
return 0;
}//also added brackets to force return if error (only need one error to return)
}
}
}
return status; //added return status
}
So, now in the main, you would do something like this to call check_move():
int main(void)
{
int player = 1;
int position[2];
initialize();
print_board();
get_move(player, position);
while(check_move(position) != 1)
{
printf("Try again!\n\n");
print_board();
get_move(player, position);
}
update_board(position[0], position[1]);
print_board();
getchar();
getchar();
getchar();
return 0;
}