Multidimentional Array data

Hello All,
I need your assistance once again.
I am working with Multidimensional array and i want to store the number in array.
But I am facing problem in that. If i don't use break function it will keep updating the whole column value .
I want to increase my value by one and store it one value at a time in array .
can somebody please help me in this regards??

Thanks in advance.

int i,j=0;
const int columns=5;
const int rows=20;
int maxa=0;
int maxb=400;
int biDimArray [rows] [columns]={
  {0, 0, 0},       
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0}
};



void setup() {
  Serial.begin(9600);
  Serial.println();
  Serial.println("Print Array Before Overwriting The Content");
  for (i=0; i<11; i++){
    for(j=0; j<5; j++){
       Serial.print(biDimArray [i][j]);
      Serial.print(' ');
    }
    Serial.println();
  }
  Serial.println();
  delay(1000);

}

void loop() { /*I am bit confuse here.*/
  if (maxa<maxb){
    for(i=0; i<11; i++){
      maxa+=1;
      biDimArray [1][1+i]=maxa;
      break;
    }

    
    Serial.println("Array updated");
  for (i=0; i<11; i++){
    for(j=0; j<5; j++){
       Serial.print(biDimArray [i][j]);
      Serial.print(' ');
    }
    Serial.println();
  }
  Serial.println();
  delay(1000); 
  
  }
  else{
    
    Serial.println("Array updated");
  for (i=0; i<11; i++){
    for(j=0; j<5; j++){
       Serial.print(biDimArray [i][j]);
      Serial.print(' ');
    }
    Serial.println();
  }
  Serial.println();
  delay(1000); 
  while(1);
  }

Where does this number "11" come from?

That is a typical "magic" number :nerd_face:

i consider 20 constant rows but i only need 11 rows right now. That's why i use 11.

Enable the warnings in the IDE and it will show your problem.
It's always a good idea to have the warnings enabled.
( Your 2nd index is 'columns' with a max value of 4 . But you count until 11 )

Makes even less sense than the random appearance of 11

1+i ranges from 1 to 11, and is used in the second index position which is of dimension 5.

Also the loop has an unconditional break, so its not a loop in the first place. Confused.

an you only provide 10 entries not 11 (and they are all at 0 anyway)

i am new in programming... That s why really confused. could you please tell me where can i do correction in this program. so it can work out.

for printing the array, you could start with this (to avoid magic numbers in the code)

const byte maxColumns = 5;
const byte maxRows = 20;

const byte usedColums = 3; // could assert that it's <= maxColumns
const byte usedRows = 10;  // could assert that it's <= maxRows

int biDimArray[maxRows][maxColumns] = {
  {0, 0, 0},
  {1, 1, 1},
  {2, 2, 2},
  {3, 3, 3},
  {4, 4, 4},
  {5, 5, 5},
  {6, 6, 6},
  {7, 7, 7},
  {8, 8, 8},
  {9, 9, 9},
};


void setup() {
  Serial.begin(115200); // don't go slow
  Serial.println();
  Serial.println("Print Array Before Overwriting The Content");
  for (byte i = 0; i < usedRows; i++) {
    for (byte j = 0; j < usedColums; j++) {
      Serial.print(biDimArray[i][j]);
      Serial.write('\t');
    }
    Serial.println();
  }
  Serial.println();
}

void loop() {}

(typed here, untested)

not sure what you want to do in the loop

I'm just wondering:

Every time I read of "multidimensional" Arrays from beginners, I would like to ask what kind of data you want to store, because most of the time, an Array of struct makes life far easier.

So what are you really saving in your 5 values?

As i mentioned in my program above, in the loop i am two variable maxa and maxb (Two outputs of sensor). in case if maxa is lesser than maxb, in this case matrix will store its value. As an example, if maxa =5 and maxb=15 than array will store a[1][1] store maxa value and if the condition is true for next sampling, i need to increase row value(a[2][1]) and store another value in that.

This looks to me like a case of the XY problem. @ashish_senger : Can you take a step back and explain what you are trying to accomplish? As It's been already suggested, it may be that a 2d array is not the best data structure for your problem.

so you have 2 values and I understand you want to compare them and remember them (both) is some condition is met but.... ➜ What is the third columns for ?

Hello
Take some time and study some tutorials about multidimentional array´s. You will find fg and simple descriptions inside the WWW.

If this system work out, i will incorporate another sensor and store its value in 3 column.

@ashish_senger
IF you describe what you want to achieve we could give you better answers.

This is an example why I think a structure fits better to "several sensor data".
It makes the code readable and easier to maintain if new sensors should be added to the Array.

/*
  https://forum.arduino.cc/t/multidimentional-array-data/901512/

   bring a novice from unreadable multidemensional arrays to readable struct
*/

const size_t rows = 20;
int maxa;
int maxb;

// define your own data structure
//write how your Sensors are values are name
struct Data
{
  int temperature;
  int humidity;
  //int newValue;   // one day...
};

// define your array of Data rows
// it's an Array of your define
// no need to initialize a global with 0 values:
Data myData [rows] {};

// helper function to debug print the values from the Array to serial

void printValues()
{
  for (size_t i = 0; i < rows; i++)
  {
    Serial.print(i);
    Serial.print("\t");
    Serial.print (myData[i].temperature);
    Serial.print("\t");
    Serial.print (myData[i].humidity);
    //Serial.print("\t");
    //Serial.print (myData[i].newValue); // one day
    Serial.println();
  }
}

void setup() {
  Serial.begin(9600);
  Serial.println();
  Serial.println("Print Array Before Overwriting The Content");

  size_t targetRow = 0;
  for (size_t i = 0; i < rows; i++)
  {
    // simulate some random values to simulate a sensor
    maxa = random(42, 4711);
    maxb = random(42, 4711);

    // decide whatver ...
    if (maxa < maxb)
    {
      // "store" the the value to the array
      myData[targetRow].temperature = maxa;
    }
    targetRow++;
  }

  printValues();
}

void loop() {
  // put your main code here, to run repeatedly:

}

If you need further help, please describe what kind of data you want to hold in your variables per row.

here is another example along the same line

const size_t sensorCount = 2;       // we use only 2 sensors
const size_t memorySize = 5;       // we will remember at max this number of entries

struct t_sensorValues {             // this is to store one sample of all the sensors values
  int sensors[sensorCount];
};

t_sensorValues currentValues;       // the current value

t_sensorValues memory[memorySize];     // where we will archive all the data meeting the condition
size_t memoryCount = 0;             // how many we archived

// printing sensors sampled value
void printSensorData(t_sensorValues& sensors) {
  for (size_t i = 0; i < sensorCount; i++) {
    Serial.print(sensors.sensors[i]);
    if (i == sensorCount - 1) Serial.println(); else Serial.write('\t');
  }
}

// printing what has been archived so far
void printMemory() {
  for (size_t i = 0; i < memoryCount; i++) {
    Serial.print(F("memory[")); Serial.print(i); Serial.print(F("] =\t"));
    printSensorData(memory[i]);
  }
  Serial.println(F("-------------"));
}

// getting a sample from all sensors to update the currentValues
void readSensors() {
  for (size_t i = 0; i < sensorCount; i++)
    currentValues.sensors[i] = random(0, 1023); // whatever
}

// the function that will decide if we met a certain criteria for archiving
// returns false if it could not archive (array full)
bool verifySensors() {
  bool success = true;
  if (currentValues.sensors[0] < currentValues.sensors[1]) {  // test some condition that will decide if you need to memorize this sample
    Serial.println("Condition met !");
    if (memoryCount < memorySize) {    // condition is met, archive both values if we have space left
      memory[memoryCount++] = currentValues;
      printMemory();
    } else {
      success = false;
    }
  }
  return success;
}

// -------- The main code ----------
void setup() {
  Serial.begin(115200); Serial.println();
}

void loop() {
  readSensors();
  if (!verifySensors()) {
    Serial.println("sorry, memory was full, could not archive. Stopping here.");
    while (true) yield(); // die here
  }
}

serial monitor at 115200 bauds will show something like this

Condition met !
memory[0] =	209	281
-------------
Condition met !
memory[0] =	209	281
memory[1] =	52	575
-------------
Condition met !
memory[0] =	209	281
memory[1] =	52	575
memory[2] =	156	176
-------------
Condition met !
memory[0] =	209	281
memory[1] =	52	575
memory[2] =	156	176
memory[3] =	336	908
-------------
Condition met !
memory[0] =	209	281
memory[1] =	52	575
memory[2] =	156	176
memory[3] =	336	908
memory[4] =	120	863
-------------
Condition met !
sorry, memory was full, could not archive. Stopping here.