Array using const int values

Hi,

My question is on using arrays, where I'm imputing one of the dimensions using an integer. I'd like to use the variable name of the const int var I've defined equal to the integer, but can't. Is this disallowed in C or am I doing something wrong? Code snippets:

// global variables
const int num_of_races = 4;
const int total_runners = 2;
double run_time[total_runners][num_of_races];   // defines a 2x4 matrix.

//function prototype
void print_runner_data(int total_runners, int num_of_races, double run_time[][4]);

//loop
print_runner_data(total_runners, num_of_races, run_time);

// function definition
void runner_data(int num_of_races, int runner_num, int total_runners, double run_time[][4])  

You'll see I entered '4' instead of 'num_of_races'. When I try to enter 'num_of_races' I get this error message:

C:\Documents\Arduino\runner data\runner_data_6\runner_data_6.ino:66:91: error: use of parameter outside function body before ']' token
 void print_runner_data(int total_runners, int num_of_races, double run_time[][num_of_races]);
                                                                                           ^
C:\Documents\Arduino\runner data\runner_data_6\runner_data_6.ino:106:101: error: use of parameter outside function body before ']' token
 void runner_data(int num_of_races, int runner_num, int total_runners, double run_time[][num_of_races])    // enter all runner data
                                                                                                     ^
C:\Documents\Arduino\runner data\runner_data_6\runner_data_6.ino:106:101: error: use of parameter outside function body before ']' token
 void runner_data(int num_of_races, int runner_num, int total_runners, double run_time[][num_of_races])    // enter all runner data
                                                                                                     ^
exit status 1
Compilation error: use of parameter outside function body before ']' token

Looks like the compiler is trying to use the 2nd argument for the function instead of the global variable. Using different names for the global variables and the function parameters appears to work, and is a lot less confusing in actual code.

It's not what it looks like, it's what the standard says the compiler does when you declare a variable with a same name in a smaller scope.


to the error, it seems you are doing weird thing as the compiler complains

error: use of parameter outside function body

➜ post the full code of your example. Don't post snippets (Snippets R Us!)

Would a :: before the used global not specify global scope?

it would ( The global namespace is referred to by using ::)

int x = 10;

void function(int x) {
  Serial.print("local x = ");  Serial.println(x);
  Serial.print("global x = ");  Serial.println(::x);
}

void setup() {
  Serial.begin(115200);
  function(20);
}

void loop() {}

Serial Monitor (@ 115200 bauds) will show

local x = 20
global x = 10

Memory is still working, kind of. :grinning:

I never had to use that specifier, and there are good reasons not to be forced to.

indeed - it's good to know but rarely used

@david_2018 [david_2018] and @J-M-L

Both correct, thank you. I modified the global vars have one instance to use const int vars to set the double array, and then I copied those values into a new const int which was then passed into the function parameters. So yes, looks like I confused the compiler.

//Global vars
const int num_of_races = 4;  // change this line for # of races, defines num of array columns
  const int numRaces = num_of_races;  // don't touch
const int total_runners = 2; // change this line to for # of runners, defines num of array rows
  const int totalRunners = total_runners;  // don't touch

//function prototypes
void runner_data(int num_of_races, int runner_num, int total_runners, double run_time[][numRaces]);    // enter all runner data
void print_runner_data(int total_runners, int num_of_races, double run_time[][numRaces]);

//function calls
 print_runner_data(total_runners, num_of_races, run_time);


//function definitions
void runner_data(int num_of_races, int runner_num, int total_runners, double run_time[][numRaces])    // enter all runner data

void print_runner_data(int total_runners, int num_of_races, double run_time[][numRaces])

Entire code below:

/***** LIBRARIES *****/
#include<Arduino.h>


/***** GLOBAL VARIABLES *****/
int runner_num = 0;     // defines start num of which runner is running
double race_time = 0;   // for array, 2nd column, race time which gets input
const int num_of_races = 4;  // change this line for # of races, defines num of array columns
  const int numRaces = num_of_races;  // don't touch
const int total_runners = 2; // change this line to for # of runners, defines num of array rows
  const int totalRunners = total_runners;  // don't touch
const int last_loop = total_runners-1;
const int math_calculations = 2; // changes based on number of on-board calculations to perform
double run_time[total_runners][num_of_races];   // defines a 2x4 matrix. Row 1 = runner 1 data, Row 2 = runner 2 data
double sorted_run_time[total_runners][num_of_races];  // same dimensions
double calculations[total_runners][math_calculations];  
      /* structure: 2xn, where n represents n-1 number of element positions, each representing a different calculation
         basic structure: calculations[runner number] : [average][minimum][maximum][median]
      */
bool print_race_data = false;


/***** FUNCTION PROTOTYPES *****/
void runner_data(int num_of_races, int runner_num, int total_runners, double run_time[][numRaces]);    // enter all runner data
void print_runner_data(int total_runners, int num_of_races, double run_time[][numRaces]);
void copy_data(int total_runners, int num_of_races, double run_time[][numRaces], double sort_run_time[][numRaces]);
void sort_data(int total_runners, int num_of_races, double sorted_run_time[][numRaces]);
void average(int total_runners, int num_of_races, int runner_num, double run_time[][numRaces], double calculations[][totalRunners]);
void minimum(int runner_num, int total_runners, double sorted_run_time[][numRaces], double calculations[][totalRunners]);


/***** SETUP *****/
void setup() 
{
  Serial.begin(9600);
  
  /***** RUNNER DATA *****/
  runner_data(num_of_races, runner_num, total_runners, run_time);

  if (print_race_data == true)
  {
    // function calls (these will eventually become function memebers of a math calculations class)
    print_runner_data(total_runners, num_of_races, run_time);
    copy_data(total_runners, num_of_races, run_time, sorted_run_time);
    sort_data(total_runners, num_of_races, sorted_run_time);
    average(total_runners, num_of_races, runner_num, run_time, calculations);
    minimum(runner_num, total_runners, sorted_run_time, calculations);
  }
}

/***** MAIN LOOP *****/
void loop() 
{

  // END PROGRAM
  Serial.flush();
  exit(0);
}
/***** END OF MAIN LOOP *****/


/***** FUNCTION DEFINITIONS *****/

void runner_data(int num_of_races, int runner_num, int total_runners, double run_time[][numRaces])    // enter all runner data
{
 for(runner_num; runner_num < total_runners; runner_num++)
  {
    Serial.print("***\nEnter times for runner: ");
    Serial.println(runner_num+1);
  
    int j=0; // starts val is for loop
    for(j; j < num_of_races; j++)
      {
        //Serial.println("Runner 1***");   //debug line
        Serial.print("race ");
        Serial.print(j+1);        //race number
        Serial.print(": ");
        while (Serial.available() == 0) {}  // waits until data is entered into serial monitor
        run_time[runner_num][j] = Serial.parseFloat();    //takes incoming characters and converts it to a number. This is read into the buffer altogether.  Default is data type long.
        Serial.print(run_time[runner_num][j]);
        Serial.println(" minutes");
        
        // flush input buffer, do before asking for new input.  Notes: https://forum.arduino.cc/t/serial-input-basics-updated/382007
        while (Serial.available() > 0) {Serial.read();}
      }

  // increase runner number for display and entry of new array for run times
  //runner_num++;
  //j = 0; //reset for loop for next runner's data to be entered  
  }

  Serial.println("\nAll race data has been entered.");
  Serial.println("*** Race results ***");
  print_race_data = true;  // flag changed to true to allow for calcs to begin
}


void print_runner_data(int total_runners, int num_of_races, double run_time[][numRaces])
{
  int a = 0;
  int b = 0;
  // recall structure of table: run_time[total_runners][num_of_races]
    for (a; a < total_runners; a++)
    {
      Serial.print("*Runner ");
      Serial.print(a+1);
      Serial.println(" data*");

        for(b; b < num_of_races; b++)
        {
          Serial.print("   race ");
          Serial.print(b+1);
          Serial.print(": ");
          Serial.print(run_time[a][b]);  // Holds 'a' constant, so it dynamically moves to and prints at array position "b"
        }
        Serial.println("\n");
        b = 0; //reset num_of_races counter to allow for next runner's times to be displayed
    }
}


void copy_data(int total_runners, int num_of_races, double run_time[][numRaces], double sorted_run_time[][numRaces])
{ 

  // copy run_time array into sorting array.  This preserves raw data
  int b = 0;
  int c = 0;
  // recall structure of table: run_time[total_runners][num_of_races]
    for (c; c < total_runners; c++)
    {
       for(b; b < num_of_races; b++)
        {
          sorted_run_time[c][b] = run_time[c][b];  // Holds 'c' constant, so it dynamically moves to and copies at array position "b"
        }
        b = 0; //reset num_of_races counter to allow for next runner's times to be displayed
    }

}

void sort_data(int total_runners, int num_of_races, double sorted_run_time[][numRaces])
{


  //sort data int sorted array via bubble sort.
   // structure:  double sorted_run_time[total_runners][num_of_races]


  for (int i = 0; i < total_runners; i++)  // increase row for each runner
  { 
    // loop to control # of passes
    for (int pass = 0; pass < (num_of_races - 1); pass++) 
      {
        double hold;

          //loop to control number of comparisons per pass
          for (int j = 0; j < num_of_races - 1; j++)
            {
              //compare elements, swap locations if 1st element > next element
              if (sorted_run_time[i][j] > sorted_run_time[i][j+1]) // holds runner number constant
                { 
                  hold = sorted_run_time[i][j];
                  sorted_run_time[i][j] = sorted_run_time[i][j+1];
                  sorted_run_time[i][j+1] = hold;
                }
            }

      }

  }

  // print sorted array
  // recall structure of table: run_time[total_runners][num_of_races]
  Serial.println("Print Sorted Data");

    for (int a = 0; a < total_runners; a++)
    {
      Serial.print("*Runner ");
      Serial.print(a+1);
      Serial.println(" data*");
     
        for(int b = 0; b < num_of_races; b++)
        {
          Serial.print("   race ");
          Serial.print(b+1);
          Serial.print(": ");
          Serial.print(sorted_run_time[a][b]);  // Holds 'a' constant, so it dynamically moves to and prints at array position "b"
        }
        Serial.println("\n");
       //b = 0; //reset num_of_races counter to allow for next runner's times to be displayed
    }
}

void average(int total_runners, int num_of_races, int runner_num, double run_time[][numRaces], double calculations[][totalRunners])
{
  
  double running_total = 0;
  double average = 0;
  runner_num = 0;  //reset to default

  for(runner_num; runner_num < total_runners; runner_num++)
    {
      for(int j = 0; j < num_of_races; j++)
      {   
        running_total = running_total + run_time[runner_num][j];  
      }       
      
      average = (running_total / num_of_races);
      running_total = 0; //reset for next loop

      //place data into calcs array
      int calc_position = 0;
      calculations[runner_num][calc_position] = average;
      Serial.print("Average run time for runner ");
      Serial.print(runner_num+1);
      Serial.print(": ");
      Serial.print(calculations[runner_num][calc_position]);
      Serial.println(" minutes"); 
      calc_position++;
  }
}

void minimum(int runner_num, int total_runners, double sorted_run_time[][numRaces], double calculations[][totalRunners])
{
  runner_num = 0; // reset to 0
  
  for(runner_num; runner_num < total_runners; runner_num++)
  {
    calculations[runner_num][1] = sorted_run_time[runner_num][0]; //places min time from sorted array of low to high run times into calculations array
  }

    //print calc array for run min time
    runner_num = 0; // reset to 0
    int calc_position = 1;
    
    for (runner_num; runner_num < total_runners; runner_num++)
      {
        Serial.print("Minimum run time for runner ");
        Serial.print(runner_num+1);
        Serial.print(": ");
        Serial.print(calculations[runner_num][calc_position]);
        Serial.println(" minutes"); 
      }
}

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