how to fill two dimensional array(as a pointer) in a function? - arrays

I declared this in the main :
char** board;
int N;
.
.
.
fill_Board(board, N);
then I scanf the number N and using malloc I did dynamic allocation for the char** board like that :
void creat_board(char*** board,int N)
{
int i=0;
(*board)=(char**)malloc(N*sizeof(char*));
if (board==NULL)
{
printf("malloc failed!\n");
exit(1);
}
for (i=0;i<N;i++)
{
(*board)[i]=(char*)malloc(N*sizeof(char)); /*add the
if((*board)[i]==NULL)
{
printf("malloc failed!\n");
exit(1);
}
}
}
I want now to fill the board and to print it but its not working ..here is what I tried to fill:
Void fill_Board(char** board,int N)
{
int i=0,j=0;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
board[i][j]='S';
}
}
}
when I tried to print it the values in the array (board) didnt change at all .why it that happening??

use pointer to array:
void fill_Board(size_t N, char (*board)[N][N])
{
for(size_t i = 0; i < N; i++)
{
for(size_t j = 0; j < N; j++)
{
(*board)[i][j]='S';
}
}
}
or more simple:
void fill_Board(size_t N, char board[N][N])
{
for(size_t i = 0; i < N; i++)
{
for(size_t j = 0; j < N; j++)
{
board[i][j]='S';
}
}
}
to dynamically allocate memory:
void *create_Board(size_t N)
{
return calloc(1, sizeof(char[N][N]));
}

Related

Is there a way to combine two different strings alphabetically using stack in C?

Here is the program:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char *stack;
int stackindex = 0;
int push(char in){
if (stack==NULL) {
stack = (char *) malloc (sizeof(char));
}
else {
stack = (char *) realloc(stack, sizeof(char)* ++stackindex);
}
stack[stackindex] = in;
return 1;
}
char pop(){
return stack[stackindex--];
}
void show() {
for (int i=stackindex; i>=0;i--) {
printf("%c", pop());
}
}
int main(int argc, char**argv) {
for (int i = 1; i < argc; i++) {
int k = strlen(argv[i]);
int j;
for (int j = 0; j < k ; j++) {
char in = argv [i] [j];
push( in );
}
for (j = i; j < k; j++){
char temp;
if (stack[i] > stack[j]) {
temp = stack[j];
stack[j] = stack[i];
stack[i] = temp;
}
if (i, argc-1) push(' ');
}
show();
return 0;
}
}
I've been trying to make an output from the string "im loving" alphabetically in descending order (ASCII based) using command line arguments like this: vonmliig, but the output was "m i" instead.
Looks like you are trying to sort as you build the stack. I would build the stack first, then sort.
int main(int argc, char**argv)
{
if (argc >= 3) {
// Build stack
for (int i = 1; i < argc; i++) {
int k = strlen(argv[i]);
for (int j = 0; j < k ; j++) {
push(argv[i][j]);
}
}
// Sort
for (int i = 0; i < stackindex; i++) {
for (int j = i; j < stackindex; j++){
if (stack[i] > stack[j]) {
char temp = stack[j];
stack[j] = stack[i];
stack[i] = temp;
}
}
}
show();
}
return 0;
}
Also, there is an error in the handling of stackindex in push().
int push(char in)
{
char *temp = realloc(stack, stackindex + 1);
if (!temp) {
// Handle error
return -1;
}
stack = temp;
stack[stackindex++] = in;
return 1;
}
char pop()
{
if (stackindex) return stack[--stackindex];
else return 0;
}
void show()
{
do {
printf("%c", pop());
} while (stackindex);
}

Sort a matrix row wise (c programming)

I wrote this code for sorting an nxn matrix: odd rows in descending order, double rows in ascending order, but it doesn't pass the compiler stage.
What did I do wrong?
It mostly tells me this: assignment to 'int' from 'int *' makes integer from pointer without a cast (how can I solve this problem)?
#include <stdio.h>
#include <stdlib.h>
int comp_a(int a, int b) {
if (a < b) {
return 1;
} else {
return 0;
}
}
int comp_d(int a, int b) {
if (a > b) {
return 1;
} else {
return 0;
}
}
void sort(int a[], int n, int (*comp)(int, int)) {
int t;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
if (comp(a[j], a[j + 1]) == 0) {
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
}
int main() {
int n;
printf("Enter the dimension of matrix(n): ");
scanf("%d", &n);
int *mat = (int*)calloc(n, sizeof(int));
for (int i = 0; i < n; i++) {
mat[i] = (int*)calloc(n, sizeof(int));
}
for (int i = 0; i < n; i++) {
printf("Enter row [%d]: ", i);
for (int j = 0; j < n; j++) {
scanf("%d", &mat[i][j]);
}
}
for (int i = 0; i < n; i++) {
if (i%2 == 0) {
sort(mat[i], n, &comp_a);
} else {
sort(mat[i], n, &comp_d);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
return 0;
}
You should change your matrix allocation to this:
int **mat = (int**)calloc(n, sizeof(int*)); // (1)
for(int i = 0; i < n; i++) {
mat[i] = (int*)calloc(n, sizeof(int)); // (2)
}
(1): Declaring an array of pointers of size n x int*.
(2): Where each pointer in this array points to an array of integers of size n x int.
I changed your matrix allocation
int *mat = (int*)calloc(n, sizeof(int));
for(int i = 0; i < n; i++) {
mat[i] = (int*)calloc(n, sizeof(int));
}
to
int mat[n][n];
I changed for loop of ordred
for(int i = 0; i < n; i++) {
for(int j = 0; j < n - 1; j++) {
if(comp(a[j], a[j + 1]) == 0)
because he take a lot of time (time of running) to
for(int i = 0; i < n; i++) {
for(int j = i+1; j < n; j++) {
if(comp(a[i], a[j]) == 0)
my code:
#include <stdio.h>
#include <stdlib.h>
int comp_a(int a, int b) {
if(a < b) {
return 1;
}
else {
return 0;
}
}
int comp_d(int a, int b) {
if(a > b) {
return 1;
}
else {
return 0;
}
}
void sort(int a[], int n, int (*comp)(int, int)) {
int t;
for(int i = 0; i < n; i++) {
for(int j = i+1; j < n; j++) {
if(comp(a[i], a[j]) == 0) {
t = a[j];
a[j] = a[i];
a[i] = t;
}
}
}
}
int main() {
int n;
do
{
printf("Enter the dimension of matrix(n): ");
scanf("%d", &n);
}while(n<1);
int mat[n][n];
for(int i = 0; i < n; i++) {
printf("Enter row [%d]: ", i);
for(int j = 0; j < n; j++) {
scanf("%d", &mat[i][j]);
}
}
for(int i = 0; i < n; i++) {
if(i%2 == 0) {
sort(mat[i], n, &comp_a);
}
else {
sort(mat[i], n, &comp_d);
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
return 0;
}

problem reading 2d array after passing a ponter from one function to another

I'm trying to read from a 2D array I have declared in my main function but while being in a different function. I thought that if I will send a pointer to the first cell of that array then this will be possible, however I'm still having problem doing it
The issue is passing a 2d array which I have declared in my main function to another function, which itself is called from another function. I know this is a basic question but after many tries I still can't understand what I'm doing wrong and would sincerely appreciate your help.
I've simplified the following code the problem in the following code:
void main(){
N = 5, M = 4
double arr[][4] = {
{ 1,2,1,5 },
{ 8,9,7,2 },
{ 8,7,6,1 },
{ 5,4,5,3 },
{ 5,4,5,3 }
};
double(*pointer)[4]; // pointer creation
pointer = arr; //assignation
function_1(pointer ,N,M);
}
function_1(double *arr, int N, int M){
function_2(arr,N,M);
}
function_2(double *arr, int N, int M){
int c = 0;
for(int i=0; i<n; i++){
for(int j=0l j<M; j++){
arr[i][j] = c; // error while trying to read from arr[i][j]
c += 1;
}
}
}
I have specefique the len of array in all of the function
function_2(double arr[5][4], int N, int M)
{
int c = 0;
for(int i=0; i<N; i++)
{
for(int j=0; j<M; j++)
{
arr[0][0] = c;
c += 1;
}
}
}
void function_1(double arr[5][4], int N, int M)
{
function_2(arr,N,M);
}
int main()
{
int N = 5, M = 4;
double arr[][4] =
{
{ 1,2,1,5 },
{ 8,9,7,2 },
{ 8,7,6,1 },
{ 5,4,5,3 },
{ 5,4,5,3 }
};
function_1(arr ,N,M);
return 0;
}

Can't swap top element with bottom element in matrix

I'm trying to write sliding puzzle where I'm using 3x3 matrix as board. In main function user inputs the number of tile which swaps positions with 0. Everything works except when I enter number that is located in a position above zero it does not swap positions with it. How do I correct that?
Here's the code:
#include <stdio.h>
#include <stdlib.h>
#define empty_space 0
int m,n,i,j,z,y;
void print_matrix(int matrix[3][3])
{
for ( m=0; m<3; m++){
for (n=0; n<3; n++)
{
printf("%d\t",matrix[m][n]);
} printf("\n");
}
printf("\n");
}
void swap(int *i, int *j) {
int t = *i;
*i = *j;
*j = t;
}
void slide(int a[3][3] , int t)
{
for ( i=0; i<3; i++){
for ( j=0; j<3; j++)
{
if (a[i][j]==t)
{
if (a[i+1][j]==empty_space && i+1<=2)
{
swap(&a[i+1][j],&a[i][j]);break;
}
if (a[i-1][j]==empty_space && i-1>=0)
{
swap(&a[i][j],&a[i-1][j]);break;
}
if (a[i][j+1]==empty_space && j+1<=2)
{
swap(&a[i][j],&a[i][j+1]);break;
}
if (a[i][j-1]==empty_space && j-1>=0)
{
swap(&a[i][j],&a[i][j-1]);break;
}
}
}
}
}
int goal_test (int a[3][3],int b[3][3])
{
int flag=0;
for ( z=0; z<3; z++){
for ( y=0; y<3; y++){
if(a[z][y]==b[z][y])
flag++;
}
}
if (flag==9)
return 1;
else return 0;
}
int main()
{
static int mat[3][3]={{1,2,3},{6,0,4},{7,5,8}};
int goal[3][3]={{1,0,3},{6,5,4},{7,8,2}};
print_matrix(mat);
int x;
while(goal_test(mat,goal)==0)
{
printf("enter tile to slide:\t");
scanf("%d",&x);
slide(mat, x);
print_matrix(mat);
}
return 0;
}
Here's what happens:
Any number can swap its position as long as it's not located above zero

C sudoku backtracking solver precheck function

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.

Resources