@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");
}
}