Printing elements from an array?

I'm trying to understand how arrays work (which seems like a super cool feature in the Arduino) but I seem to be a bit stuck. After reading through the great arduino "string" reference page, I wanted to see if I could play with a simple 2-dimensional array and print out its values (using numbers turned into char, just for the heck of it).

Below is my code... I think I am close, but not quite there yet. Not sure if the commands are correct, but I keep getting a compiling error... Not sure what is going on...

Any suggestions with this would be great!! :slight_smile:

char* products[][3]= {  
                         {"43", "3.142", "100"},
                         {"5.673", "23.76", "456.42"}                         
                      };


void setup()
{
  Serial.begin(9600);
  Serial.println("Let's build a text-based array using numbers :) ");
                                 
                           
    for(int i=0; i<3; i++){
         for(int j=0; j<2; j++){

         // Here, my printed output should be:
         // 43 3.142 100
         // 5.673 23.76 456.42
           
          Serial.print("My array contains the following numbers (printed by row):");
          Serial.println(products[j][i]);      
         }
    }

}


void loop()
{ 
}
         // Here, my printed output should be:

No, it shouldn't. You are printing the first three characters of the first two strings.

Not sure if the commands are correct, but I keep getting a compiling error... Not sure what is going on...

But, you didn't post them, so how can we help?

I'm not sure why you want to print that long message 6 times. I wouldn't.

Thanks Paul. I just happened to realize that my sketch wasn't compiling because of the wrong dimensions I had specified for the array. I amended that in the previous post, and it is now compiling with no problem.

I see what you mean... Sorry for not being more clear. All I would like to do is to have the loop print each row contained in the array. The concept of arrays is a bit confusing to me, but I think that an example like this one will help me get a better handle on it... :slight_smile:

So, output would look like:
My array contains the following numbers (printed by row):
43 3.142 100
5.673 23.76 456.42

So, output would look like:

Surely you've tested the code. So, you should be able to say:

So, output looks like:

Yes... right now, the output looks like this:

Let's build a text-based array using numbers :slight_smile:
List the contents of the array:43
List the contents of the array:5.673
List the contents of the array:3.142
List the contents of the array:23.76
List the contents of the array:100
List the contents of the array:456.42

But instead, I am trying to have it print the example below. I realize it has to do with the loop, but I'm not sure how to make it so that it will print the output shown below:

Let's build a text-based array using numbers :slight_smile:
List the contents of the array:43 5.673 3.142 23.76 100 456.42

You print all that text and a line feed every time.

Thanks Jimmy for the suggestion! Makes total sense... I figured it out!

char* products[][3]= {  
                         {"43", "3.142", "100"},
                         {"5.673", "23.76", "456.42"}                         
                      };



void setup()
{
  Serial.begin(9600);
  Serial.println("Let's build a text-based array using numbers :) ");
  Serial.print("List the contents of the array:");
                           
    for(int i=0; i<3; i++){
         for(int j=0; j<2; j++){

         // Here, my printed output should be:
         // 43 3.142 100
         // 5.673 23.76 456.42           
          Serial.print(" ");
          Serial.print(products[j][i]);      

         }
    }
}


void loop()
{ 
}

One more related question... can I modify the value of a particular element in the array?

For instance, is there a command that would let me substitute the "100" value for "101"?

After defining the "products" array as in the code above, maybe I could add something along the lines of:
char products[0][2] = "101"; (this doesn't seem to compile, though)

For instance, is there a command that would let me substitute the "100" value for "101"?

strcpy(products[0][2], "101");

It has to fit. Your strings have various lengths. If you change the "101" to "101.0" it will break in this case.

Are you really storing numbers as strings? Computers have better ways to store numbers.

http://www.cplusplus.com/reference/cstring/strcpy/

Wuau... works like a charm. I will study that link with great detail (I had seen it before but I somehow missed this particular command). Thank you so much!

Yeah, I know what you mean re. the "unorthodox" manipulation of numbers like characters. I'm just playing around with arrays for now to see how they work. Thanks again for the tip... I learned something super useful today :slight_smile: