# 2D ARRAY WITH A CLASS

I am a beginner on an uno and programming on 1.5.6. I have a question about how i can manipulate a 2d array without actually making the 2d array(in the interest of space). I have tried using a class, which is the example below, the upside to this is that it leaves me a lot more space, and the code runs faster.

``````class enter{
private:
int right;
int left;
public:
enter(){

}
enter(int y,int x){
right=y;
left=x;
}
int getRight(){
return right;
}
int getLeft(){
return left;
}
boolean check(int y,int x){
if(right==y&&left==x){
return true;
}
else{
return false;
}
}
};
void setup() {
Serial.begin(9600);  // put your setup code here, to run once:

}

void loop() {

for (int p = 0; p < 10; p++) {
for (int i = 0; i < 15; i++) {
//  client.print(array[p][i]);
//  client.print("|");
Serial.print("0");
Serial.print("|");
}
//client.println("
");
Serial.println();

}
Serial.println("break");
enter b(2,3);

for (int p = 0; p < 10; p++) {
for (int i = 0; i < 15; i++) {
//  client.print(array[p][i]);
//  client.print("|");

if(b.check(p,i)){
Serial.print("4");
Serial.print("|");
}
else{
Serial.print("0");
Serial.print("|");
}

}
//client.println("
");
Serial.println();

}
Serial.println("done");
}
``````

The issue comes up with the fact that i cannot make a variable number of these objects, so i would have to know exactly the number of “4’s” that are needed. Is there anyway i can work around this, like a va_list or do i need another approach.

I have a question about how i can manipulate a 2d array without actually making the 2d array

I’ve got news for you. You can’t manipulate something you didn’t make.

The issue comes up with the fact that i cannot make a variable number of these objects

A variable number of what objects? Instances of the enter class?

Of course you can create a variable number of them. What you can’t do, without using malloc() or new() is create a variable length array of them.

Yodogwood:
The issue comes up with the fact that i cannot make a variable number of these objects, so i would have to know exactly the number of "4's" that are needed. Is there anyway i can work around this, like a va_list or do i need another approach.

There is a solution but Rule #1 applies. You can not have something for nothing.

Your code sets out an array of 10 x 15 x single values, which could be packed into an array of 150 bits, which is about 20 bytes. If we assume you actually want each cell in your grid to hold 5 values (0 to 4), each cell would require 3 bits and it is still only 60 bytes.

If you go down the object route, each cell in your grid requires X and Y properties, consuming 2 bytes at the very least. To do anything useful with an instance of a 'cell' object, your program needs to keep a reference to it's position in memory. So each instance of your 'cell' consumes another two bytes for the pointer - Which brings you up to 6 bytes.

This demonstrates a 'natural truth' of Object Oriented Programming. There is a cost to using a Class and it is rarely worth paying for a simple data type. OOP does not save memory or CPU cycles. OOP makes complex data types, easier for humans to work with.

If you really want to use objects, you might consider the entire grid to be the complex data type.