printing of array contents

Hi guys
this is my first post in this helpful forum.

  1. Where can I find the manual of the AVR-Compiler? It’s for me sometimes
    very mysterious what the Compiler offers.
    For example, if I read “lvalue”, is it “logical value” or “left value”?

  2. In my attached sketch dos.ino are at least 3 errors.

The total sum of this 5 x 5 array is correct, also the vertical and horizontal sums.
Even when I add the diagonal sum, top-down from left to right, it is ok. = 25.
But when i add the diagonal sum, top-down from right to left my result = 15.
And when I start from bottom to top, left to right or right to left my result = 0.

What am I missing?

I hope, my operation with the attachment is succesfully.

Sincerely bolsak.

dos.ino (2.75 KB)

dos.ino (2.75 KB)

Any C or C++ tutorial will help understanding basic terms like 'lvalue'.

Please post code, rather than attaching it, and post the exact error messages you get.

'lvalue' means left value. It's … got a bit of a history, going mack to when the C language was invented back in the '70s. Basically, it means "something you can put on the left-hand side of an assignment operator".

Consider x = 5+3;

the subexpressions 'x' and '5+3' are both of type int (presumably) and both of them have some sort of integer value. but '5+3' is not an lvalue, so

5 + 3 = x;

Will give you the error.

'lvalue' means left value. It's … got a bit of a history, going mack to when the C language was invented back in the '70s. Basically, it means "something you can put on the left-hand side of an assignment operator".

Consider x = 5+3;

the subexpressions 'x' and '5+3' are both of type int (presumably) and both of them have some sort of integer value. but '5+3' is not an lvalue, so

5 + 3 = x;

Will give you the error. In terms of types of expressions, it's syntactically and semantically (up to a point) ok. But you can't generate code for it - the constant expression '5+3' doesn't resolve down to a memory location where the value of x can be put. The compiler can parse it, can understand the subexpressions that make up the expression. The failure happens way down near the end of the line, working out what the side-effect of that '=' should be, which is very nearly the last thing that the compiler needs to do.

Other languages (eg: Pascal) use syntax - the left hand side of an assignment must be a variable or an array reference. But in C, the left hand side can be something like a reference to an element of an array returned by a function specified by some other function:

extern int *foo(char *, int, int);
extern int bar(int);

foo("test", 1, 2)[bar(4)-3] = 5+3;

So limiting the syntax doesn't work. It's too complicated. Instead, C introduced the idea that in addition to an expression having a type and a value, an expression may or may not be an lvalue.

TO’s code:

// dos.ino 
// 30.3.18
int tab[5][5] =    // 5 * 5
{ {1, 2, 3, 4, 5},
  {2, 3, 4, 5, 6},
  {3, 4, 5, 6, 7},
  {4, 5, 6, 7, 8},
  {5, 6, 7, 8, 9}  };
  
void setup() {
  Serial.begin(115200);
  pinMode(13, OUTPUT);     // BlinkLED
  Serial.println("===================================");
  Serial.println("======================= dos.ino ===");
  Serial.println("===== 30.3.18 =====================");
  int sum_horizontal = 0;
  int sum_total = 0;
  int sum_vertical = 0;
  int sum_diagonal1 = 0; // lo-ru  result I    = 25
  int sum_diagonal2 = 0; // ro-lu  result II   = 15 
  int sum_diagonal3 = 0; // lu-ro  result III  =  0
  int sum_diagonal4 = 0; // ru-lo  result IV   =  0
  int product_tab = 0;   //  ????
  byte  i, j;
  //--------------------------------------------------
  Serial.println("             this is the array :");
//  Serial.println();
  for (i = 0; i < 5; i++)
  { for (j = 0; j < 5; j++)
      Serial.print(tab[i][j]);
    {
      Serial.println();
    }
  }
  Serial.println();
  //-----------------------------------
  for (i = 0; i < 5; i++)
  { for (j = 0; j < 5; j++)
    { sum_total += tab[i][j];       //
    }
  }
  Serial.print("       totalsum = ");
  Serial.print(sum_total);
  Serial.println();
  // -------------------------------------------------------------
  for (i = 0, j = 0; i < 5, j < 5; i++, j++) // j = lines
  {                                          // i = columns
    sum_diagonal1 += tab[i][j];
  }
  Serial.print(" diagonal-sum 1 = ");  // I top-down, left-right
  Serial.println(sum_diagonal1);       
  // ---------------------------------------------------------------
  for (i < 5, j = 0; i = 0, j < 5; i--, j++)
  {
    sum_diagonal2 += tab[i][j];
  }
  Serial.print(" diagonal-sum 2 = ");  // II top-down, right-left
  Serial.println(sum_diagonal2);
  // ----------------------------------------------------------------
  for (i = 0, j < 5; i < 5, j = 0; i--, j++)
  {
    sum_diagonal3 += tab[i][j];
  }
  Serial.print(" diagonal-sum 3 = "); // III  bottom-up, left-right
  Serial.println(sum_diagonal3);
  // -----------------------------------------------------------------
  for (i < 5, j < 5; i = 0, j = 0; i--, j--)
  {
    sum_diagonal4 += tab[i][j];
  }
  Serial.print(" diagonal-sum 4 = "); // IV  bottom-up, right-left
  Serial.println(sum_diagonal4);
  // ----------------------------------------------------------------
  for (i = 0; i < 5; i++)
  { for (j = 0; j < 5; j++)
    {
      sum_horizontal += tab[i][j];
    }
  }
  Serial.print("     horizontal = ");         //
  Serial.println(sum_horizontal);

  for (j = 0; j < 5; j++)
  { for (i = 0; i < 5; i++)
    {
      sum_vertical += tab[i][j];
    }
  }
  Serial.print("       vertical = ");    
  Serial.println(sum_vertical);
}
void loop() {
  digitalWrite(13, millis() >> 9 & 1);
}

If you turn on warnings (File / Preferences / Show verbose output during compilation CHECK) you get some of them when compiling your code, like:

warning: left operand of comma operator has no effect [-Wunused-value]

   for (i = 0, j < 5; i < 5, j = 0; i--, j++)

                        ^

For proper use of for statements take a look here (for example): http://www.learncpp.com/cpp-tutorial/57-for-statements/

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile: