I'm stuck trying 2d arrays.

Thanks BobKay

Next problem - argh!

so (?) using #define means everytime I refer to numPorts, the compiler inserts 2

But it seems to want a "," or "..." and I don't follow that.

Sorry to be a pain

Bob

//-----------------
//set up the global variables
#define numReadings 10
#define numPorts 2
long numberofReadings=numReadings;
int readings[numReadings][numPorts];      // the readings from the analog input ports
int readIndex[numPorts];// the index of the current reading
float total[numPorts];                  // the running totals for each port
float average[numPorts];                // the average for each port


void setup() {
  // initialize serial:
  Serial.begin(9600);
  analogReference(EXTERNAL);
  InitArray(readings,numReadings,numPorts); //call function to pre-fill the arrays 
}

void loop() {
  average= GetNewAverage(readings, numReadings,numPorts);
  // send it to the computer as ASCII digits
  Serial.print(average);
  Serial.print("  ");
  delay(1000);        // delay in between reads for stability
  Serial.println("  ");
}
void InitArray(int readings[][numPorts],int numReadings, int numPorts){
  // initialize all the readings to an initial read value from each port
  for (int j=0; j< numPorts; j++){
    int initReading=analogRead(j);
    for (int i = 0; i < numReadings; i++) readings[i][j] = initReading;
    total[j]=numberofReadings*initReading;
    average[j]=initReading;
  }
}
float GetNewAverage(int readings[][numPorts], int numReadings, int numPorts){
  for (int j=0;j= numReadings) {
      // ...wrap around to the beginning:
      readIndex = 0;
    }
  }
}

errors:

Func7_2_dimensional_array:8: error: expected ',' or '...' before numeric constant Func7_2_dimensional_array:9: error: expected ',' or '...' before numeric constant Func7_2_dimensional_array.ino: In function 'void setup()': Func7_2_dimensional_array:8: error: too many arguments to function 'void InitArray(int ()[2], int)' Func7_2_dimensional_array:16: error: at this point in file Func7_2_dimensional_array.ino: In function 'void loop()': Func7_2_dimensional_array:9: error: too many arguments to function 'float GetNewAverage(int ()[2], int)' Func7_2_dimensional_array:20: error: at this point in file Func7_2_dimensional_array:20: error: incompatible types in assignment of 'float' to 'float [2]' Func7_2_dimensional_array:22: error: call of overloaded 'print(float [2])' is ambiguous C:\Documents and Settings\RCM\Desktop\Calorimeter development environment\arduino-1.0.5\hardware\arduino\cores\arduino/Print.h:58: note: candidates are: size_t Print::print(char) C:\Documents and Settings\RCM\Desktop\Calorimeter development environment\arduino-1.0.5\hardware\arduino\cores\arduino/Print.h:59: note: size_t Print::print(unsigned char, int) C:\Documents and Settings\RCM\Desktop\Calorimeter development environment\arduino-1.0.5\hardware\arduino\cores\arduino/Print.h:60: note: size_t Print::print(int, int) C:\Documents and Settings\RCM\Desktop\Calorimeter development environment\arduino-1.0.5\hardware\arduino\cores\arduino/Print.h:61: note: size_t Print::print(unsigned int, int) C:\Documents and Settings\RCM\Desktop\Calorimeter development environment\arduino-1.0.5\hardware\arduino\cores\arduino/Print.h:62: note: size_t Print::print(long int, int) C:\Documents and Settings\RCM\Desktop\Calorimeter development environment\arduino-1.0.5\hardware\arduino\cores\arduino/Print.h:63: note: size_t Print::print(long unsigned int, int) Func7_2_dimensional_array.ino: At global scope: Func7_2_dimensional_array:27: error: expected ',' or '...' before numeric constant Func7_2_dimensional_array:36: error: expected ',' or '...' before numeric constant Func7_2_dimensional_array.ino: In function 'float GetNewAverage(int ()[2], int)': Func7_2_dimensional_array:39: error: invalid types 'int ()[2][int [2]]' for array subscript Func7_2_dimensional_array:41: error: invalid types 'int ()[2][int [2]]' for array subscript Func7_2_dimensional_array:43: error: invalid types 'int ()[2][int [2]]' for array subscript Func7_2_dimensional_array:45: error: incompatible types in assignment of 'int*' to 'int [2]' Func7_2_dimensional_array:47: error: ISO C++ forbids comparison between pointer and integer Func7_2_dimensional_array:49: error: incompatible types in assignment of 'int' to 'int [2]'

BobKay: The compiler is right. A const int is a variable.

Nonsense.

float GetNewAverage(int readings[][numPorts], int numReadings, int numPorts){

Here you promised the compiler that your function would return a float.

Are your pants a tad warm?

total = total + readings[readIndex][j];

Can I suggest that you’re also a little shaky on 1d arrays? (Hint: readIndex, total)

Here's what you wrote:

#define numReadings 10
#define numPorts 2
...
float GetNewAverage(int readings[][numPorts], int numReadings, int numPorts){
...
}

Here's what the compiler sees after the pre-processor does its thing:

...
float GetNewAverage(int readings[][2], int 10, int 2){
...
}

See the problem?

Regards, Ray L.

OK I do appreciate your patience Guys.

I'll pick up your points later please Ray L and AWOL

Firstly who is correct out of AWOL and BobKay

Should I be using

int const numPorts =2;

or

#define numports 2

I can then sort that out and move on to the other points I will also get one function working first and use that the inform changes to the second function. small steps at a time!

TIA

Bob

I don't know if you can use int const numPorts =2;, but you most certainly can use const int numPorts =2;

You can use the #define method, but this is C++, and the preprocessor is so C, and, as you've seen, it can trip up the unwary.

You define readIndex as an array:

int readIndex[numPorts];

but towards the bottom of the program you use:

  • readIndex = 0;*

Most compilers are not going to like that. If the compiler did what you’re trying to do with that statement, it would move the array to memory location 0. My guess is that there’s some pretty important stuff already stored there.

econjack:
You define readIndex as an array:

int readIndex[numPorts];

but towards the bottom of the program you use:

  • readIndex = 0;*

Most compilers are not going to like that. If the compiler did what you’re trying to do with that statement, it would move the array to memory location 0. My guess is that there’s some pretty important stuff already stored there.

The compiler shouldn’t allow that, and will give an invalid lvalue assignment error

Ok I will stick with

const int numPorts=2;

I've cut the code down to one function and one call in set up and nothing in loop()

to keep it simple.

It may now be more a case of me not understanding function calls properly?

we now have

//-----------------
//set up the global variables
const int numReadings =10;
const int numPorts =2;
long numberofReadings=numReadings;
int readings[numReadings][numPorts];      // the readings from the analog input ports
int readIndex[numPorts];// the index of the current reading
float total[numPorts];                  // the running totals for each port
float average[numPorts];                // the average for each port


void setup() {
  // initialize serial:
  Serial.begin(9600);
  analogReference(EXTERNAL);
  InitArray(readings,numReadings,numPorts); //call function to pre-fill the arrays 
}

void loop() {
}
void InitArray(int readings[][numPorts],numReadings,numPorts){
  // initialize all the readings to an initial read value from each port
  for (int j=0; j< numPorts; j++){
    int initReading=analogRead(j);
    for (int i = 0; i < numReadings; i++) readings[i][j] = initReading;
    total[j]=numberofReadings*initReading;
    average[j]=initReading;
  }
}

and Errors:

Func9_2_dimensional_array:7: error: 'numPorts' was not declared in this scope Func9_2_dimensional_array:7: error: 'numReadings' has not been declared Func9_2_dimensional_array:7: error: 'numPorts' has not been declared Func9_2_dimensional_array.ino: In function 'void setup()': Func9_2_dimensional_array:17: error: invalid conversion from 'int (*)[2]' to 'int' Func9_2_dimensional_array:7: error: too many arguments to function 'void InitArray(int, int)' Func9_2_dimensional_array:17: error: at this point in file Func9_2_dimensional_array.ino: At global scope: Func9_2_dimensional_array:22: error: 'numReadings' is not a type Func9_2_dimensional_array:22: error: 'numPorts' is not a type

Which version of the IDE are you using and what board are you compiling for?

void InitArray(int readings[][numPorts],int numReadings, int numPorts){

AWOL:
The compiler shouldn’t allow that, and will give an invalid lvalue assignment error

Which is why I said most compilers won’t like that. It’s trying to replace a constant lvalue. To a beginner, which is easier to understand: The compiler won’t like that, or You’re trying to reassign a constant lvalue? A beginner probably doesn’t even know what an lvalue is.

AWOL: Which version of the IDE are you using and what board are you compiling for?

I am using ver 1.0.5 and the target is the UNO board

9fingers: I am using ver 1.0.5 and the target is the UNO board

Good grief...you're way behind. I'd update your IDE to 1.8.1.

econjack: Good grief...you're way behind. I'd update your IDE to 1.8.1.

Sounds a bit like suggesting I need to change the engine when the headlamps are not bright enough!

I'm not using any of the latest hardware and only using quite basic features of the language.

Sounds a bit like suggesting I need to change the engine when the headlamps are not bright enough!

Different IDEs will give different error messages, which is why I asked; I couldn't match the error messages with what I was seeing.

only using quite basic features of the language.

sp. "misusing"

AWOL: Different IDEs will give different error messages, which is why I asked; I couldn't match the error messages with what I was seeing. sp. "misusing"

No problem. My reply was directed to Econjack.

I could set up a fresh version of IDE on another machine if it helps you to help me. Let me know your preference.

Yup "Misusing" is probably more correct but I'm hoping to improve! I've not done any serious programming since the mid 70s using Algol 60 but that is long forgotten I'm afraid.

I'm not saying an update will cause the code to compile, but at least we'll be singing from the same hymn-sheet.

OK Awol, Which version would you suggest I install 1.8.1 in the interest of musical harmony or something else?