I'm having trouble with this assignment. I am supposed to create minesweeper-ish arrays. It reads in a file:
100
3
0 0
2 1
7 7
10
0 0
0 1
0 2
0 3
0 4
0 5
0 6
0 7
7 0
7 1
the first line is the number of boards (100) the second line is the number of bombs in the board. the 2 integer lines are the row and column, respectively of the location of the bomb.
my problem is it only prins out one board and my second question is how do i switch the 9 to a * in the board?
thank you!
#include <stdio.h>
#define BOARD_SIZE 8
#define BOMB 9
int main() {
FILE *fp;
fp = fopen("mine.txt","r");
int numberBoards = 0;
int numberMines = 0;
int col;
int row;
int currentBoard = 0;
// first row is going to be the number of boards
fscanf(fp,"%d",&numberBoards);
while ( fscanf(fp,"%d",&numberMines) > 0 ) {
int i,j;
// start board with all zeros
int board[BOARD_SIZE][BOARD_SIZE] = { {0} };
currentBoard++;
printf("Board #%d:\n",currentBoard);
//Read in the mines and set them
for (i=0; i<numberMines; i++) {
fscanf(fp,"%d %d",&col,&row);
board[col-1][row-1] = BOMB;
}
//mine proximity
for (i=0; i<BOARD_SIZE; i++) {
for (j=0; j<BOARD_SIZE; j++) {
if ( board[i][j] == BOMB ) {
//Square to the left
if (j > 0 && board[i][j-1] != BOMB) {
board[i][j-1]++;
}
//Square to the right
if (j < 0 && board [i][j+1] !=BOMB){
board [i][j+1]++;
}
//Square to the top
if (i > 0 && board [i-1][j] !=BOMB) {
board [i-1][j]++;
}
//Square to the bottom
if ( i < 0 && board [i+1][j] !=BOMB){
board [i+1][j]++;
}
}
}
//Print out the minesweeper board
for (i=0; i<BOARD_SIZE; i++) {
for (j=0; j<BOARD_SIZE; j++) {
printf("%d ",board[i][j]);
}
printf("\n");
}
printf("\n");
}
//Close the file.
fclose(fp);
return 0;
}
}
The fclose and return should not be in the while loop - that is why it is creating just one board.
I don't understand your question about printing multiple boards. To print a * instead of a 9:
if ( board[i][j] == 9 ) printf("* ");
else printf("%d ",board[i][j]);
Related
I want to get output like this using nested for loops:
0 0
0 1
0 2
0 3
1 3
1 2
1 1
1 0
2 0
2 1
2 2
2 3
3 3
3 2
3 1
3 0
So I came up with this solution:
for(i=0;i<4;i++) {
if(i == 1){
for(j=3;j>=0;--j) {
Serial.print(i);
Serial.print(" ");
Serial.println(j);
}
}else if(i == 3){
for(j=3;j>=0;--j) {
Serial.print(i);
Serial.print(" ");
Serial.println(j);
}
}else{
for(j=0;j<4;j++) {
Serial.print(i);
Serial.print(" ");
Serial.println(j);
}
}
}
This works but is there way to do this without if conditions inside the loops?
Here you are.
#include <stdio.h>
int main(void)
{
const int N = 3;
for ( int i = 0; i <= N; i++ )
{
for ( int j = 0; j <= N; j++ )
{
printf( "%d %d\n", i , i % 2 == 0 ? j : N - j );
}
}
return 0;
}
The program output is
0 0
0 1
0 2
0 3
1 3
1 2
1 1
1 0
2 0
2 1
2 2
2 3
3 3
3 2
3 1
3 0
Or the loops can be written like
const int N = 4;
for ( int i = 0; i < N; i++ )
{
for ( int j = 0; j < N; j++ )
{
printf( "%d %d\n", i , i % 2 == 0 ? j : N - j - 1 );
}
}
I am not the Arduino guy, but as its similar to C and this question is more about the algorithm let me sketch an approach in C:
#define LEFT_START (0)
#define LEFT_END (3)
#define RIGHT_START (0)
#define RIGHT_END (3)
enum DIRECTION {
DOWN = -1,
UP = 1
};
int main(void)
{
for (int direction = UP, i = LEFT_START, j = RIGHT_START - sign;
i <= LEFT_END;
++i, direction *= -1)
{
for (j += direction;
j >= RIGHT_START && j <= RIGHT_END;
j += direction)
{
printf("%d %d\n", i, j);
}
}
}
No if/then, not explicitly, nor implicitly via ternary-operator, BTW. ;)
I don't know what your purpose was, but you need to use if statement. This code will do the thing you want
int n =3;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
for (int i=0; i<=n; i++){
if(i%2 == 0){
for (int j=0; j<=n; j++){
Serial.print(i);
Serial.print(" ");
Serial.println(j);
}
}
else{
for (int j=n; j>=0; j--){
Serial.print(i);
Serial.print(" ");
Serial.println(j);
}
}
}
}
The output looks like this,
Defining variables/constant n, i, j is entirely up to you based on your project. This code is just a simple demonstration.
So, this is supposed to go through a file of 0's, ';'s and 1;s (all in character form originally but i set to integers), however when i run it the code counts the neighboring living cells wrong, pulling 3's in 5 spots where there should be a 2 for my neighbor count.
Current output;
Neighbor square for how many neighbors each cell has (counting all 8 surrounding tiles), then the updated matrix after basic game of life operations happen, lastly the original Matrix
This is the original matrix below
0;0;0;0;0;0
0;0;0;1;0;0
0;1;0;1;0;0
0;0;1;1;0;0
0;0;0;0;0;0
0;0;0;0;0;0
The rest of the process happens flawlessly, but the counting of neighbors doesn't work. What do i do wrong?
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void manage_board(FILE *input, int counter, int iterate);
int main (int argc, const char * argv[]){
FILE *input = fopen(argv[1],"r");
int counter = 0,temp1=0,temp2=0;
char temp = fgetc(input);
while (temp != EOF){
if (temp == '1' || temp == '0'){
counter+=1;
}
temp = fgetc(input) ;
}
counter = sqrt(counter);
rewind(input);
manage_board(input,counter, atoi(argv[2]));
fclose(input);
return 0;
}
void manage_board(FILE *input, int counter, int iterate){
int matrix[counter][counter];
FILE *output = fopen("output.csv","w");
int original[counter][counter];
int updated[counter][counter];
int rows = 0,cols = 0, neighbors =0;
char temp = fgetc(input);
/*for (int i=0; i<counter; i++){
for (int j=0; j<counter; j++){
updated[i][j] = 0;
}
printf ("\n");
}*/
while (temp != EOF){
//printf("%c",temp);
if (temp == '1' && temp != ';'){
matrix[cols][rows] = 1;
rows++;
}
else if (temp == '0' && temp != ';'){
matrix[cols][rows] = 0;
rows++;
}
if (rows==6){
cols++;
rows=0;
}
temp = fgetc(input);
}
for (int i=0; i<counter; i++){
for (int j=0; j<counter; j++){
updated[i][j] = 0;
}
//printf ("\n");
}
for (int i=0; i<counter; i++){
for (int j=0; j<counter; j++){
original[i][j] =matrix[i][j];
}
//printf ("\n");
}
for (int loops = 0; loops < iterate;loops++){
for (int i=0; i<counter; i++){
for (int j=0; j<counter; j++){
if ( i !=counter-1){ //down one
if(matrix[i+1][j] == 1){
neighbors ++;
}
}
if ( i !=0){//up one
if(matrix[i-1][j] == 1){
neighbors++;
}
}
if ( j != counter-1){//right one
if (matrix[i][j+1] == 1){
neighbors++;
}
}
if (j !=0 ){//left one
if (matrix[i][j-1] == 1 ){
neighbors++;
}
}
if (i !=counter-1 && j != counter-1){//bot right
if(matrix[i+1][j+1] == 1)
neighbors++;
}
if (j != counter-1 && i !=0 ){//top right
if(matrix[i-1][j+1] == 1){
neighbors++;
}
}
if (j !=0 && i !=0){//top left
if (matrix[i-1][j-1] == 1){
neighbors++;
}
}
if (i !=counter-1 && j !=0 ){//bottom left
if (matrix[i+1][j-1] == 1){
neighbors++;
}
}
///printf("%d", matrix[i][j]);
//printf ("%d ",neighbors);
if (neighbors < 2 ){
updated[i][j]=0;
}
else if (neighbors == 2 && matrix[i][j] == 1)
updated[i][j]=1;
else if (neighbors > 3){
updated[i][j]=0;
}
else if (neighbors = 3){
updated[i][j]=1;
}
printf("%d ",neighbors);
neighbors = 0;
}
printf("\n");
}
for (int i=0; i<counter; i++){
for (int j=0; j<counter; j++){
matrix[i][j]= updated[i][j];
printf("%d",updated[i][j]);
updated[i][j] = 0;
}
printf("\n");
}
}
printf("original board\n");
for (int i=0; i<counter; i++){
for (int j=0; j<counter; j++){
printf("%d", original[i][j]);
}
printf ("\n");
}
//printf(" \nnew matrix\n\n");
/* for (int i=0; i<counter; i++){
for (int j=0; j<counter; j++){
if (updated[i][j] == 1){
fprintf( output, "%d %d \n", i, j);
//printf(updated)
}
}
}
*/
/* A live cell with fewer than two live neighbors dies.
A live cell with more than three live neighbors also dies.
A live cell with exactly two or three live neighbors lives.
A dead cell with exactly three live neighbors becomes alive.*/
}
Here is my output
0 0 1 1 1 0
1 1 3 1 3 0
1 1 5 3 3 0
1 3 3 2 3 0
0 1 3 3 1 0
0 0 0 0 0 0
000000
001010
000110
011110
001100
000000
original board
000000
000100
010100
001100
000000
000000
The neighbor matrix should appear
0 0 1 1 1 0
1 1 3 1 2 0
1 1 5 3 3 0
1 2 3 2 2 0
0 1 2 2 1 0
0 0 0 0 0 0
There are a few errors:
1.
First of all you only handle a fixed number of columns:
if (rows==6){
cols++;
rows=0;
}
This must be
if (rows==counter){
cols++;
rows=0;
}
You start a new line after reading 6 values. For this purpose you have an extra parameter counter. Remember: You don't have a fixed size.
If your input is not a 6*6 field, you will put the read values in wrong positions.
2.
Apart from that your initial array matrix is not initialized and contains garbage values. This clearly can mess up your result.
Solution is easy:
Use memset or create a tiny loop to set everything to 0.
3.
For each character you increment rows which is wrong. You switched rows and columns. But as you only accept square input fields, this does ont matter.
4.
Finally your problem:
Your counting is correct, but during taking actions you destroy the value of neighbors.
else if (neighbors = 3){
updated[i][j]=1;
}
There should be a ==
Your compiler should give a warning about this.
I'm trying to write a program to display Pascal's triangle up to a user-inputted number of levels. We aren't allowed to use the factorial method. My current code is this:
#include <stdio.h>
void trianglePrint(const int numLevels);
int main() {
int numLevels;
printf("Please enter how many levels of Pascal's Triangle you would
like to see: ");
scanf("%d", &numLevels);
trianglePrint(numLevels);
return 0;
}
void trianglePrint(const int numLevels) {
int pascalTriangle[28][28];
int i, j;
for (i = 0; i < numLevels; ++i) {
for (j = 0; j <= i; ++j) {
if (i == 0 || i == 1 || j == 0 || j == numLevels) {
pascalTriangle[i][j] = 1;
printf("%d ", pascalTriangle[i][j]);
}
else {
pascalTriangle[i][j] = pascalTriangle[i - 1][j - 1] +
pascalTriangle[i - 1][j];
printf("%d ", pascalTriangle[i][j]);
}
}
printf("\n");
}
}
We're only supposed to be able to go up to 28 levels, which is why I am using an array of size 28 in both dimensions.
This works fine for about 6 levels of the triangle, but for larger levels it gives really large integers. I assumed it was due to uninitialized arrays, but I'm not sure. Does anyone know where the error is?
If you change
if (i == 0 || i == 1 || j == 0 || j == numLevels)
to
if (i == 0 || i == 1 || j == 0 || j == i)
(thanks to Melpomene), then all accesses to your array end up on already intiailised members.
That solves the strange numbers.
Output:
Please enter how many levels of Pascal's Triangle you would like to see: 6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Process returned 0 (0x0) execution time : 2.264 s
Press any key to continue.
Note:
Also initialising an array is a wise precaution. You could initialise with values which help finding an error, instead of hiding it, e.g. 42.
The problem is that you haven't set pascalTriangle[i - 1][j] before you use it to compute pascalTriangle[i][j] in the else clause.
Try the code
void Pascal(int n)
{
int arr[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= i; j++)
{
// First and last values in every row are 1
if (i == j || j == 0)
arr[i][j] = 1;
else // Other values are sum of values just above and left of above
arr[i][j] = arr[i-1][j-1] + arr[i-1][j];
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
I have an assignment which consists of creating a piece of code that generates and stores values in the shape:
1 1 0 1 1
1 0 0 0 1
0 0 0 0 0
1 0 0 0 1
1 1 0 1 1
I may use only arrays and loops. It's no necessary to print the array but I'm printing it just to check if I've done correctly. In my head this should work, but it returns garbage. The array is two dimensional, must have equal odd numbered sizes. I have literally less than one week of experience with any sort of programing.
main(){
int number;
printf("PLEASE GIVE ME A EVEN NUMBER FROM 3 TO 20\n");
scanf("%d", &number);
if(number%2==0)
{
printf("THIS WON'T FIT\n");
return 0;
}
int array1[number][number];
int lin, col, zero=1, count;
int ones=(number-1)/2;
for(lin=0;lin<number;lin++)
{
for(col=0;col<number;col++)
{
for(count=1; count<=ones;count++)
{
array1[lin][col]=1;
for(count=1;count<=zero;count++)
{
array1[lin][col]=0;
}
}
}
--ones;
zero+=2;
}
for(lin=0;lin<number;lin++)
{
for(col=0;col<number;col++)
printf("%d", array1[lin][col]);
printf("\n");
}
}
#include <stdio.h>
#include <stdlib.h>
int main(void){
int number;
printf("PLEASE GIVE ME A ODD NUMBER FROM 3 TO 19\n");
scanf("%d", &number);
if(number%2==0 || number < 3 || number > 19){
printf("THIS WON'T FIT\n");
return 0;
}
int center = number / 2;
int array[number][number];
for(int i = 0; i < number; ++i){
for(int j = 0; j < number; ++j){
array[i][j] = abs(i-center)+abs(j-center) > center;
printf("%d ", array[i][j]);
}
puts("");
}
return 0;
}
I am trying to solve this Queen problem of placing 4 queen in 4x4 matrix . I know it can be solved with backtracking algorithm . However i have not studied that and i am trying to solve it with my current knowledge . So what i am trying is to generate all possible combination of Queen in 4x4 matrix and print only one which cannot cancel each other .
1) For generating all combination , i am using rand function .
However there is obviously fault in my above coding . There are some outputs with only three '1' instead of four '1' . I am not able to eliminate this problem .
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
main()
{
srand(time(NULL));
int ar[30][30], i , j , a , b , c = -1, d = -1, k = 0;
while (1)
{
for (i = 0 ; i < 4 ; i++)
{
for (j = 0 ; j < 4 ; j++)
{
ar[i][j] = 0;
}
}
for (i = 0 ; i < 2 ; i++)
{
for (j = 0 ; j < 2 ; j++)
{
a = rand() % 3 ;
b = rand() % 3 ;
if (a != c || b != d)
{
ar[a][b] = 1 ; // here 1 = Queen
c = a ;
d = b;
}
}
}
}
}
2) Also is there any way i can reduce the time complexity using only these method ?
Instead of using temporary variables to check whether the array is filled, use the array itself!
for (i = 0 ; i < 2 ; i++)
{
for (j = 0 ; j < 2 ; j++)
{
a = rand() % 3 ;
b = rand() % 3 ;
if (ar[a][b] == 0)
{
ar[a][b] = 1 ; // here 1 = Queen
}
}
}
Your problem is that the inner loop will execute 4 times and you can only control 1 repeat with variables c and d.
Let's say a is 1 and b is 1: you make c = 1 and d = 1.
then a is 2 and b is 1 ... making c = 2 and d = 1.
then if a is 1 and b is 1 again, you cannot check for duplicate.
(1) You check only that a queen isn't placed on the same square as the last queen you placed. Remove the variables c and d and check whether ar[a][b] is still zero.
(2) Your scatter approach will produce many set-ups that are misses. Especially, because you don't enforce that ther cannot be any queens on the same rank and file. (In addition, rand() % 3 produces random values from 0 to 2, inclusively. You will never get a non-threatening configuration that way.)
If you want to use your random (bogosort) approach, you could use a one-dimensional array where the index is the rank and the number is the file where a queen is. Then you start with:
int queen[4] = {0, 1, 2, 3};
and shuffle the array. For 4 queens, that will yield 4! = 24 possibile configurations. You could try to iterate through them systematically.
The following is the brute force, backtracking code for the 8 queens problem, asked some time ago. Just change 8 to 4:
int checkBoard(int board[8][8]);
int putQueens(int board[8][8], int nQueens);
void printBoard(int board[8][8]);
int eightQueens(void)
{
int board[8][8];
memset(board, 0, sizeof(int)*64);
if (putQueens(board, 0)) {
printBoard(board);
return (1);
}
return(0);
}
int putQueens(int board[8][8], int nQueens)
{
int i, j;
for (i=0; i<8; i++) {
for (j=0; j<8; j++) {
if (board[i][j]==0) {
board[i][j]= 1;
if (checkBoard(board)) {
if (nQueens==7) return(1);
if (putQueens(board, nQueens+1)) return(1);
}
board[i][j]= 0;
}
}
}
return(0);
}
int checkBoard(int board[8][8])
{
int i, j;
for (i=0; i<8; i++) {
for (j=0; j<8; j++) {
if (board[i][j]) {
int ii, jj;
for (ii=i+1; ii<8; ii++) {
if (board[ii][j]) return(0);
}
for (jj=j+1; jj<8; jj++) {
if (board[i][jj]) return(0);
}
for (ii=i+1, jj=j+1; ii<8 && jj<8; ii++, jj++) {
if (board[ii][jj]) return(0);
}
for (ii=i-1, jj=j-1; ii>0 && jj>0; ii--, jj--) {
if (board[ii][jj]) return(0);
}
for (ii=i-1, jj=j+1; ii>0 && jj<8; ii--, jj++) {
if (board[ii][jj]) return(0);
}
for (ii=i+1, jj=j-1; ii<8 && jj>0; ii++, jj--) {
if (board[ii][jj]) return(0);
}
}
}
}
return (1);
}
void printBoard(int board[8][8])
{
int i,j;
printf(" ");
for (j=0; j<8; j++) printf(" %1d",j+1); printf("\n");
for (i=0; i<8; i++) {
printf("%1d ",i+1);
for (j=0; j<8; j++)
printf(" %1c", board[i][j]==0? '-':'*');
printf("\n");
}
}