Help me understand how to interpret and write this code

Hello! First post here! I’m very new to Arduino and programming in general, and I’m enrolled in an introductory course that has me slamming my head against the wall.

I’m seeking help in understanding how some code should be formatted for an assignment.

I need to write a program that:

  • globally declares an array of size fifteen. The array data type is int.
  • write a function that fills the array with random values in the range [low, high]. This function has two parameters: low, high.
  • write a function that serially transmits the array data. This function has one parameter that controls the number of array elements printed on the same line.
  • In the loop function, call the function that fills the array with random values and then call the print array function. Each time through the loop, the array is filled with random values and then serially transmitted (printed).

This seems like it should be easy! I believe I have a rough idea of how this all goes together, but I keep getting errors like:

… not declared in this scope

This is the code I’ve got so far:

    // Create an array size 15
    int A_ray[15];

    void setup() 
    {
 
      Serial.begin(9600); // Baud rate: 9600

    }

    void loop() 
    {

      fill_Array()    // Access fill_Array
      print_Array()   // Access print_Array
    }

    int fill_Array( low , high ) // I want low and High to be 0 and 25
    {
   
       A_ray[15] = random(0,26); // Fill A_ray with random numbers 0 through 25

    }

    void print_Array(void)
    {
      Serial.println(A_ray[15]); // serially prints array in a matrix fromat
    }

I’m not sure how to implement the functions and their parameters to get the serial output. In the examples, I’ve been provided this is how it looks but I’m not sure of my functions. Please help me get this program running!

For the fill array you would want to create a for loop and then on that loop set a_ray == random(0,26).
You could also call serial.print from within the same loop or create a for loop for it as well.
Look up for loops.

A_ray[15] = random(0,26); // Fill A_ray with random numbers 0 through 25

That only puts a value into one element of the array (element 14). See reply #6. FYI array indexes start at 0 so the 15th element is number 14.

Use a for loop to iterate through the array filling each element with the random number.

for (int n = 0; n < 15; n++)
{
    A_ray[n] = random(0,26);
}

Printing the matrix is a bit more complicated but still would use the for loop or loops. What are the dimensions of the matrix.

mattlogue, == is for comparison, not assignment.

A few observations :

Read up on for loops
The array has 15 elements numbered 0 to 14. There is no element 15
Statements in C++ end in a semi colon.
Variables must be declared before you can use them.

A_ray[15] = random(0,26); // Fill A_ray with random numbers 0 through 25

That only puts a value into one element of the array (element 14)

I am sure that is not what you meant to write

OK, back to the drawing board thanks for the tips! I will report back with what I come up with! Thank you!

Yes, brain fart. That puts a value into a memory location not "owned" by the array and can lead to some fun debugging.

In your two for() loops, you are still writing a value to memory you don’t own. For a 15 element array, indexes are 0 through 14.

//i <= 15
i<15;

'm confused how I would code an iteration in serial.print to print a specific value of array elements per line.

The modulo operator % can help you place the Serial.println();

void print_Array( byte numPerLine )
{
  for (byte elements = 0 ;  elements < 15; elements = elements + 1 ) // this is what I'm unsure of
  {
    Serial.print(A_ray[elements]);
    Serial.print(" ");
    if ((elements +1) % numPerLine == 0) 
      Serial.println();
  }

mattlogue:
For the fill array you would want to create a for loop and then on that loop set a_ray == random(0,26).
You could also call serial.print from within the same loop or create a for loop for it as well.
Look up for loops.
[/quote]
Why the italics? Using code tags or nobbc tags (in case you want to post 'inline' as you did) would have prevented that :wink: