How to write row and column form

hello, is there a way to make a program in row and table form, i have such problem. maybe using array function or something, please let me know about that. thank you

Are you trying to output to the Serial monitor or some other display as a table, or to export the data, perhaps as a comma separated file, so that it can be imprted into Excel ?

I will use the value of N for the next calculation again

So, what you really want to do is to use a number of values in a formula and the table layout has nothing to do with it

Please explain what x*n and similar expressions mean in your example and explain which of the values are fixed and which are variable

It is not clear what you are asking. You can pretty much write a program to do all of these calculations, assuming the processor you are using can provide the mathematical precision you need. Also, this is an Arduino forum so if you are using one of the Arduino boards then there must be a reason.

Please describe your application in terms of the following:

  • The input interface(s) and data format(s) you will be using on the Arduino board. For example: serial, analog, digital, Bluetooth, etc.
  • How will that input data be processed?
  • The output interface(s) and data format(s) you expect from the Arduino. For example: serial, analog, digital, Bluetooth, LCD, relays, etc.

This will allow us to help you.

so the fixed value is n, for x,y and z the value follows the row, for example in the first row like this, (10,245134)+(50,245134)-(7*0,245134)
as well as the next line.

Of course it is possible to do calculations with a program like you have pointed out in the spreadsheet. Do I understand you correctly that what you want is the following:

  • There is a floating point number n (with n = 0.245134)

  • There is a table with columns for x, y, z and F

  • This table has a number of rows (in the example row 4 to 6)

  • The program shall calculate (x * n)+(y * n)-(z * n) and store the result in F per row

  • The program shall sum up the data in column F for a given number of rows (N = SUM(F4:F6)

  • My questions:

    • Will the sum N be used as n in the next calculation or will n always stay 0.245134?
    • Is the number of rows to be summarized always 3 or will it vary?
    • Why do you multiply x, y and z each with n? From pure math you could do also (x+y-z)*n instead.
    • Do you need to keep the previous data or can "the table" be erased and used again after N was calculated?

Would you mind to explain the background of your calculations? That would help to assist in finding a suitable solution ...

Perhaps the OP is wanting to write a spread sheet interpreter program.

I'm sorry if the question is not clear,
i just want to make such calculation in arduino program, but i don't know how and which function is suitable to use.

Are you after a 2D array? Multidimensional Arrays in C / C++ - GeeksforGeeks

int x[3][4] = {
  {0, 1,  2,  3}, 
  {4, 5,  6,  7}, 
  {8, 9, 10, 11}
};

thank you,
-The value of N I will use for the next calculation again, and the value of n will remain.
-for the number of columns is the same in all rows
-That I only use a simple calculation as an example
-I only need N values

Ok. So how many rows must your table have in maximum?

There is a probability that you only get a solution for your simple example ... If you could explain the background of your project it might be easier and quicker to get to a suggestion.

about 70-80 lines

Here is your example expressed using normal variables with names taken from your spreadsheet

float n = 0.245134;
int x4 = 1;
int x5 = 2;
int x6 = 4;
int y4 = 5;
int y5 = 2;
int y6 = 9;
int z4 = 7;
int z5 = 4;
int z6 = 1;

float f4 = (x4*n) + (y4*n) + (z4*n);
float f5 = (x5*n) + (y5*n) + (z5*n);
float f6 = (x6*n) + (y6*n) + (z6*n);

float bigN = f4 + f5 + f6;

void setup()
{
  Serial.begin(115200);
  Serial.println(bigN);
}

void loop()
{
}

It is, of course, clumsy, but as has been pointed out, the values would be better put in a2 dimensional array with x, y and x being the column numbers

So it could be done like this

int a[][3] =
{
  {1, 5, 7},
  {2, 2, 4},
  {4, 9, 1}
};

float n = 0.245134;

float f4 = (a[0][0] * n) + (a[0][1] * n) + (a[0][2] * n);
float f5 = (a[1][0] * n) + (a[1][1] * n) + (a[1][2] * n);
float f6 = (a[2][0] * n) + (a[2][1] * n) + (a[2][2] * n);

float bigN = f4 + f5 + f6;

void setup()
{
  Serial.begin(115200);
  Serial.println(bigN);
}

void loop()
{
}

I am not going to take it any further, but the code could be shortened by the use of a for loop and the intermediate results (f4, f5 and f6) could be eliminated by totalling as you iterate through the array of values

I was typing something similar as you posted :smiley:

#define T_ROW 3
#define T_COL 3

float table[T_ROW][T_COL] = {
  {1.f, 5.f, 7.f},
  {2.f, 2.f, 4.f},
  {3.f, 9.f, 1.f}
};

float num = 0.245134f;

float calculate(float arr[][T_COL], float n) {
  float tempResults[T_ROW];
  float sum;

  for (int i = 0; i < T_ROW; i++) {
      // I think there's a better way to do the below so the values aren't hardcoded
      // but I'm still thinking on it.
      tempResults[i] = (table[i][0] + table[i][1] - table[i][2]) * n;
  }

  for (int i = 0; i < T_ROW; i++)
    sum += tempResults[i];

  return sum;
}

void setup() {
  Serial.begin(9600);
  Serial.println(calculate(table, num), 5);
}

void loop() {}

so i want to apply calculation in my excel file to serve in arduino program,
that's for the value of each row is collected in the "hasil" value
which will then add up all .

Great minds !

We don't know where the data is coming from and how it gets into the array of course, but I suspect we are being asked for help based on what @sagise has decided is the way to do it, when given more details a different solution may be appropriate

How is the data going to be read by the Arduino and at what frequency ?

What do you see as the advantage of using the Arduino to do the calculations when Excel would be so much easier to use and you already have the spreadsheet to do it.

You could export a file of raw data from the Arduino and import it into your spreadsheet to do the calculation and presentation of the results

I second that!!

At least in the spreadsheet @sagise has provided I could read "kemiringan sumbu rotasi bumi" and Mr. Google told me it is Indonesian for "Earth rotational axis" ...

@sagise : Are you getting data from some sensor(s) and try to do a (whatever) standalone calculation in an Arduino?

The following sketch will show you that there are lots of ways to possibly provide a solution for the task you mentioned so far ... But if this or any other sketch will really do what you expect is still not clear ...

const int RowCount = 3;
const int ColCount = 4;
const int LastCol = ColCount -1;
const int LastRow = RowCount -1;

float n = 0.245134;
 
// The functions below do the calculation as described: 
// Multiply data from columns 1 and 2 and sum those up,
// Multiply data from column 3 and subtract this from the former sum
 
// Be aware that columns in C/C++ are always numbered from 0 upwards
// So the first column has index 0, the second index 1 etc.
// and therefore column 3 has the index 3-1= 2 in the array!

float originaltable[RowCount][ColCount] = {
     {1,5,7,0},
     {2,2,4,0},
     {4,9,1,0}
 };

void OriginalFunctionPerRow(int row){
  originaltable[row][LastCol] = 0;
  for (int i = 0; i < 2 ; i++){
       originaltable[row][LastCol] += originaltable[row][i]*n;   
    }
  originaltable[row][LastCol] -= originaltable[row][2]*n;
  }

float SumOriginaltable(){
  float theSum = 0;
  for (int i = 0; i < RowCount; i++){
       theSum += originaltable[i][LastCol]; 
    }
   return theSum; 
  }

// To ease the calculation in "..FunctionPerRow()"
// I have incoporated the "minus" in the table values  
// of the third column and named that table changedtable[][]!
// So the function SumChangedTable() assumes that the sign of the third column data 
// has been inverted from + to - (or - to + if applicable)

float changedtable[RowCount][ColCount] = {
     {1,5,-7,0},
     {2,2,-4,0},
     {4,9,-1,0}
 };

void ChangedFunctionPerRow(int row){
  changedtable[row][LastCol] = 0;
  for (int i = 0; i < LastCol; i++){
       changedtable[row][LastCol] += changedtable[row][i]*n;   
    }
  }

float SumChangedtable(){
  float theSum = 0;
  for (int i = 0; i < RowCount; i++){
       theSum += changedtable[i][LastCol]; 
    }
   return theSum; 
  }


// This function goes through the rows to calculate the sum per row for
// both tables in parallel 

void CalculateFunctionValues(int fromRow, int toRow){
  for (int i = fromRow; i <= toRow; i++){
     OriginalFunctionPerRow(i);  
     ChangedFunctionPerRow(i);  
    }
  }


void setup() {
  Serial.begin(115200);
  // This calculates the sums per Row for both tables in parallel
  CalculateFunctionValues(0,LastRow);
  
  Serial.print("Result for N in the original table is ");Serial.println(SumOriginaltable());
  
  Serial.print("Result for N in the changed table is ");Serial.println(SumChangedtable());
}

void loop() {
  // Nothing to do in loop() for this example
}

At least you can study the different solutions to learn and - maybe - find out what serves your problem.

One issue to be discussed will be the accuracy of the floating point calculations ... See here

https://www.arduino.cc/reference/en/language/variables/data-types/float/

P.S.: Be aware that this is only demonstrating principles, it is not foolproof nor exactly fitting your task; in best case - it fits to your example.

P.P.S.: And in the microcontroller world you would do as much as possible in integer and only what is necessary as float!