How to use arrays

]Hi, I have to make a program that ask the user how many numbers he want to calculate the square of (f. example, if the user types 4 the program will show the square of 0, 1, 2 and 3). I was specifically ask to do it using an array.
I manege to do it with a normal loop but i tried to use an array all i get is errors... does anyone have an idea please?
Any explanation or trick about arrays are more than welcome :slight_smile:

void setup()
{	
        Serial.begin(9600);
	Serial.setTimeout(100);
  	Serial.println("How many squares?");
}
void loop()
{
	if (Serial.available()
    {
      int Squares = Serial.parseInt();
      Serial.println(Squares);
      int Array[Squares]; 
      Serial.println("Answer: ");
      for (int i = 0; i < Squares ; i++)
      {
    
		Serial.println(i*i);
      }
	Serial.println();
    }
}

i tried to use an array all i get is errors

Post your best effort at writing the code and the errors that it causes

What are you trying to do with the array?

 if (Serial.available()

That if is missing a closing ). That is the only error in the posted code.

There is an unused variable warning because the variable, Array, is declared but never used.

Do you want to populate the array with the squares like so?

for (int i = 0; i < Squares ; i++)
      {
         Serial.println(i * i);
         Array[i] = i * i;  // put the square of i into an array
      }

Cross post.
Thread closed.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.