Sorry for the vague title, I wasn't really sure how to explain. I'm trying to solve the Eight Queens puzzle.
For those unfamiliar with the eight queens puzzle:
This program is supposed to find a possible way that 8 queens can
be placed on an 8x8 chessboard so that the queens cannot
capture one another -- that is, so that no column, row, or
diagonal is occupied by more than one queen.
The way I have it mapped out in my head is:
1) I'll set the whole array: chess[8][8] = {2}
2) Go to the beginning of the array, and as long as chess[i][j] == 2, it will be reassigned to 1. Then as soon as that happens, I have another program block called set_illegal, which will go and set the diagonals, row, and column to 0. (For that reason I will have to have chess[8][8] be a global variable.)
3) After set_illegal is finished, the test program will jump back into the loop of assign_q and the process will start all over again.
4) After this it will print out the solution. Unfortunately I didn't code this find multiple solutions... so it will only display 1 solution (kind of nooby programming lol...)
Any input is much appreciated!!
#include <stdio.h>
#include <stdlib.h>
#define N 8
int chess[N][N] = {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}, row1, column1;
//the reason for row1 and column1 is so that set_row, set_column, and set_diagonal
//will have a spot to start. Also I could have assigned all the elements in the array to 2 using a loop, but I was feeling a little lazy...
void assign_q(int **chess[N][N]**);
int main()
{
int row, column;
assign_q(chess);
for (row = 0; row < N; row++) //prints the whole table
{
for (column = 0; column < N; column++)
printf("%d ", chess[row][column]);
printf("\n");
}
return 0;
}
void set_illegal(void);
void assign_q(int chess[N][N])
{
int row, column;
for (column = 0; column < N; column++)
{
for (row = 0; row < N; row++)
{
if (chess[row][column] == 2) //if the element of the array is equal to 2, then it will set it to 1
{
chess[row][column] = 1;
row1 = column;
column1 = column;
set_illegal(); //goes through the column, row, and diagonal to set them all illegal
break;
}
}
}
}
void set_column(void);
void set_row(void);
void set_diagonal(void);
void set_illegal()
{
set_column();
set_row();
set_diagonal();
}
void set_column()
{
int row;
for (row = 0; row < N; row++)
chess[row][column1] = 0; //sets the column illegal
}
void set_row()
{
int column;
for (column = 0; column < N; column++)
chess[row1][column] = 0; //sets the row illegal
}
void set_diagonal()
{
int row, column;
for (row = row1 + 1, column = column1 + 1; row < N && column < N; row++, column++)
chess[row][column] = 0; //sets diagonals in the slope of -1 downwards illegal
for (row = row1 - 1, column = column1 - 1; row >= 0 && column >= 0; row--, column--)
chess[row][column] = 0; //sets diagonals in the slope of -1 upwards illegal
for (row = row1 - 1, column = column1 + 1; row >= 0 && column < N; row--, column++)
chess[row][column] = 0; //sets diagonals in the slope of +1 upwards illegal
for (row = row1 + 1, column = column1 - 1; row < N && column >= 0; row++, column--)
chess[row][column] = 0; //sets diagonals in the slope of +1 downwards illegal
}
After the change in bold, the only error I get is that the program doesnt actually work ahahaha. Anyways I'll figure that one out. Thanks for all the help!
void assign_q(int chess) - I think you want a board here, not a single field.
Related
int a[3][4], i, j;
int row_total;
printf("Enter matrix values : \n");
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 4; j++)
scanf("%d", &a[i][j]);
}
row_total *= a[i][j];
if (row_total > 0) {
printf("%d\n", row_total);
}
The matrix in the example has 3 rows and 4 columns. The values of this matrix are determined by the user. There are examples everywhere in the form of the product of 2 matrices. But I couldn't find how to process the values in rows of a single matrix. I need to multiply the values that the user enters for the row. How can I find the product of the values written in the rows of the matrix?
In the last part, I just tried to print the values to see if I could read the matrix multiplication of the code. But I could not read the values and get any multiplication result.
initialization
Always initialize your variables
because when you don't initialize it, it will have a "garbage" value.
You had problems reading the values because of the garbage value in row_total.
https://stackoverflow.com/a/1597426/17652416
Arrays
Pay attention that arrays start from 0 not 1 so if the amount of rows is 4 for
example, the rows will be - 0, 1, 2, 3 and not 1, 2, 3, 4.
So the iterations should start from i = 0; i < 4 and not i = 1; i <= 4.
More about arrays in C
Fixed initialization
Defines
#define ROWS 3
#define COLS 4
int a[ROWS][COLS] = { 0 };
int row = 0, col = 0;
int row_total = 1;
printf("Enter matrix values : \n");
//Get the values for the matrix from the user
for (row = 0; row < ROWS; row++)
{
for (col = 0; col < COLS; col++)
{
scanf("%d", &a[row][col]);
}
}
Iterating on the rows - the answer
//Print the product of every row
for (row = 0; row < ROWS; row++)
{
row_total = 1;
for (col = 0; col < COLS; col++)
{
row_total *= a[row][col];
}
printf("The product of row %d is: %d\n", row, row_total);
}
Hi a have a code like these:
for (int row = 0; row < a.length; row++) {
for (int col = 0; col < a[row].length; col++) {
// Do something with a[row][col];
}
}
But i want to make an operation in a grid (8-neighbours) for every a[row][col] , however when im at the corners i will have problems (i don't know how to check if im in a corner), i was thinking in code a lot of if - conditionals, but i dont know the effective way to do this..
If there is a perse method for do this types of traversal neighbours arrays i would very greatful if you could give me a link, I've spent all day looking for information and I can't find anything.
You can use these arrays which represent the changes in the indexes when you look at the neighbors from a given position:
int dx[] = {-1,0,1,1,1,0,-1,-1};
int dy[] = {-1,-1,-1,0,1,1,1,0};
At position (row, col), dx[i] represents a change in row and dy[i] represents a change in col. You can use each position in the array to look at the neighbors.
Make a helper function that checks if a position is safe:
boolean isValidPosition(int x, int y) {
return 0 <= x && x < a.length && 0 <= y && y < a[x].length;
}
Then iterate over all 8 positions and you will have access to the neighbors via a[row + dx[i]][col + dy[i]].
for (int row = 0; row < a.length; row++) {
for (int col = 0; col < a[row].length; col++) {
for (int i = 0; i < 8; i++) {
if (isValidPosition(row + dx[i], col + dy[i])) {
// Do something with a[row + dx[i]][col + dy[i]];
}
}
}
}
I want to output Yay if the matrix doesn't contain the same number on the same row or column otherwise output Nay
this is for my college homework. I already tried to check the column and row in the same loop but the output still not right
#include <stdio.h>
int main()
{
int size;
int flag;
scanf("%d",&size);
char matrix[size][size];
for (int i = 0; i < size; i++)
{
for (int l = 0; l < size; l++)
{
scanf("%s",&matrix[i]);
}
}
for (int j = 0; j < size; j++){
for (int k = 0; k < size; k++){
if(matrix[j] == matrix[j+1] || matrix[j][k]==matrix[j][k+1])
{
flag = 1;
}
else
{
flag = 0;
}
}
}
if (flag == 1)
{
printf("Nay\n");
}
else
{
printf("Yay\n");
}
return 0;
}
I expect to output "Nay" when I input
3
1 2 3
1 2 3
2 1 3
and "Yay" when i input
3
1 2 3
2 3 1
3 1 2
Your matrix is a 2D array and you are referencing it using only a single subscript matrix[index] at several places which returns the address of the row. Index it using both the row and column indices. Try the code below:
{
int size;
int flag;
scanf("%d",&size);
char matrix[size][size];
for (int i = 0; i < size; i++)
{
for (int l = 0; l < size; l++)
{
scanf("%s",&matrix[i][l]);
}
}
for(int j = 0; j < size; j++){
for (int k = 0; j < size; j++){
if(matrix[j][0]== matrix[j][k] || matrix[k][0]==matrix[k][j])
{
flag = 1;
break;
}
else
{
flag = 0;
}
}
}
if (flag == 1)
{
printf("Nay\n");
}
else
{
printf("Yay\n");
}
return 0;
}
Your have a logic problem. Your flag is reset on every element in the matrix and thus only reflects the result of the last check.
In addition, you need a break; in your nested loop. The logic is, if your flag becomes 1, you are sure to say Nay, and you don't want the flag to be reset to 0.
int flag = 0;
for (int i = 0; i != size && !flag; ++i) {
for (int j = 0; j != size; ++j) {
if ( /* detection */ ) {
flag = 1;
break;
}
}
}
if (flag)
printf("Nay\n");
else
printf("Yay\n");
Note: The commented /* detection */ part requires more work. Since it's your homework, you may try it first. You could use a hash table for memorization. Or brutal force to make the program simply work. It seems that your detection only checks for neighboring elements, which is not sufficient to assert that an element is unique in its row or column. Consider
1 2 1
3 4 5
6 7 8
I can't do your homework for you. The following is the brutal-force way you may consider.
The if ( /* detection */ ) part could be if (has_same_element(matrix, i, j)), with a function (pseudo code)
int has_same_element(matrix, row, col)
{
for each element a in matrix's row except matrix[row][col] itself
if (a == matrix[row][col])
return 1
for each element b in matrix's col except matrix[row][col] itself
if (b == matrix[row][col])
return 1
return 0
}
Of course there are smarter ways, like using a hash table, in which case you don't even need the nested loop. For the time being, work out a feasible solution, instead of the best solution.
I'm trying to solve the 8 queens puzzle problem in C. I'm having problems with the recursive search. The program is supposed to start at a given column:
execute(tabuleiro,8,0);
Where the 8 is the number of columns in the board, and 0 is the start column.
This works when I start at column 0. When I send any other column number to the recursive search, the program just counts to the last column. For example, if I choose to start the search from the number 5 column, the code search from the column 5 to 7, after this it should search from 0 to 4, but it doesn't do that.
If I do this:
execute(tabuleiro,8,3);
It fills in only the last 5 colummns, and does not return to column 0 to finish the solution:
Also, how can I select the initial position for the queen in this code? Like I said before, the column is assigned in the code, but I'm not sure how to pick the correct column.
The code has 3 functions: one is to display the board, a second to check if the move is legal (so one queen doesn't attack the other), and the last one to place one queen and recur for the remainder of the board.
#include <stdlib.h>
#include <windows.h>
int sol = 0;
void viewtab(int tab[][8], int N)
{
int i,j;
for( i = 0; i < N; i++)
{
for( j = 0; j < N; j++)
{
if(tab[i][j] == 1)
printf("R\t");
else
printf("-\t");
}
printf("\n\n");
}
printf("\n\n");
system("pause");
printf("\n");
}
int secury(int tab[][8], int N, int lin, int col)
{
// this function is to check if the move is secury
int i, j;
// attack in line
for(i = 0; i < N; i++)
{
if(tab[lin][i] == 1)
return 0;
}
//attack in colune
for(i = 0; i < N; i++)
{
if(tab[i][col] == 1)
return 0;
}
// attack in main diagonal
//
for(i = lin, j = col; i >= 0 && j >= 0; i--, j--)
{
if(tab[i][j] == 1)
return 0;
}
for(i = lin, j = col; i < N && j < N; i++, j++)
{
if(tab[i][j] == 1)
return 0;
}
// attack in main secondary
for(i = lin, j = col; i >= 0 && j < N; i--, j++)
{
if(tab[i][j] == 1)
return 0;
}
for(i = lin, j = col; i < N && j >= 0; i++, j--)
{
if(tab[i][j] == 1)
return 0;
}
// if arrive here the move is secury and return true
return 1;
}
void execute(int tab[][8], int N, int col)
{
int i;
if(col == N)
{
printf("Solution %d ::\n\n", sol + 1);
viewtab(tab, N);
sol++;
return;
}
for( i = 0; i < N; i++)
{
// check if is secury to put the queen at that colune
if(secury(tab, N, i, col))
{
// insert the queen (with 1)
tab[i][col] = 1;
// call recursive
execute(tab, N, col + 1);
// remove queen (backtracking)
tab[i][col] = 0;
}
}
}
int main()
{
int i, j, tabuleiro[8][8];
for (i = 0; i < 8; i = i + 1)
for (j = 0; j < 8; j = j + 1) tabuleiro[i][j] = 0;
execute(tabuleiro,8,0);
return 0;
}
The search always stops in the rightmost column because you specifically tell it to stop there:
void execute(int tab[][8], int N, int col)
{
int i;
if(col == N)
{
printf("Solution %d ::\n\n", sol + 1);
viewtab(tab, N);
sol++;
return;
}
Look at your termination condition: you check the current column against the highest column number, and stop there.
If you want to go back to column 0, you have to change your loop logic. For instance, let col reach N, at which point you reset it to 0, and let it continue until you hit the original value. Another way is to continue until the count of placed queens is N.
You choose the initial point in the same way: you pick the first one and make your recursive call. If that eventually results in a solution, you print it. If not, your top-most call continues to the next row (line) of the board and puts the first queen there.
This is already in your main logic. Just make sure that secury will return true when the board is empty, rather than false or throwing an error.
A. You can place the first Queen at (0,0).
B. And begin the search also from (0,0).
C. I do not see any need to start looking for some other index.
Successfully!!
I haven't used C for quite a long time.
I have a 2d array, where each element is 1 or 0. I want to know for each line whether there is 1 or not. I am doing so :
for (row = xa; row < 50; row++) {
// need to know first if there is any '1' in the next line
if (|schemaArray[row] == 1) {
printf("1 found in row %d\n",row );
}
}
Am I using it wrong?
schemaArray is an argument in my parameter list :
int findPerimeter(int schemaArray[50][50]) {
You will have to iterate over all columns of the row to check if there exists a 1.
Sample code:
for (int row = xa; row < 50; row++ ) {
int bIsOne = 0;
for (int i = 0;i < col_size && !bIsOne; i++ ) {
bIsOne = bIsOne | schemaArray[row][i];
}
if( bIsOne )
printf("1 found in row %d\n",row );
}
This can't be done using bit-wise operators unless you construct a bitmap for the schemaArray. In that case, you could check the entire row at once.
This is mostly an overkill. Do it only if your piece of code is performance critical.
Preprocessing step: Construct a bitmap array for schemaArray
long long bitMapSchemaArray[ROW_SIZE];
for (int i = 0; i < row_count; i++) {
long long columnBitMap = 0;
for (int j = 0; j < col_count; j++ ) {
columnBitMap <<= 1; // Multiplies by 2
columnBitMap = columnBitMap | schemaArray[i][j]; // Add a 1 if schemaArray[i][j]=1 else 0
}
bitMapSchemaArray[i] = columnBitMap;
}
In your function, you could then use the bitmap as:
for (int i = 0; i < row_count; i++) {
if( bitMapSchemaArray[i] )
printf("There is a 1 in %d row\n", i+1);
}
However, at most, you will be able to have 64 columns in 2-D array assuming we use an array of 64 bit integers. Of course, you can also extrapolate this to have more than 64 columns by using ceil(column_count)/64 64 bit integers. In that case, bitwise OR each column to check if the cumulative result is still non-zero.
Simple just iterate over the entire row and find out
for (row = 0; row < 50; row++) {
for (col= 0; row < 50; col++) {
if (schemaArray[row][col] == 1){
printf("1 found in row %d\n",row );
break;
}
}
}