i need to input 6 on 6 matrix and to out put the location of the
cell that all the cells around him has the value 0.
let's say:
1 1 1 1 1 1
1 1 1 1 1 1
0 0 1 1 1 1
2 0 1 1 1 1
0 0 1 1 1 1
1 1 1 1 1 1
the out put will be row 3 col 0
this is the code i made.. how i solve this ?
i tried a lot please help me
#include<stdio.h>
#define N 6
int main()
{
int arr[N][N] = { 0 }, i = 0, j = 0, x = 0, y = 0, counter = 0;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)//input//
{
scanf("%5d", &arr[i][j]);
}
printf("\n");
}
for (i = 0; i < N; i++)//output//
{
for (j = 0; j < N; j++)
{
printf("%5d", arr[i][j]);
}
printf("\n");
}
for (i = 0; i < N; j++)
{
for (j = 0; j < N; j++)
{
for (x = i - 1; x <= i + 1; x + 2)
{
for (y = j - 1; y <= j + 1; y + 2)
{
if (x >= 0 && x < N &&y >= 0 && y < N&&arr[x][y] == 0)
{
counter++;
}
}
}
if (i == 0 && j == 0 || i == 0 && j == N - 1 || i == N - 1 && j == 0
|| i == N - 1 && j == N - 1)
{
if (counter == 3)
{
printf("the row is %d the col is %d\n", i, j);
}
}
else if (i == 0 && j >= 1 && j < N - 1 || i == N - 1 && j >= 1 && j
< N - 1 && j == 0 && i >= 1 && i <= N - 1 && j == N - 1 && i >= 1 && i<N -1)
{
if (counter == 5)
{
printf("the row is %d the col is %d\n ", i, j);
}
}
else
{
if (counter == 8)
{
printf("the row is %d the col is %d\n ", i, j);
}
}
}
}
}
There are problems with your x and y loops:
for (x = i - 1; x <= i + 1; x + 2)
{
for (y = j - 1; y <= j + 1; y + 2)
{
You aren't incrementing x or y in these loops. The expression x + 2 merely evaluates the value x + 2. It doesn't do anything with it. If you want to actually set x to x + 2, then you need to use x = x + 2, or more concisely, x += 2.
Incrementing x and y by 2 is incorrect. It will only examine 4 points: (i-1,i-1), (i-1,i+1), (i+1,i-1), and (i+1,i+1). It will skip the following 4 points: (i-1,i), (i,i-1), (i,i+1), (i+1,i). You need to increment x and y by 1 each time, i.e. use x++ and y++ in the loop incremement instead of adding 2. The, inside the loop, add an additional test for x == i && y == i and skip that point (the center point).
I believe this is what you want (note that you can do eveything in two loops if you nest the conditionals in a smart way to avoid checking areas outside the matrix):
#include <stdio.h>
#define ROWS 5
#define COLS 5
void printBlockeds(int M[ROWS][COLS])
{
int i, j;
for(i = 0; i < ROWS; ++i)
{
for(j = 0; j < COLS; ++j)
{
int isBlocked = 1;
if(i > 0)
{
isBlocked = isBlocked && !M[i-1][j];
if(j > 0) isBlocked = isBlocked && !M[i-1][j-1];
if(j < COLS-1) isBlocked = isBlocked && !M[i-1][j+1];
}
if(j > 0) isBlocked = isBlocked && !M[i][j-1];
if(j < COLS-1) isBlocked = isBlocked && !M[i][j+1];
if(i < ROWS-1)
{
isBlocked = isBlocked && !M[i+1][j];
if(j > 0) isBlocked = isBlocked && !M[i+1][j-1];
if(j < COLS-1) isBlocked = isBlocked && !M[i+1][j+1];
}
if(isBlocked) printf("(%d, %d)\n", i, j);
}
}
}
int main()
{
int M[ROWS][COLS] = {{1, 1, 1, 0, 1},
{1, 0, 0, 0, 0},
{1, 0, 1, 0, 1},
{1, 0, 0, 0, 1},
{1, 0, 1, 0, 1}};
printBlockeds(M);
return 0;
}
Output for the case above:
(0, 4)
(2, 2)
(4, 2)
Related
I have a problem in my C program. I need to write a histogram of numbers. If the number on the input will be outside the interval [1, 9], consider such a number as the value 1. I don't understand why it doesn't work.
#include <stdio.h>
#include <stdlib.h>
void printHistogram_vertical(int *hist, int n);
int main()
{
int i, j;
int inputValue;
scanf("%d", &inputValue);
int hist[inputValue];
for (i = 0; i < inputValue; ++i)
{
scanf("%d", &hist[i]);
}
int results[10] = {0};
for (i = 0; i < 10; ++i)
{
for (j = 0; j < inputValue; ++j)
{
if (hist[j] >= 10 && hist[j] < 1)
{
results[j] == 1;
}
if (hist[j] == i)
{
results[i]++;
}
}
}
return 0;
}
void printHistogram_vertical(int *hist, int n)
{
int i, j;
for (i = 1; i < n; i++)
{
printf(" %d ", i);
for (j = 0; j < hist[i]; ++j)
{
printf("#");
}
printf("\n");
}
}
Input:
9
3 3 2 3 7 1 1 4 10
My Output:
1 ##
2 #
3 ###
4 #
5
6
7 #
8
9
The correct output:
1 ###
2 #
3 ###
4 #
5
6
7 #
8
9
If the number is bigger than 10 and smaller than 1 it should count this number as 1. I write this function:
for (i = 0; i < 10; ++i)
{
for (j = 0; j < inputValue; ++j)
{
if (hist[j] >= 10 && hist[j] < 1)
{
results[j] == 1;
}
if (hist[j] == i)
{
results[i]++;
}
}
}
There are 2 problems with following condition:
if (hist[j] >= 10 && hist[j] < 1)
{
results[j] == 1;
}
The comparison is broken. Value cannot be above 9 AND below 1 at the same time. It should be OR instead.
What should be increment of index 1, is actually comparison == of wrong index.
Replacement:
if (hist[j] >= 10 || hist[j] < 1)
{
results[1]++;
}
But double for loop construction is more complicated than it needs to be. It could be replaced with single for loop:
for (j = 0; j < inputValue; ++j) {
int value = hist[j];
if(value >= 1 && value <= 9) {
results[value]++;
}
else {
results[1]++;
}
}
My task is to draw a coordinate system and line y+x=n, for n<=10.My code gives the correct result, but there is a condition that spaces or any other signs cannot be drawn after the line x+y=n. That is the only problem I have in solving this task and I hope you could help. I am a beginner.
(The only problem is that my code prints spaces above the line x+y=n and it should not)
#include <stdio.h>
int
main()
{
int n, i, j;
scanf("%d", &n);
char m[12][63] = { " " };
for (i = 0; i < 12; i++) {
for (j = 0; j < 63; j++) {
m[i][j] = ' ';
if (i == 0 && j == 1)
m[i][j] = '0';
if (i == 11 && j == 61)
m[i][j] = '2';
if (j == 0 && i != 0 && i != 11)
m[i][j] = '0' + 10 - i;
if (j == 2 && i != 11)
m[i][j] = '+';
if (i == 11 && j % 3 == 2)
m[i][j] = '0' + ((j - 2) / 3) % 10;
if (i == 11 && j % 3 == 1 && j > 29 && j < 59)
m[i][j] = '1';
if (i == 10 && j % 3 == 2)
m[i][j] = '+';
}
}
for(i=1;i<n+1;i++){
if((10-n+i)!=10 && (2+3*i)!=1)
m[10-n+i][2+3*i]='*';
}
m[0][0]='1';
for (i = 0; i < 12; i++) {
for (j = 0; j < 63; j++) {
printf("%c", m[i][j]);
}
printf("\n");
}
}
//n=5
10+
9 +
8 +
7 +
6 +
5 +
4 + *
3 + *
2 + *
1 + *
0 + + + + + + + + + + + + + + + + + + + + +
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Sounds like you want to not print the trailing spaces on each line. So you need to find and skip those trailing spaces rather than printing them:
for (i = 0; i < 12; i++) {
int eol = 63;
while (eol > 0 && m[i][eol-1] == ' ') --eol;
for (j = 0; j < eol; j++) {
printf("%c", m[i][j]);
}
printf("\n");
}
#include<stdio.h>
int main() {
int x = 0, y = 0;
int maze1[4][4] = { { 1, 0, 0, 0 }, { 1, 1, 1, 1 }, { 0, 1, 0, 0 }, { 1, 1, 1, 1}}; // The maze which is should be solved.
int maze[4][4];
for (int k = 0; k < 4; k++) { //Creating 4*4 matrix
for (int l = 0; l < 4; l++) {
maze[k][l] = 0;
}
}
printf("\n\n\n");
sol(maze, x, y, maze1);
for (int k = 0; k < 4; k++) {
for (int l = 0; l < 4; l++) {
printf("%3d", maze[k][l]);
}
printf("\n");
}
return 0;
}
int sol(int maze[4][4], int x, int y, int maze1[4][4]) {
if (x < 4 && x >= 0 && y >= 0 && y < 4 && maze[x][y] == 0 && maze1[x][y] == 1) {
maze[x][y] = 1;
if (x == 3 && y == 3) {
return 0;
}
if (sol(maze, x + 1, y, maze1) == 1) //How does it works ?
return 0;
if (sol(maze, x, y + 1, maze1) == 0) // How does it works ?
return 0;
else {
maze[x][y] = 0;
return 0;
}
}
}
The lines you marked are simple functions calls. The fact that the function called is the one currently executing is not a problem. This is called a recursive function call. You can read about recursion on Wikipedia and elsewhere. The recursive call gets its own parameters and local variables.
For example, say you wanted to calculate the sum of all the non-negative integers up to a given integer. Well, you could define that as follows:
int sum_up_to(int i) {
if (i <= 0)
return 0;
else
return i + sum_up_to(i-1);
}
So,
sum_up_to(0) returns 0.
sum_up_to(1) returns 1 + sum_up_to(0), which is 1 + 0, which is 1.
sum_up_to(2) returns 2 + sum_up_to(1), which is 2 + 1, which is 3.
sum_up_to(3) returns 3 + sum_up_to(2), which is 3 + 3, which is 6.
sum_up_to(4) returns 4 + sum_up_to(3), which is 4 + 6, which is 10.
My function:
int checkSE(disk board[][SIZE], disk hypotheticalDisk)
{
int i;
int j;
int row;
int col;
int player;
int opponent;
int checkSEflag;
player = hypotheticalDisk.type;
(player == 0) ? (opponent = 1) : (opponent = 0);
row = hypotheticalDisk.pos.row;
col = hypotheticalDisk.pos.col;
checkSEflag = 0;
for (i = row + 2, j = col + 2; ((i < SIZE) && (j < SIZE) && (checkSEflag == 0)); i++, j++)
{
if (board[i][j].type == player)
{
for (--i, --j; board[i][j].type == opponent; i--, j--)
{
if (i == row && j == col)
{
checkSEflag = 1;
break;
}
}
}
printf("\n%d and %d and %d", i, j, checkSEflag);
}
return checkSEflag;
}
My output:
2 and 3 and 0
2 and 3 and 0
2 and 3 and 0
2 and 3 and 0
2 and 3 and 0
.
.
.
And it keeps on going...
I want both i and j to increase until they are equal to SIZE (SIZE predefined to be 8) or until checkSEflag is assigned to be equal to 1.
It looks like the values of i and j just aren't being changed...
I tried taking them out of the loop conditions and instead placed them
in the loop body, though that didn't change anything.
I doubt the post increment operators just decided to not work so I must be doing something wrong, any ideas of what that may be?
These two lines:
for(i = row+2, j = col+2; ((i < SIZE) && (j <SIZE) && (checkSEflag == 0)); i++, j++)
...
for(--i, --j; board[i][j].type == opponent; i--, j--)
so, you are both incrementing and decrementing (i,j); try sprinkling printfs around these and see if you are both incrementing and decrementing i,j on each iteration...
I can't really see what's wrong with my check neighbors function in my game of life. It checks all the 8 neighbours then depending on the living count assigns the cell being checked to living or dead, then updates the board.
void check_neighbours (int board[][COL])
{
int living = 0, i, j, k, l;
int new_board[ROW][COL];
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
if ((board[i + 1 % ROW][j % COL]) == '#')
{
living++;
}
if ((board[i - 1 % ROW ][j % COL]) == '#')
{
living++;
}
if ((board[i % ROW][j + 1 % COL]) == '#')
{
living++;
}
if ((board[i % ROW][j - 1 % COL]) == '#')
{
living++;
}
if ((board[i + 1 % ROW][j + 1 % COL]) == '#')
{
living++;
}
if ((board[i - 1 % ROW ][j + 1 % COL ]) == '#')
{
living++;
}
if ((board[i + 1 % ROW ][j - 1 % COL ]) == '#')
{
living++;
}
if ((board[i - 1 % ROW ][j - 1 % COL ]) == '#')
{
living++;
}
if (living == 3)
{
new_board[i][j] = '#';
}
if (living <= 2)
{
new_board[i][j] = '-';
if (living < 3)
{
new_board[i][j] = '-';
}
}
}
for (k = 0; k < ROW; k++)
{
for (l = 0; l < COL; l++)
{
board[k][l] = new_board[k][l];
}
}
}
}
edit: Added parentheses but still prints the same
Try sticking some brackets into your math:
board[(i + 1) % ROW]
is much safer than
board[i + 1 % ROW]
Test edge conditions before doing the living count.
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
int living = 0;
for (int drow = -1; drow <= 1; drow++) {
int row = i+drow;
if (row < 0 || row >= ROW) continue; // over the edge
for (int dcol = -1; dcol <= 1; dcol++) {
int col = j+dcol;
if (col < 0 || col >= COL) continue; // over the edge
if (row==0 && col==0) continue;
if (board[row][col] == '#') {
living++;
}
} // endfor dcol
} // endfor drow
// This section may need review - GOL rules are unclear in post -see below
if (living == 3) {
new_board[i][j] = '#';
}
...
} // endfor j
} // endfor i
If rules follow:
Any live cell with fewer than two live neighbors dies, as if caused by under-population.
Any live cell with two or three live neighbors lives on to the next generation.
Any live cell with more than three live neighbors dies, as if by over-population.
Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
#define ALIVE ('#')
#define DEAD ('-')
#define POP_MIN 2
#define POP_MAX 3
#define POP_REPRO 3
new_board[i][j] = board[i][j];
if (board[i][j] == ALIVE) {
if (living < POP_MIN) new_board[i][j] = DEAD;
else if (living > POP_MAX) new_board[i][j] = DEAD;
} else {
if (living == POP_REPRO) new_board[i][j] = ALIVE;
}
Isn't this:
board[i + 1 % ROW]
going out of bounds? Try fixing these issues.
As Paul R suggested, you could use parentheses, in order to catch up with the higher precedence of % operator in comparison to the + operator. So change the code to this:
board[(i + 1) % ROW]
Assuming ROW = 5 and i = 4, you get:
(4 + 1) % 5 = 0 // yeah!
4 + 1 % 5 = 5 // oh dear...