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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "stdafx.h"
#include <iostream>
#include <random>
#include <ctime>
#include <fstream>


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
closed account (SECMoG1T)
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for(int i=1;i<N-1;i++)
    {
        for(int j=1;j<N-1;j++)
        {
           /// those cell with either no 
           ///left||right||upper||bottom {i.e cells on the edges} neigbours don't satisfy 
           ///your conditions and will be lost

           //you can now safely check for those conditions given by @KBW
           //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.
         }
        
    }
Last edited on
TY.
Topic archived. No new replies allowed.