This is my first post on StackOverflow, so I apologize if I'm doing something wrong. I'm relatively new to C, so I'm sure my code is fairly ugly and hacky in places, however the majority of the code does what I expect it to. I'm having trouble with a precheck method that I'm using to check a sudoku board before I begin feeding it through my solver-logic. I'm redirecting input from a text file with strings that look like
4.....8.5.34.........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......
4.....8.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.5......
4.....8.5.3..........7......2.....6.....8.4......1...x...6.3.7.5..2.....1.4......
417369825632158947958724316825437169791586432346912758289643571573291684164875293
417369825632158947958724316825437169791586432346912758289643.71573291684164875293
Each string is (ideally) 81 characters with just digits 1-9 and '.'s. I parse a string into a temp char array, and then use the method fillBoard to transfer the chars in the temp array into a 2d int array. Once this is complete, I call my precheck method. If the filled board doesn't pass the row, column, and box checks, the precheck method returns a one, indicating that the puzzle is not solvable (meaning an error message should be printed and that the program should move on to the next string). For some reason, my precheck method is returning one even for strings that should be solvable. I'm not sure why this is. Any help would be appreciated. Thanks.
#include <stdio.h>
#include <string.h>
int alphaError = 0;
struct Point findEmpty(int board[9][9]);
int usedInBox(int board[9][9], int boxStartRow, int boxStartCol, int num);
int positionSafe(int board[9][9], int row, int col, int num);
int usedInCol(int board[9][9], int col, int num);
int usedInRow(int board[9][9], int row, int num);
int solvePuzzle(int board[9][9]);
int precheck(int board[9][9]);
int main()
{
char c;
int charCount = 0;
int i = 0;
char tempStr[100000];
int board[9][9];
while((fscanf(stdin, "%c", &c)) != EOF)
{
printf("%c", c);
if(c != '\n')
{
if(isalpha(c))
{
alphaError = 1;
}
tempStr[i] = c;
i++;
charCount++;
}
else
{
if(charCount != 81 || alphaError == 1)
{
printf("Error\n\n");
i = 0;
charCount = 0;
alphaError = 0;
}
else
{
fillBoard(board, tempStr);
printBoard(board);
if(precheck(board) == 1)
{
printf("Error\n\n");
}
else
{
if(solvePuzzle(board) == 1)
{
printBoard(board);
}
else
{
printf("No solution\n\n");
}
}
i = 0;
charCount = 0;
}
}
}
return 0;
}
struct Point
{
int x;
int y;
} point;
struct Point findEmpty(int board[9][9])
{
struct Point point1;
point1.x = -1;
point1.y = -1;
int row, col;
for(row = 0; row < 9; row++)
{
for(col = 0; col < 9; col++)
{
if(board[row][col] == 0)
{
point1.x = col;
point1.y = row;
}
}
}
return point1;
}
int usedInBox(int board[9][9], int boxStartRow, int boxStartCol, int num)
{
int row, col;
for(row = 0; row < 3; row++)
{
for(col = 0; col < 3; col++)
{
if(board[row + boxStartRow][col + boxStartCol] == num)
{
return 1;
}
}
}
return 0;
}
int positionSafe(int board[9][9], int row, int col, int num)
{
if((usedInRow(board, row, num)) == 0 && (usedInCol(board, col, num)) == 0 && (usedInBox(board, (row-row%3), (col-col%3), num)) == 0)
{
return 1;
}
else
{
return 0;
}
}
int usedInCol(int board[9][9], int col, int num)
{
int row;
for(row = 0; row < 9; row++)
{
if(board[row][col] == num)
{
return 1;
}
}
return 0;
}
int usedInRow(int board[9][9], int row, int num)
{
int col;
for(col = 0; col < 9; col++)
{
if(board[row][col] == num)
{
return 1;
}
}
return 0;
}
int solvePuzzle(int board[9][9])
{
int num;
struct Point point2;
point2 = findEmpty(board);
if(point2.x == -1)
{
return 1;
}
for(num = 1; num <= 9; num++)
{
if(positionSafe(board, point2.y, point2.x, num) == 1)
{
board[point2.y][point2.x] = num;
if(solvePuzzle(board) == 1)
{
return 1;
}
board[point2.y][point2.x] = 0;
}
}
return 0;
}
void printBoard(int board[9][9])
{
int i, j;
for (i = 0; i < 9; i++)
{
for (j = 0; j < 9; j++)
{
printf("%d", board[i][j]);
}
}
printf("\n\n");
}
void fillBoard(int board[9][9], char tempStr[100000])
{
int i, j;
int k = 0;
for(i = 0; i < 9; i++)
{
for(j = 0; j < 9; j++)
{
if(tempStr[k] == '.')
{
board[i][j] = 0;
}
else
{
board[i][j] = (tempStr[k] - '0');
}
k++;
}
}
}
int precheck(int board[9][9])
{
int i, j, num;
for(i = 0; i < 9; i++)
{
for(j = 0; j < 9; j++)
{
if(board[i][j] != 0)
{
num = board[i][j];
if(positionSafe(board, i, j, num) == 0)
{
return 1;
}
}
}
}
return 0;
}
So you are using precheck on an already filled board? That might be the problem because usedInCol, usedInRow and usedInBlock will return 1 if the value is already present. So precheck should be used only while filling the board, not after. It will always return 1 if you check values you take from the already filled board.
Related
I am writing a program to solve Sudoku puzzles in C I declared the array in main() when I try and access it in a different function the compiler gives the error I've tried using pointers to the array but nothing seems to allow me to access it
error: ‘grid’ undeclared (first use in this function) 121 |
printf("%d ", &grid[i][i]);
How can I access this array from a different function?
Here is the code
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main()
{
int grid[9][9] =
{
{8,0,0,0,0,0,0,0,0},
{0,0,3,6,0,0,0,0,0},
{0,7,0,0,9,0,2,0,0},
{0,5,0,0,0,7,0,0,0},
{0,0,0,0,4,5,7,0,0},
{0,0,0,1,0,0,0,3,0},
{0,0,1,0,0,0,0,6,8},
{0,0,8,5,0,0,0,1,0},
{0,9,0,0,0,0,4,0,0}
};
int LineNum = 9;
int RowAmount = 9;
for (int i = 0; i < LineNum; ++i)
{
for (int i = 0; i < RowAmount; ++i)
{
printf("%d ", &grid[i][i]);
}
printf("\n");
}
}
int find_empty_box(int sudoku)
{
for (int x = 0; x <= 9; ++x)
{
for (int y = 0; y < 9; ++y)
{
if (sudoku[&x][&y] == 0)
{
return x, y;
}
}
return NULL, NULL;
}
}
int Answer_Valid(int sudoku, int guess, int row, int col)
{
int row_values = &sudoku[&row];
for (int i = 0; i < sudoku[&row]; ++i)
{
if (guess == sudoku[&row[&i]])
{
return false;
}
}
int column_values = &sudoku[&col];
for (int t = 0; t <= 9; ++t)
{
for (int n = 0; n < &sudoku[&col]; ++n)
{
if (&guess == &sudoku[&col[&n]])
{
return false;
}
}
}
int row_start = (row / 3) * 3;
int col_start = (col / 3) * 3;
for (int x = 0; x <= row_start && row_start + 3; ++x)
{
for (int y = 0; y < col_start && col_start + 3; ++y)
{
if (sudoku[&x][&y] == guess)
{
return false;
}
return true;
}
}
}
int Solver(sudoku, guess)
{
int row, col = find_empty_box(sudoku);
if (row == NULL)
{
return true;
}
for (int i = 0; i < 1||2||3||4||5||6||7||8||9; ++i)
{
if(Answer_Valid(sudoku, guess, row, col))
{
sudoku[&row][&col] = guess;
if(Solver(sudoku))
{
return true;
}
return false;
}
}
int LineNum = 9;
int RowAmount = 9;
for (int i = 0; i < LineNum; ++i)
{
for (int i = 0; i < RowAmount; ++i)
{
printf("%d ", &grid[i][i]);
}
printf("\n");
}
}
first time using the site. I am trying to figure out how to solve a 'maze' using the shortest path. The code works for traditional mazes but the path I am trying to work on is essentially more open. When run the current path goes right, then down, then left and goes up then turns right before finally reaching B. My solution needs to go right then up then left to B. Any help would be appreciated!
9,11
xxxxxxxxxxx
x......B..x
x...xxxx..x
x...xxxx..x
x....A....x
x..xx.xx..x
x.........x
x.........x
xxxxxxxxxxx
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <stdlib.h>
char** maze;
int** checked;
int rows;
int cols;
int start_row;
int start_col;
enum area
{
space,
wall,
end,
trail,
};
void alloc_maze() {
maze = malloc(rows * sizeof(char*));
for (int i = 0; i < rows; i++) {
maze[i] = malloc(cols * sizeof(char*));
}
}
void alloc_checked() {
checked = malloc(rows * sizeof(char*));
for (int i = 0; i < rows; i++) {
checked[i] = malloc(cols * sizeof(char*));
}
}
void get_maze(const char* file_name)
{
char c;
char rows_t[3] = { '\0' };
char cols_t[3] = { '\0' };
int rows_i = 0;
int cols_i = 0;
int swap = 0;
FILE* file = fopen(file_name, "r");
if (file) {
while ((c = getc(file)) != EOF) {
if (c == '\n') {
break;
} else if (c ==',')
{
swap = 1;
}
else if (!swap) {
rows_t[rows_i] = c;
rows_i++;
}
else {
cols_t[cols_i] = c;
cols_i++;
}
}
}
rows = atoi(rows_t);
cols = atoi(cols_t);
alloc_maze();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
c = getc(file);
if (c == '\n') {
c = getc(file);
}
maze[i][j] = c;
if (c == 'A') {
start_row = i;
start_col = j;
}
}
}
fclose(file);
}
void get_checked() {
alloc_checked();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (maze[i][j] == 'x') {
checked[i][j] = wall;
}
else if (maze[i][j] == 'B') {
checked[i][j] = end;
}
else {
checked[i][j] = space;
}
}
}
}
int search(int row, int col) {
int* current = &checked[row][col];
if (*current == end) {
printf("\n congrats you found the shortest path is");
return 1;
}
if (*current == space) {
*current = wall;
if (search(row, col + 1)) {
*current = trail;
printf("E");
return 1;
}
if (search(row + 1, col)) {
*current = trail;
printf("N");
return 1;
}
if (search(row - 1, col)) {
*current = trail;
printf("S");
return 1;
}
if (search(row, col - 1)) {
*current = trail;
printf("W");
return 1;
}
}
return 0;
}
void add_trail() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (maze[i][j] != 'A'){
if (checked[i][j] == trail) {
maze[i][j] = 'O';
}
}
}
}
}
void print_checked() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", checked[i][j]);
}
printf("\n");
}
printf("\n");
}
void print_maze() {
printf("\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%c", maze[i][j]);
}
printf("\n");
}
printf("\n");
}
int main()
{
get_maze("quickest_route_4.txt");
get_checked();
print_maze();
search(start_row, start_col);
add_trail();
print_maze();
return 0;
}
You have implemented a DFS - Depth First Search maze searching algorithm. This means that your searching algorithm is going to explores as far as possible along a direction before trying another one.
In your code this means that it will always try all of the options going right and then all of the options that are going down and this causes your code to not find the shortest path but just finding a path.
If you do want to find the shortest path you should implement a BFS - Breadth First Search algorithm, it will find you the shortest path since it is progressing the search in all of the active nodes at the same time. It will be a bit harder to implement though since it uses a queue data structure.
Good luck
Also notice that the path you are printing is coming out in reverse order.
i'm an trying to write a program that will call a function win if there is n number of X's or O's in a row. I am have difficulty with the diagonal win that slopes upwards. can any offer any advice on what I should change? Thanks in advance. It calls an infinite loop instead of returning true.
bool left_diag_win(char **board, int num_rows, int num_cols) {
int i;
if (board[0][0] == '*') {
return false;
}
for (i = 1; i < num_rows; ++i) {
if (board[i][i] != board[0][0]) {
return false;
}
}
return true;
}
bool right_diag_win(char **board, int num_rows, int num_cols) {
int i;
if (board[0][num_cols - 1] == '*') {
return false;
}
for (i = 1; i < num_rows; ++i) {
if (board[i][num_cols - i - 1] != board[0][num_cols - 1]) {
return false;
}
}
return true;
}
You code seems correct, but you probably have a problem elsewhere. Here is an example where it behaves as expected:
#include <stdbool.h>
#include <stdio.h>
bool left_diag_win(char **board, int num_rows, int num_cols) {
if (board[0][0] == '*') {
return false;
}
for (int i = 1; i < num_rows; ++i) {
if (board[i][i] != board[0][0]) {
return false;
}
}
return true;
}
bool right_diag_win(char **board, int num_rows, int num_cols) {
if (board[0][num_cols - 1] == '*') {
return false;
}
for (int i = 1; i < num_rows; ++i) {
if (board[i][num_cols - i - 1] != board[0][num_cols - 1]) {
return false;
}
}
return true;
}
int main(void) {
char *matrix1[] = { "X****", "*X***", "**X**", "***X*", "****X" };
char *matrix2[] = { "****X", "***X*", "**X**", "*X***", "X****" };
printf("matrix:\n");
for (int i = 0; i < 5; i++) {
printf("%s\n", matrix1[i]);
}
printf("left-diagonal: %d\n", left_diag_win(matrix1, 5, 5));
printf("right-diagonal: %d\n\n", right_diag_win(matrix1, 5, 5));
printf("matrix:\n");
for (int i = 0; i < 5; i++) {
printf("%s\n", matrix2[i]);
}
printf("left-diagonal: %d\n", left_diag_win(matrix2, 5, 5));
printf("right-diagonal: %d\n\n", right_diag_win(matrix2, 5, 5));
return 0;
}
Output:
matrix:
X****
*X***
**X**
***X*
****X
left-diagonal: 1
right-diagonal: 0
matrix:
****X
***X*
**X**
*X***
X****
left-diagonal: 0
right-diagonal: 1
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hey everyone so I wrote a sudoku solver in C that uses recursive backtracking. However the output is not what's expected.For the code to work you need to pass in an array of 81 numbers, and the 0 on the board equals a '.' in the array. The problem is that my output starts filling in everything with 1's in place of where the '.'. I dont understand why and I need a fresh pair of eyes to look it over for me.
#define DEBUG FALSE
#define TRUE 1
#define FALSE 0
#include <stdio.h>
/* function declarations */
int readPuzzle(int puzzle[9][9]);
int findRowErrors(int puzzle[9][9]);
int findColErrors(int puzzle[9][9]);
int findBoxErrors(int puzzle[9][9]);
int solvePuzzle(int puzzle[9][9], int index);
int validMove(int puzzle[9][9], int index, int num);
int noSolution(int puzzle[9][9]);
void writePuzzle(int puzzle[9][9]);
int main (void)
{
int puzzle[9][9];
int index = 0;
int error;
while ((error = readPuzzle(puzzle)) != EOF)
{
error += findRowErrors(puzzle);
error += findColErrors(puzzle);
error += findBoxErrors(puzzle);
if (error) printf("Error\n\n");
else
{
/* in DEBUG mode, show initial puzzle in standard sudoku form */
if (DEBUG) writePuzzle(puzzle);
solvePuzzle(puzzle, index);
if (!noSolution(puzzle)) writePuzzle(puzzle);
}
}
return 0;
}
int readPuzzle(int puzzle[9][9])
{
int i, num, row, col;
int error = FALSE;
for (i = 0; (num = getchar()) != '\n'; i++)
{
if (num == EOF) return EOF;
putchar(num);
if ((num < '1' || num > '9') && (num != '.')) error = TRUE;
if (num == '.') num = '0';
row = (i / 9) % 9;
col = i % 9;
puzzle[row][col] = num - '0';
}
putchar('\n');
if (i != 81) error = TRUE;
return error;
}
int findRowErrors(int puzzle[9][9])
{
int row, col, i;
/* check rows */
for (row = 0; row < 9; row++)
{
for (col = 0; col < 9; col++)
{
for (i = col + 1; i < 9; i++)
{
if ( (puzzle[row][col] != 0) && (puzzle[row][col] == puzzle[row][i]) )
{
return TRUE; /* row error found in puzzle\
*/
}
}
}
}
return FALSE;
}
int findColErrors(int puzzle[9][9])
{
int row, col, i;
for (col = 0; col < 9; col++)
{
for (row = 0; row < 9; row++)
{
for (i = row + 1; i < 9; i++)
{
if ( (puzzle[row][col] != 0) && (puzzle[row][col] == puzzle[i][col]) )
{
return TRUE; /* column error found in puzzle */
}
}
}
}
return FALSE;
}
int findBoxErrors(int puzzle[9][9])
{
int row, col, i, j;
for (row = 0; row < 9; row += 3)
{
for (col = 0; col < 9; col += 3)
{
for (i = 0; i < 9; i++)
{
for (j = i + 1; j < 9; j++)
{
if ( (puzzle[row + i / 3][col + i % 3] != 0) &&
(puzzle[row + i / 3][col + i % 3] ==
puzzle[row + j / 3][col + j % 3]) )
{
return TRUE; /* box error found in puzzle*/
}
}
}
}
}
return FALSE;
}
int noSolution(int puzzle[9][9])
{
int row, col;
for (row = 0; row < 9; row++)
{
for (col = 0; col < 9; col++)
{
if (!puzzle[row][col])
{
printf("No solution\n\n");
return TRUE;
}
}
}
return FALSE;
}
void writePuzzle(int puzzle[9][9])
{
int row, col;
for (row = 0; row < 9; row++)
{
if (DEBUG) printf("\n");
if ((DEBUG) && (row == 3 || row == 6))
{
printf("----------------------\n");
}
for (col = 0; col < 9; col++)
{
if (DEBUG) printf(" ");
if (puzzle[row][col]) printf("%d", puzzle[row][col]);
else printf(".");
if ((DEBUG) && (col == 2 || col == 5)) printf(" |");
}
}
printf("\n\n");
}
int solvePuzzle(int puzzle[9][9], int index)
{
int num;
int row = index / 9;
int col = index % 9;
if (index == 81) return TRUE; /* all cells are filled */
if (puzzle[row][col] != 0)
{
return solvePuzzle(puzzle, ++index); /* recursive call */
}
else
{
for (num = 1; num <= 9; num++)
{
if (validMove(puzzle, index, num))
{
puzzle[row][col] = num;
if (solvePuzzle(puzzle, index)) return TRUE;
puzzle[row][col] = 0;
}
}
return FALSE;
}
}
/*Checks to see Valid moves for rows, columns, and regions*/
int validMove(int puzzle[9][9],int start, int num)
{
int r, c;
int row = start / 9;
int column = start % 9;
int regionFirstRow = row - (row %3 );
int regionFirstColumn = column - (row % 3);
/*Checks rows for valid moves*/
for(c = 0; c < 9; c++)
{
if(puzzle[row][c] == num)
{
return FALSE;
}
}
/*Checks columns for valid moves*/
for(r = 0; r < 9; r++)
{
if(puzzle[r][column] == num)
{
return FALSE;
}
}
/*FINISH THIS!!!!!!!!!*/
/*Checks each 3x3 region for valid moves*/
for(r = 0; r < 3; r++)
{
for(c = 0; c < 3; c++)
{
if(puzzle[regionFirstRow + r][regionFirstColumn + c] == num)
{
return FALSE;
}
}
}
return TRUE;
}
}
There is a bug in the box region calculation in function validMove
int regionFirstColumn = column - (row % 3);
should be
int regionFirstColumn = column - (column % 3);
I have a function that returns a pointer to an array. I'm running it in a loop and free() seems to be giving me problems. I'm not sure where, but it appears that somewhere in the main loop the memory that I'm trying to free is being used. I'm using Xcode 3.2.1 in 10.6 | Debug | x86_64 build.
The program will run through the main loop one time; the second time it encounters the free() it gives me the following error:
malloc: *** error for object 0x100100180: incorrect checksum for freed object -
object was probably modified after being freed.
Can someone point out (no pun intended) what I'm doing wrong with pointers here?
Here is the program:
int main(int argc, char **argv) {
int *partition;
int lowerLimit;
int upperLimit;
// snip ... got lowerLimit and upperLimit from console arguments
// this is the 'main loop':
for (int i = lowerLimit; i <= upperLimit; i += 2) {
partition = goldbachPartition(i);
printOutput(partition[0], partition[1], i);
free(partition); // I get problems on the second iteration here
}
return 0;
}
int *goldbachPartition(int x) {
int solved = 0;
int y, z;
int *primes;
int *result;
result = intAlloc(2);
primes = atkinsPrimes(x);
for (int i = intCount(primes)-1; i >= 0; i--) {
y = primes[i];
for (int j = 0; j < y; j++) {
z = primes[j];
if (z + y >= x) {
break;
}
}
if (z + y == x) {
solved = 1;
result[0] = y;
result[1] = z;
break;
} else if (y == z) {
result[0] = 0;
result[1] = 0;
break;
}
}
free(primes);
return result;
}
int *atkinsPrimes(int limit) {
int *primes;
int *initialPrimes;
int *filtered;
int *results;
int counter = 0;
int sqrtLimit;
int xLimit;
int resultsSize;
primes = intAlloc(limit+1);
intFillArray(primes, limit+1, 0);
sqrtLimit = floor(sqrt(limit));
xLimit = floor(sqrt((limit+1) / 2));
// these loops are part of the Atkins Sieve implementation
for (int x = 1; x < xLimit; x++) {
int xx = x*x;
for (int y = 1; y < sqrtLimit; y++) {
int yy = y*y;
int n = 3*xx + yy;
if (n <= limit && n % 12 == 7) {
primes[n] = (primes[n] == 1) ? 0 : 1;
}
n += xx;
if (n <= limit && (n % 12 == 1 || n % 12 == 5)) {
primes[n] = (primes[n] == 1) ? 0 : 1;
}
if (x > y) {
n -= xx + 2*yy;
if (n <= limit && n % 12 == 11) {
primes[n] = (primes[n] == 1) ? 0 : 1;
}
}
}
}
for (int n = 5; n < limit; n++) {
if (primes[n] == 1) {
for (int k = n*n; k < limit; k += n*n) {
primes[k] = 0;
}
}
}
initialPrimes = intAlloc(2);
if (limit >= 2) {
initialPrimes[counter++] = 2;
}
if (limit >= 3) {
initialPrimes[counter++] = 3;
}
filtered = intFilterArrayKeys(primes, limit+1);
results = intMergeArrays(initialPrimes, filtered, counter, trueCount(primes, limit+1));
resultsSize = counter + trueCount(primes, limit+1);
free(primes);
free(initialPrimes);
free(filtered);
results[resultsSize] = 0;
return results;
}
int trueCount(int *subject, int arraySize) {
int count = 0;
for (int i = 0; i < arraySize; i++) {
if (subject[i] == 1) {
count++;
}
}
return count;
}
int intCount(int *subject) {
// warning: expects 0 terminated array.
int count = 0;
while (*subject++ != 0) {
count++;
}
return count;
}
void intFillArray(int *subject, int arraySize, int value) {
for (int i = 0; i < arraySize; i++) {
subject[i] = value;
}
}
int *intFilterArrayKeys(int *subject, int arraySize) {
int *filtered;
int count = 0;
filtered = intAlloc(trueCount(subject, arraySize));
for (int i = 0; i < arraySize; i++) {
if (subject[i] == 1) {
filtered[count++] = i;
}
}
return filtered;
}
int *intMergeArrays(int *subject1, int *subject2, int arraySize1, int arraySize2) {
int *merge;
int count = 0;
merge = intAlloc(arraySize1 + arraySize2);
for (int i = 0; i < arraySize1; i++) {
merge[count++] = subject1[i];
}
for (int i = 0; i < arraySize2; i++) {
merge[count++] = subject2[i];
}
return merge;
}
int *intAlloc(int amount) {
int *ptr;
ptr = (int *)malloc(amount * sizeof(int));
if (ptr == NULL) {
printf("Error: NULL pointer\n");
}
return ptr;
}
void printOutput(int num1, int num2, int rep) {
if (num1 == 0) {
printf("%d: No solution\n", rep);
exit(0);
} else {
printf("%d = %d + %d\n", rep, num1, num2);
}
}
Why is intAlloc not returning int* ?
int *intAlloc(int amount) {
int *ptr;
ptr = (int *)malloc(amount * sizeof(int));
if(ptr == NULL) {
printf("Error: NULL pointer\n");
exit(1);
}
return ptr; //like this
}
EDIT (after your update):
On atkinsPrimes() where is filtered being intAlloc()ed?
int *atkinsPrimes(int limit) {
int *primes;
int *initialPrimes;
int *filtered;
int *results;
int resultsSize;
primes = intAlloc(limit+1);
// ...
initialPrimes = intAlloc(2);
// ...
resultsSize = counter + trueCount(primes, limit+1);
free(primes);
free(initialPrimes);
free(filtered); // Where was it intAlloc()ed?
results[resultsSize] = 0; // make the array 0-terminated to make it easier to work with
return results;
}
EDIT (after your N-th update):
This is a compilable version of your code. It ran smooth on my machine, no crashes. Compiled with g++ (due to declarations of variables inside the for statement):
g++ (Debian 4.3.2-1.1) 4.3.2
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int *goldbachPartition(int x);
int *atkinsPrimes(int limit);
int trueCount(int *subject, int arraySize);
int intCount(int *subject) ;
void intFillArray(int *subject, int arraySize, int value);
int *intFilterArrayKeys(int *subject, int arraySize);
int *intAlloc(int amount);
void printOutput(int num1, int num2, int rep) ;
int *intMergeArrays(int *subject1, int *subject2, int arraySize1, int arraySize2);
int main(int argc, char **argv) {
if (argc < 3) {
printf("Usage: ./program <lower> <upper>\n");
return 0;
}
int *partition;
int lowerLimit = atoi(argv[1]);
int upperLimit = atoi(argv[2]);
// snip ... got lowerLimit and upperLimit from console arguments
// this is the 'main loop':
for (int i = lowerLimit; i <= upperLimit; i += 2) {
partition = goldbachPartition(i);
printOutput(partition[0], partition[1], i);
free(partition); // I get problems on the second iteration here
}
return 0;
}
int *goldbachPartition(int x) {
int solved = 0;
int y, z;
int *primes;
int *result;
result = intAlloc(2);
primes = atkinsPrimes(x);
for (int i = intCount(primes)-1; i >= 0; i--) {
y = primes[i];
for (int j = 0; j < y; j++) {
z = primes[j];
if (z + y >= x) {
break;
}
}
if (z + y == x) {
solved = 1;
result[0] = y;
result[1] = z;
break;
} else if (y == z) {
result[0] = 0;
result[1] = 0;
break;
}
}
free(primes);
return result;
}
int *atkinsPrimes(int limit) {
int *primes;
int *initialPrimes;
int *filtered;
int *results;
int counter = 0;
int sqrtLimit;
int xLimit;
int resultsSize;
primes = intAlloc(limit+1);
intFillArray(primes, limit+1, 0);
sqrtLimit = floor(sqrt(limit));
xLimit = floor(sqrt((limit+1) / 2));
for (int x = 1; x < xLimit; x++) {
int xx = x*x;
for (int y = 1; y < sqrtLimit; y++) {
int yy = y*y;
int n = 3*xx + yy;
if (n <= limit && n % 12 == 7) {
primes[n] = (primes[n] == 1) ? 0 : 1;
}
n += xx;
if (n <= limit && (n % 12 == 1 || n % 12 == 5)) {
primes[n] = (primes[n] == 1) ? 0 : 1;
}
if (x > y) {
n -= xx + 2*yy;
if (n <= limit && n % 12 == 11) {
primes[n] = (primes[n] == 1) ? 0 : 1;
}
}
}
}
for (int n = 5; n < limit; n++) {
if (primes[n] == 1) {
for (int k = n*n; k < limit; k += n*n) {
primes[k] = 0;
}
}
}
initialPrimes = intAlloc(2);
if (limit >= 2) {
initialPrimes[counter++] = 2;
}
if (limit >= 3) {
initialPrimes[counter++] = 3;
}
filtered = intFilterArrayKeys(primes, limit+1);
results = intMergeArrays(initialPrimes, filtered, counter, trueCount(primes, limit+1));
resultsSize = counter + trueCount(primes, limit+1);
free(primes);
free(initialPrimes);
free(filtered);
results[resultsSize] = 0;
return results;
}
int trueCount(int *subject, int arraySize) {
int count = 0;
for (int i = 0; i < arraySize; i++) {
if (subject[i] == 1) {
count++;
}
}
return count;
}
int intCount(int *subject) {
// warning: expects 0 terminated array.
int count = 0;
while (*subject++ != 0) {
count++;
}
return count;
}
void intFillArray(int *subject, int arraySize, int value) {
for (int i = 0; i < arraySize; i++) {
subject[i] = value;
}
}
int *intFilterArrayKeys(int *subject, int arraySize) {
int *filtered;
int count = 0;
filtered = intAlloc(trueCount(subject, arraySize));
for (int i = 0; i < arraySize; i++) {
if (subject[i] == 1) {
filtered[count++] = i;
}
}
return filtered;
}
int *intMergeArrays(int *subject1, int *subject2, int arraySize1, int arraySize2) {
int *merge;
int count = 0;
merge = intAlloc(arraySize1 + arraySize2);
for (int i = 0; i < arraySize1; i++) {
merge[count++] = subject1[i];
}
for (int i = 0; i < arraySize2; i++) {
merge[count++] = subject2[i];
}
return merge;
}
int *intAlloc(int amount) {
int *ptr;
ptr = (int *)malloc(amount * sizeof(int));
if (ptr == NULL) {
printf("Error: NULL pointer\n");
}
return ptr;
}
void printOutput(int num1, int num2, int rep) {
if (num1 == 0) {
printf("%d: No solution\n", rep);
exit(0);
} else {
printf("%d = %d + %d\n", rep, num1, num2);
}
}
Since you are still omitting some source, I can only imagine that the problem is hidden there.
EDIT: (my last update)
To assist your debugging, you should replace your main() function by the one below:
int main(int argc, char **argv)
{
int *primes = NULL;
primes = atkinsPrimes(44); // Evil magic number
free(primes);
return 0;
}
Having a minimal example to reproduce the behavior you pointed out is much better then the whole thing. Have fun with atkinsPrimes(44)