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!!
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()
{
}
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...
So, output would look like:
My array contains the following numbers (printed by row):
43 3.142 100
5.673 23.76 456.42
Let's build a text-based array using numbers
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
List the contents of the array:43 5.673 3.142 23.76 100 456.42
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)
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