I get: 'unexpected type
required: variable
found : value' on the marked lines (*)
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < boardSize; j++) {
rows[i].getSquare(j) = matrix[i][j]; // * points to the ( in (j)
columns[j].getSquare(i) = matrix[i][j]; // * points to the ( in
int[] b = getBox(i, j);
int[] boxCord = getBoxCoordinates(i + 1, j + 1);
boxes[b[0]][b[1]].getSquare(boxCord[0], boxCord[1]);
}
}
This is my Row class:
private Square[] row;
Row(int rowCount) {
this.row = new Square[rowCount];
}
public Square getSquare(int index) {
return this.row[index];
}
Please help me out by pointing out what I'm doing wrong here.
Thanks in advance.
You cannot assign something to the return value of a method. Instead, you need to add a setSquare() method to the Row class:
public Square setSquare(int index, Square value) {
this.row[index] = value;
}
and use it like this:
rows[i].setSquare(j, matrix[i][j]);
Java doesn't have pointers - references are not the same thing.
It's impossible to tell what's really going on based on the code you posted. I think you need a method to set the value of that Square in the private array your Row class owns.
public void setSquare(int index, Square newSquare) {
this.row[index] = newSquare;
}
It looks like a poor abstraction, in any case.
Java has no pointers. Objects are passed and returned by reference. In terms of C++, rows[i].getSquare(j) is an rvalue, not an lvalue, so you cannot assign to it.
Instead, you should create and use rows[i].setSquare(...).
Related
I made a Matrix class in JAVA that consists of a 2D int array with row and column variables. The constructor of the class generates a Matrix of dimensions n x m and I also implemented two methods that print the values of the Matrix and its transpose (getMatrixValues and getTransposedValues).
However, I would like to make a function that takes as input a matrix and returns its transpose, but since this class is not an array I cannot iterate over it using AT[j][i] = A[i][j], if I understand the exception correctly that IntelliJ is returning me ("java: array required, but Matrix found").
Obviously, I could simply use an int[][] class to begin with instead of defining a new class, but since I am new to JAVA and object-oriented programming in general I wondered whether there is not another possibility without discarding my Matrix class?
Here is my code:
import java.util.Random;
import java.util.Arrays;
public class Matrix {
private int n; // rows
private int m; // cols
private int[][] A; // matrix
public static void main(String[] args){
Matrix A = new Matrix(4,4);
A.getMatrixValues();
System.out.println("\n");
A.getTransposedValues();
Matrix AT = transposeMatrix(A);
}
// constructor (randomly generates matrix)
public Matrix(int rows, int cols){
n = rows;
m = cols;
A = new int[rows][cols];
Random r = new Random();
// r.setSeed(1);
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols ; j++)
{
A[i][j] = r.nextInt(10);
}
}
}
// print matrix
public void getMatrixValues(){
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.print("\n");
}
}
// print transposed matrix
public void getTransposedValues(){
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[j][i]+"\t");
}
System.out.print("\n");
}
}
public static Matrix transposeMatrix(Matrix A){
Matrix AT = new Matrix(A.get_n(), A.get_m());
for(int i = 0; i < A.get_n(); i++)
{
for(int j = 0; j < A.get_m(); j++)
{
AT[j][i] = A[i][j];
}
}
return AT;
}
// getters
public int get_n(){
return n;
}
public int get_m(){
return m;
}
}
Preface
What I have noticed only afterwards was, that when creating the transposed matrix, you actually want to create one with m-rows and n-columns.
That means, you have mixed up m and n in transposeMatrix() when instantiating the new object.
That also means that your code -as it is- only works for square matrices. Just create the object like this: new Matrix(matrix.get_m(), matrix.get_n()).
Note: I re-named your variables; see below in section "Solution".
However, since this is not part of you question, I have not fixed it in the code-snippets below.
Scopes
When in every other method, you are in the lexical scope of the object you are in. This allows you to access its fields (like int[][] A).
But when inside transposeMatrix(Matrix A), you are inside the static scope, meaning, not in the scope of an object.
What adds to the confusion is, that your instance-variable is called A, much like the parameter Matrix A of transposeMatrix(). While you were able to access the 2D-array via A, you now access a Matrix-object via A. That is both because we are not in an object anymore, and because the new local variable overrides the access to the similarly named instance-/static-variable (you would have to use this.A or Matrix.A respectively).
Access modifier
When trying to fix your code, you will stumble upon the restriction of the access modifier you used: private.
private int[][] A will make A (your array) only accessible when referenced from inside your object. But when calling transposeMatrix(), we are in a static-context, meaning, not inside your object anymore.
To fix this, can change the access modifier to allow us to access that field from outside the object. To enable this, you can change the modifier to any other option, with the easiest being to just remove it. However, I suggest you to read more about Access Modifier in the official documentation.
Solution
Let's say we removed private from int[][] A. Will the code work?
No. That's because of the confusion I talked about when explaining scopes. To clear up the confusion, let's rename some variables: (Changed int[][] A to int[][] array, Matrix A to Matrix matrix, Matrix AT to Matrix matrixTransposed)
int[][] array; // <-- Notice the removed access modifier!
// ...
public static Matrix transposeMatrix(Matrix matrix){
Matrix matrixTransposed = new Matrix(matrix.get_n(), matrix.get_m());
for (int i = 0; i < matrix.get_n(); i++) {
for(int j = 0; j < matrix.get_m(); j++) {
matrixTransposed[j][i] = matrix[i][j]; // Won't work!
}
}
}
The code above is still faulty. That is -as we can clearly see now- because we are trying to access a Matrix-object as if it was an array. However, we need to access its instance variable. So, instead of accessing the array the wrong way, we add a .array after every Matrix-object where we try to access its array.
public static Matrix transposeMatrix(Matrix matrix){
Matrix matrixTransposed = new Matrix(matrix.get_n(), matrix.get_m());
for (int i = 0; i < matrix.get_n(); i++) {
for(int j = 0; j < matrix.get_m(); j++) {
matrixTransposed.array[j][i] = matrix.array[i][j]; // NOW we are accessing their arrays respectively
}
}
}
Another solution
With changing the access modifier of the array (like we did above), we enable one to not only override the values of the array, but also to override the array itself.
To restrict one from doing so, we can use "Getters and Setters". They are like a middle-man, allowing us to access the array only indirectly, but with that much control over it as we seem necessary.
We can define them simply creating two new methods (hence its name):
public int get(int i, int j) {
return array[i][j];
}
public void set(int i, int j, int value) {
array[i][j] = value;
}
As you see, we simply forward the request to the "middle-man", which handles it accordingly.
Note: We might encounter a Runtime Exception since we are not checking if the fields at the specified indices actually exist. You might want to add some if-statements before accessing them.
By using the getter and setter, we can modify the code to this:
private int[][] array; // We still want to restrict access to 'array'...
// ...
// ...but still allow accessing them, be it indirectly
public int get(int i, int j) {
return array[i][j];
}
public void set(int i, int j, int value) {
array[i][j] = value;
}
// Now using getter and setter
public static Matrix transposeMatrix(Matrix matrix) {
Matrix matrixTransposed = new Matrix(matrix.get_n(), matrix.get_m);
for (int i = 0; i < matrix.get_n(); i++) {
for (int j = 0; j < matrix.get_m; j++) {
matrixTransposed.set(j, i, matrix.get(i, j));
}
}
}
Sidenote
You should also take a look at naming conventions. They are not critical to make your code function, but make reading and understanding (and thus debugging) it a lot easier.
What I think is also good, is to take a look at a style-guide to see how you can make your code more readible with easy tricks, or just to have a consistent style across your code. I enjoy Google's style-guide for Java, however, there are a lot others as well.
And, you don't have to stick to an existing style-guide, you can have your own style, too! But try to be as consistent as possible. That makes it easier for others and yourself in the future when re-reading your code.
I need to create a method to sort an Object Array. I've never done this, but I must revise this for my course. I'm totally lost when it comes to implementing a sorting method. I need to sort using a insertion sort and selection sort.
This is my code I have so far in. All I must do is call the sort() when the user wishes to do so.
package citylisttest;
public class CityList {
private City[] city;
private Integer numberOfCities;
public CityList (Integer cityListSize){
this.city=new City[cityListSize];
this.numberOfCities=0;
}
public void addCity(String city){
this.city[this.numberOfCities]=new City(city);
this.numberOfCities++;
}
public String toString(){
String cityDetails=new String();
if (this.numberOfCities!=0){
cityDetails+=String.format("%-15s\n","CITY");
for(Integer i=0;i<this.numberOfCities;i++) {
cityDetails+=this.city[i]+"\n"; }
}
else
cityDetails+="City list is empty";
return cityDetails;
}
public void sort(){
}
}
First, I would suggest renaming the variable city to cities since it is an array, and it holds more than one city. In addition, also consider encapsulating your data by marking as private your instance variable and creating getters and setters respectively.
Let's say you want to sort them by number of cities in ascending order, then your sort method should have:
for (int i = 0; i < city.length - 1; i++) {
for (int j = i + 1; j < city.length; j++) {
if (city[i].getNumberOfCities() > city[j].getNumberOfCities()) {
City temp_city = city[i];
city[i] = city[j];
city[j] = temp_city;
}
}
}
I hope this helps, but you can implement the Comparable interface or create a Comparator class following this tutorial.
EDIT: If you want to use compareto, to sort city names in ascending order:
for (int i = 0; i < city.length - 1; i++) {
for (int j = i + 1; j < city.length; j++) {
if (city[i].getName().compareTo(city[j].getName()) > 1) {
City temp_city = city[i];
city[i] = city[j];
city[j] = temp_city;
}
}
}
Assumming x, and y are strings, x.compareTo(y) gives you:
a positive number if x > y
zero if x is equal to y
a negative number if x
Documentation about this topic is really common to find in internet, but "let me google that for you".
I would suggest to understand what you want to do. Therefore I would suggest you to take a look at first, what is a sorting algorithm:
https://en.wikipedia.org/wiki/Sorting_algorithm
and then, particularly to the insertion sort algorithm:
https://en.wikipedia.org/wiki/Insertion_sort
or the selection sort:
https://en.wikipedia.org/wiki/Selection_sort
Someone can give you the answer here, but if you do not struggle with the problem, you will not learn about it and you will forget soon about it.
Hope it helps :)
This looks very much like a homework question, as the common practice is not to create your own sort algorithm.
You'll get further by attempting to design your own solution, even if it's naive, than copy/pasting whatever answer you find here.
If you really want to explore the various possible solutions (with java source code) you can follow this applet and tutorial:
https://thomas.baudel.name/Visualisation/VisuTri/
Here are the codes. But before going there I think you should watch these two videos:
insertionSort: https://www.youtube.com/watch?v=DFG-XuyPYUQ&t=142s
selectionsort: https://www.youtube.com/watch?v=f8hXR_Hvybo
public static void insertionSort(Object[] data) {
// i denotes where the partition is
for (int i = 1; i < data.length; i++) {
// the key is to the right of the partition
Object key = data[i];
int j = i - 1; // use j to scan left to insert key
while (j >= 0 && ((Comparable) key).compareTo(data[j]) < 0) {
// shift item right to make room
data[j + 1] = data[j];
j--;
}
// Found the position where key can be inserted
data[j + 1] = key;
}
}
public static void selectionSort(Object[] data) {
for (int i = 0; i < data.length - 1; i++) {
// Find the index of the minimum item, starting at `i'.
int minIndex = i;
for (int j = i + 1; j < data.length; j++) {
if (((Comparable) data[j]).compareTo(data[minIndex]) < 0)
minIndex = j;
// Exchange with the first item (at `i'), but only if different
if (i != minIndex) {
Object tmp = data[i];
data[i] = data[minIndex];
data[minIndex] = tmp;
}
}
}
When I am creating an array of Example objects, I call something like initializeArray(); I use a simple nested for loop to traverse through the array and then assign new objects with values to each index of the array using exampleArr[i][j] = new Example(false, false, false, 0); however, calling this gives me an java.lang.ArrayIndexOutofBoundsException:0 at the line above.
I am assuming that I am instantiating the new object incorrectly, as this also happens in another method which is supposed to display all of the Example objects in the array. However, I will post the nested loop I am using in case there is something that i've done wrong that I can't see.
public void initializeArray(){
for(int i = 0; i < getRows(); i++){
for(int j = 0; j < getColumns(); j++){
tileArr[i][j] = new Tile(false, false, false, 0);
}
}
}
//Declaration of rows and columns
private int rows;
private int columns;
Tile[][] tileArr = new Tile[rows][columns];
public void setRows(int r)
{
rows = r;
}
public void setColumns(int c)
{
//various setters and getters for the array
columns = c;
}
public int getRows()
{
System.out.print(rows);
return rows;
}
public int getColumns()
{
System.out.print(columns);
return columns;
}
Thanks everyone for your help! The problem has been solved.
Declare your tileArr at the top but do not initialize.
Tile[][] tileArr;
Then initialize your array before your for loop in the initializeArray() (This is assuming your rows and columns is set. You can add logic to check this as well).
tileArr = new Tile[getRows()][getColumns()];
tileArr = new Tile[rows][columns]; //Do this instead if you don't want the print statements to be called
As #gonzo said you have got to initialize your array to allocate enough memory for all the positions you are going to be using.
Tile[][] tileArr;
public void initializeArray(){
Tile[][] tileArr = new Tile[getRows()][getColumns()];
for(int i = 0; i < getRows(); i++){
for(int j = 0; j < getColumns(); j++){
tileArr[i][j] = new Tile(false, false, false, 0);
}
}
return titleArr;
}
//...wherever you want it
this.tileArray = this.initializeArray()
But there are cases when you don't know how big this array may be. For those cases you should be using List<Tile> type like LinkedList<Tile> or ArrayList<Tile> so that you don't need to allocate space for every new position to use.
I'm trying to create a third sorted array, c, from the two previously created arrays, a and b; however, I'm getting several errors within the merge method that say "The type of the expression must be an array type but it resolved to OrdArray". I've been at it for hours already, and feel like my brain is mush now. Can someone help me out?
class OrdArray
{
private long[] a; // ref to array a
private int nElems; // number of data items
//-----------------------------------------------------------
public OrdArray(int max) // constructor
{
a = new long[max]; // create array a
nElems = 0;
}
//-----------------------------------------------------------
public int size()
{ return nElems; }
//-----------------------------------------------------------
public int find(long searchKey)
{
int lowerBound = 0;
int upperBound = nElems-1;
int curIn;
while (true)
{
curIn = (lowerBound + upperBound ) / 2;
if (a[curIn] == searchKey)
return curIn; // found it
else if (lowerBound > upperBound)
return nElems; // can't find it
else // divide range
{
if (a[curIn] < searchKey)
lowerBound = curIn + 1; // it's in upper half
else
upperBound = curIn - 1; // it's in lower half
} // end else divide range
} // end while
} // end find()
//-----------------------------------------------------------
public void insert(long value) // put element into array
{
int j;
for (j = 0; j < nElems; j++) // find where it goes
if (a[j] > value) // (linear search)
break;
for (int k = nElems; k > j; k--) // move bigger ones up
a[k] = a[k-1];
a[j] = value; // insert it
nElems++; // increment size
} // end insert()
//-----------------------------------------------------------
public boolean delete(long value)
{
int j = find(value);
if (j == nElems) // can't find it
return false;
else // found it
{
for (int k = j; k < nElems; k++) // move bigger ones down
a[k] = a[k+1];
nElems--; // decrement size
return true;
}
} // end delete()
//-----------------------------------------------------------
public void display() // displays array contents
{
for (int j = 0; j < nElems; j++) // for each element,
System.out.print(a[j] + " "); // display it
System.out.println("");
}
//-----------------------------------------------------------
public static long[] merge(OrdArray a, OrdArray b)
{
long[] c = new long[a.nElems + b.nElems];
int i = 0, j = 0, k = 0;
while (i < a.nElems && j < b.nElems)
{
if (a.data[i] < b.data[j])
c[k++] = a.data[i++];
else
c[k++] = b.data[j++];
}
while (i < a.nElems)
c[k++] = a.data[i++];
while (j < b.nElems)
c[k++] = b.data[j++];
return c;
}
} // end class OrdArray
////////////////////////////////////////////////////////////////
class OrderedApp
{
public static void main(String[] args)
{
int maxSize = 100; // array size
OrdArray a, b, c; // reference to array
a = new OrdArray(maxSize); // create the array
b = new OrdArray(maxSize);
c = new OrdArray(maxSize);
a.insert(11);
a.insert(13);
a.insert(15);
a.insert(17);
a.insert(19);
a.insert(21);
a.insert(23);
a.insert(25);
a.insert(27);
a.insert(29);
b.insert(12);
b.insert(14);
b.insert(16);
b.insert(18);
b.insert(20);
b.insert(32);
b.insert(24);
b.insert(26);
b.insert(28);
b.insert(30);
OrdArray.merge(a,b);
System.out.print("Array a: ");
a.display();
System.out.println();
System.out.print("Array b: ");
b.display();
System.out.println();
System.out.print("Array c: ");
c.display();
System.out.println();
} // end main()
}// end class OrderedApp
OrdArray is not an array type (despite the name); therefore, you can't index it like an array. This expression
a[i++]
where a is an OrdArray, will have no meaning. Java doesn't give you a way to define your own [] operator for classes (unlike C++). Therefore, you'll have to add a method to OrdArray to return the element at a given index, something like
public long get(int index) { ...write the code... }
a.get(i++) // will then get the element at that index
Although I'm not sure this is what you want, since you've declared c to be an int[] and the array in OrdArray to be a long[], so I'm not sure what you're trying to do.
EDIT: After reading your comment, I realized that the merge method is inside the OrdArray class. I missed that before. Since that's the case, you don't need to add a get method; you can access the private fields of your OrdArray parameters directly. In your method:
public void merge(OrdArray a, OrdArray b)
you want to get at the private array a that you declare for each OrdArray. If you just use a, the variable will refer to the OrdArray, which isn't an array (as described above); to get at the long[] a belonging to the OrdArray a, you need to say
a.a[i++]
and likewise, for b,
b.a[i++]
This can look confusing to a reader, so I suggest coming up with a better name so that you're not calling two things a. Perhaps data?
A couple other things: You use merge like this: c.merge(a,b), which means that merge is an instance method and c is the instance you're operating on. But your method doesn't do anything with the current instance. (The c you declare in merge is a local variable that has nothing to do with the c you use when calling merge.) Right now, your method is going to a lot of trouble to construct the local array c, but then it just throws it away. You either need to (1) fix the method so that it sets up the a (or data) array in the current instance; or (2) make it a static method and make the method return the new array as a function result. I'm not sure which one your instructor wants you to do.
I'm not exactly sure what you are trying to do. But to resolve error, i have corrected the articular block.
To note, OrdArray class is not an array. It's a class that has a long[] a. So you need to get the array like any other property from the object.
For betterment, please change the method signature like this:
public void merge(OrdArray ordArr1, OrdArray ordArr2) {//Note parameters' name change
.
.
.
while (i < ordArr1.nElems && j < ordArr2.nElems)
{
if (ordArr1.a[i] < ordArr2.a[j]) //should resolve
c[k++] = ordArr1.a[i++];
else
c[k++] = ordArr2.a[j++];
}
while (i < a.nElems)
c[k++] = ordArr1.a[i++];
while (j < b.nElems)
c[k++] = ordArr2.a[j++];
}
If you accept solution wit Lists it would be:
List<Integer> result = new ArrayList<Integer>(Arrays.asList(sourceArray));
result.addAll(Arrays.asList(secondSourceArray));
Collections.sort(result);
You can optionally convert it back to array with
result.toArray();
I am confused why you are using binary search. Simple way is to insert two arrays using two insert methods or one. Using a merge method, just merge those two already sorted arrays by comparing the least element among two sorted arrays.
Remove delete, search etc methods, they are not required.
This is my code. I have inserted two integer arrays(elements) into inserta() and insertb() sorted them and merged them using insert() method. Finally I have this sorted array after merging them. Please see my code here:
package sample;
/**
*
* #author Shivasai
*/
public class Merge {
int i;
int j;
int k;
int n;
int m;
int p;
private long[] a;
private long[] b;
private long[] c;
public Merge()
{
a=new long[10];
b=new long[10];
c=new long[100];
n=0;
m=0;
p=0;
}
void inserta(long key)
{
for(i=0;i<n;i++)
{
if(a[i]>key)
break;
}
for(j=n;j>i;j--)
{
a[j]=a[j-1];
}
a[j]=key;
n++;
}
void insertb(long value)
{
for(i=0;i<m;i++)
{
if(b[i]>value)
break;
}
for(j=m;j>i;j--)
{
b[j]=b[j-1];
}
b[j]=value;
m++;
}
void insert()
{
i=0;
j=0;
while(i>n || j<m)
{
if(a[j]<b[i])
{
c[p]=a[j];
j++;
p++;
}
else
{
c[p]=b[i];
i++;
p++;
}
}
}
void displaya()
{
for(k=0;k<10;k++)
{
System.out.print("," +a[k]);
}
System.out.println();
}
void displayb()
{
for(k=0;k<10;k++)
{
System.out.print("," +b[k]);
}
System.out.println();
}
void displayc()
{
for(k=0;k<20;k++)
{
System.out.print("," +c[k]);
}
}
public static void main(String[] args)
{
Merge obj = new Merge();
obj.inserta(25);
obj.inserta(12);
obj.inserta(1800);
obj.inserta(9);
obj.inserta(10);
obj.inserta(15);
obj.inserta(18);
obj.inserta(19);
obj.inserta(0);
obj.inserta(1500);
obj.insertb(36);
obj.displaya();
obj.insertb(2);
obj.insertb(3);
obj.insertb(2000);
obj.insertb(5);
obj.insertb(6);
obj.insertb(7);
obj.insertb(8);
obj.insertb(21);
obj.insertb(85);
obj.displayb();
obj.insert();
obj.displayc();
}
}
In the constructor of the model class, I need to allocate the memory of this array of booleans (boolean[ ][ ] is_hidden;). I also need to set them to true, but have no idea how this happens, a nested loop will have to be used like the one in the paint method at he bottom, in order to set each element.
class MineFinderModel {
public static int MINE_SQUARE = 10;
public static int EMPTY_SQUARE = 0;
int num_of_cols;
int num_of_rows;
int[][] the_minefield;
boolean[][] is_hidden;
public MineFinderModel(int n_cols, int n_rows) {
num_of_rows = n_rows;
num_of_cols = n_cols;
the_minefield = new int[num_of_cols][num_of_rows];
is_hidden = new boolean[][];
}
Paint method example:
for (int i = 0;i<numCols;i++)
{
for(int j = 0;j<numRows;j++)
{
Rectangle r = getRect(i,j);
g.setColor(Color.black);
g2.draw(r);
if(i==0&&j==0) {
g2.drawOval(x,y,w,h);
g2.fillOval(x,y,w,h);
}
if(i==0&&j==(numRows-1))
g2.fillOval(x,y,w,h);
if(i==(numCols-1)&&j==0)
g2.fillOval(x,y,w,h);
if(i==(numCols-1)&&j==(numRows-1))
g2.fillOval(x,y,w,h);
You need to define the array with the sizes e.g.
is_hidden = new boolean[cols][rows]();
and iterate through, setting each cell to true (booleans, and boolean arrays, default to false).
Note that Arrays.fill() exists, but will only get you halfway, since it won't fill multidimensional arrays. You can use this, but you'd have to iterate through the rows, and use Arrays.fill on each row. Perhaps not worthwhile in this example, but worth being aware of regardless.
Try this:
int num_of_cols = 2;
int num_of_rows = 3;
boolean[][] is_hidden;
is_hidden = new boolean [num_of_cols][num_of_rows];
for (int i = 0; i < num_of_cols; i++) {
for (int j = 0; j < num_of_rows; j++) {
is_hidden[i][j] = true;
}
}
You can see it is now correctly initialized:
for (boolean[] col : is_hidden) {
for (boolean elem : col) {
System.out.println(elem);
}
}
when you define a boolean array the value of all the elements are false by default.
I would suggest instead of looping through all the elements, implement your conditions in way that you can use the default false value.
Eg.
boolean[][] isEnabled = new boolean[10][10];
// code to set all elements to true
if(isEnabled[i][j]){
//some code
}
this can be easily replaced by
boolean[][] isDisabled = new boolean[10][10];
if(! isDisabled[i][j]){
//some code
}
You can save processing time this way and code looks neat :).
Simply write a nested loop.
for (int i = 0;i<n_rows;i++){
for(int j = 0;j<n_cols;j++){
is_hidden[n_rows][n_cols] = true;
}
}
I have a simple solution for you: instead of using your array is_hidden and fill each element of it with true, use the name isVisible and don't fill it at all since every element of it is already initialized to false ;)