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);
}
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 )
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.
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 ?
@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.
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