### Creating condition.

Hello.

i have this assignment.

1]create a matrix with a given dimension NxN
2]create a function that will fill the matrix with random boolean values
3]write into a text file the coordinates of all cells that satisfy all the
following conditions:
The upper neighbor is TRUE
The left neighbor is FALSE
The right neighbor is FALSE iff (= if and only if) the bottom neighbor is
TRUE, or vice versa.
4] read the text file and restore the matrix.

All ready complete topic 1 and two.

I need help with 3.
how to create condition.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091`` ``````#include "stdafx.h" #include #include #include #include using namespace std; int** createMatrix(const unsigned int N); void deleteMatrix(int **matrix, const unsigned int N); int** fillMatrixWithTrureOrFalse(int **matrix, const unsigned int N); void writeInToFile(int **matrix, const unsigned int N); int main() { int **matrix; unsigned int N; cout << "Enter size of matrix: "; cin >> N; matrix = createMatrix(N); matrix = fillMatrixWithTrureOrFalse(matrix, N); writeInToFile(matrix, N); for (unsigned int i = 0; i < N; i++) { for (unsigned int j = 0; j < N; j++) { cout << matrix[i][j] << " "; } cout << endl; } deleteMatrix(matrix, N); return 0; } int** createMatrix(const unsigned int N) { int **matrix; matrix = new int*[N]; for (unsigned int i = 0; i < N; i++) { matrix[i] = new int[N]; } return matrix; } void deleteMatrix(int **matrix, const unsigned int N) { for (unsigned int i = 0; i < N; i++) { delete[] matrix[i]; } delete[] matrix; } int** fillMatrixWithTrureOrFalse(int **matrix, const unsigned int N) { random_device rd; mt19937 mt(rd()); uniform_real_distribution<> dist(0,2); for (unsigned int i = 0; i < N; i++) { for (unsigned int j = 0; j < N; j++) { matrix[i][j] = dist(mt); } } return matrix; } void writeInToFile(int **matrix, const unsigned int N) { ofstream file; file.open("C:/Users/PC/source/repos/ukol/souradnice.txt"); if (file.is_open()) { for (unsigned int i = 0; i < N; i++) { for (unsigned int j = 0; j < N; j++) { file << matrix[i][j] << " "; } file << endl; } } else cout << "Unable to open file"; file.close(); }``````
The upper neighbour of cell[i][j] is cell[i - 1][j]. i is zero if you're at the top.

Similarly, the left neighbour of cell[i][j] is cell[i][j - 1]. j is zero if you're at the left-most cell.
Ty.
And there is no problem for looking somthing that is out of bond ? i mean c is in 0 and im lookin in c -1 which is not in array.
same for j.
Last edited on
for you to avoid violating the bounds then you need to limit your loops,

if i == x coord and j == y coord then your working loops should be

 ``12345678910111213141516`` ``````for(int i=1;i
Last edited on
TY.
Topic archived. No new replies allowed.