error: size of array 'momentumOfparticles' has non-integral type 'float [i]'

  float Particles;
  float MomentumOfParticles;
  int MassOfparticle;
  float VelocityOfparticles;
  int i;
  int randomPick = 0;
void setup() {
  Serial.begin(9600);
  randomSeed(analogRead(A0)); //make random numer A0 pin is not connected 
  float particles[] = {1.0,1.5,2,2.5,3,3.5,4,4.5,6,6.5,7,7.5 ,8,8.5,9,9.5,10.0}; 
  float VelocityOfparticles[i] = {1,2,3,4,5,6,7,8,9,10};
  float MassOfparticle[i] = {1,2,3,4,5,6,7,8,9,10};
  float MomentumOfParticles[MassOfparticle];

  for (int i = 0; i < 10; i++) {
  float MassOfparticle[10] = {1,2,3,4,5,6,7,8,9,10};
  randomPick =  randomNub[random(0,10)];
  }
  for (int i = 0; i < 10; i++) {
  float VelocityOfparticles[10] = {1,2,3,4,5,6,7,8,9,10};
  randomPick =  randomNub[random(0,10)];
  }
  
  
  Serial.println("Particle Momentum; ");
  for (int i = 0; i < MomentumOfParticles; i++) {
      momentumOfparticles[i] = MassOfparticle * VelocityOfparticles[i];
       Serial.println(MomentumOfParticles[i]);
  }
  
}

void loop() {
  // put your main code here, to run repeatedly:

}

hw1.2.ino (1018 Bytes)

Please edit your post Add ``` [code] ``` before the code Add ``` [/code] ``` after the code

It will prevent the forum software from mangling it.

ok did that

Thanks,

  for (int i = 0; i < 10; i++)
  {
    float VelocityOfparticles[i] = random(0, 10);
    randomPick =  randomNub[random(0, 10)];
  }

So you seem to create 10 times a new variable with a size of 0 … 10 ?

The first line inside the block does not refer to the variable that you declared in the beginning of your code; is that the intention?

Time to read up on arrays, I think.

Note:
this is not necessarily related to the error that you get; but I suspect that it relates to the same type of problem.

That is not the only error that you have:

C:\Users\Jacques\Documents\Arduino\sketch_oct01a\sketch_oct01a.ino: In function 'void setup()':

sketch_oct01a:14: error: size of array 'momentumOfparticles' has non-integral type 'float [i]'

   float momentumOfparticles[MassOfparticle];

                                           ^

sketch_oct01a:17: error: array must be initialized with a brace-enclosed initializer

   float MassOfparticle[i] = random(0,10);

                                        ^

sketch_oct01a:18: error: 'randomNub' was not declared in this scope

   randomPick =  randomNub[random(0,10)];

                 ^

sketch_oct01a:21: error: array must be initialized with a brace-enclosed initializer

   float VelocityOfparticles[i] = random(0,10);

                                             ^

sketch_oct01a:22: error: 'randomNub' was not declared in this scope

   randomPick =  randomNub[random(0,10)];

                 ^

sketch_oct01a:28: error: invalid operands of types 'float [i]' and 'float' to binary 'operator*'

       momentumOfparticles[i] = MassOfparticle * VelocityOfparticles[i];

                                                                      ^

sketch_oct01a:29: error: invalid types 'float[int]' for array subscript

        Serial.println(MomentumOfParticles[i]);

                                            ^

exit status 1
size of array 'momentumOfparticles' has non-integral type 'float [i]'

Array size has to be constant of type integer:MomentumOfParticles[12]If you initialise the array like you did with particles[], you leave the brackets empty. The compiler will adjust the size of the array according to the number of elements.

I believe this is the problem.

float MassOfparticle[i] = {1,2,3,4,5,6,7,8,9,10};
float MomentumOfParticles[MassOfparticle];

Although it is not apparent, MassOfparticle, when used as an expression in the second statement is interpreted as a *pointer * to the address of the first array element, which is a float. An array dimension must resolve to a positive integer and floats don’t do this without massaging.

Possibly what you wanted was:

float MomentumOfParticles[sizeof (MassOfparticle) *  sizeof (float)/ sizeof (float)];

This would result in MomentumOfParticles having
(10 x 4 ) / 4 elements. Or, ten elements.

Check out the last paragraph

Your code is a terrible mess. As near as I can figure you just want to multiply the matching members of two lists. The random() stuff was not used at all so I removed it. The array of 17 “Particles” was not used so I commented that line out.

 // const float Particles[17] = {1.0, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 6, 6.5, 7, 7.5 , 8, 8.5, 9, 9.5, 10.0};
const int MassOfParticle[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
const float VelocityOfParticle[10]  = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
float MomentumOfParticle[10];

void setup() {
  Serial.begin(9600);
  Serial.println("Particle Momentum; ");
  
  for (int i = 0; i < 10; i++) {
    MomentumOfParticle[i] = MassOfParticle[i] * VelocityOfParticle[i];
    Serial.println(MomentumOfParticle[i]);
  }
}

void loop() {
  // put your main code here, to run repeatedly:
}

Thx for the help its solved and its working now :)

dougp:

float MomentumOfParticles[sizeof (MassOfparticle) *  sizeof (float)/ sizeof (float)];

This would result in MomentumOfParticles having
(10 x 4 ) / 4 elements. Or, ten elements.

Is that right? If it is an array of 10 floats shouldn’t sizeof return 40***? sizeof always gives size in bytes. I know it certainly looks wrong to multiply and then divide by the same number, that’s just redundant and the compiler probably notices and cuts it out. I think what you were going for was the classic “count the number of elements” code which is more like:

float MomentumOfParticles[sizeof (MassOfparticle) / sizeof (MassOfparticle[0])];

EDIT: ***Actually 20 because MassOfParticle is an array of int not float.

Delta_G: I think what you were going for was the classic "count the number of elements" code which is more like:

float MomentumOfParticles[sizeof (MassOfparticle) / sizeof (MassOfparticle[0])];

EDIT: ***Actually 20 because MassOfParticle is an array of int not float.

Although slightly clunky, what I posted does produce the correct result so maybe the compiler does do some magic there, I don't know.

I was not aware of the 'classic' technique. That's pretty nifty! Thanks! Now I've got to do some more editing.

From OP's inital post:

 float MassOfparticle[i] = {1,2,3,4,5,6,7,8,9,10};

Actually, I think what you posted gives 20, which is larger than he needs so it doesn't end up with any memory being stomped on and it will "work" in his code. But it isn't the right answer. Could write a test code if you'd like.

Delta_G: Actually, I think what you posted gives 20, which is larger than he needs so it doesn't end up with any memory being stomped on and it will "work" in his code. But it isn't the right answer. Could write a test code if you'd like.

No need, I rechecked the little test I'd written and saw a critical error. Cause: Inattention to detail. Mea culpa! :blush:

I think my next project needs to be a mind-reading compiler.

dougp: I think my next project needs to be a mind-reading compiler.

@PaulS has one but he won't share with any of the rest of us. He calls it his "Crystal Ball". Unfortunately it always seems to be out of order.

dougp: I believe this is the problem.

float MassOfparticle[i] = {1,2,3,4,5,6,7,8,9,10};
float MomentumOfParticles[MassOfparticle];

Although it is not apparent, MassOfparticle, when used as an expression in the second statement is interpreted as a *pointer * to the address of the first array element, which is a float. An array dimension must resolve to a positive integer and floats don't do this without massaging.

You are correct it’s a mess and won’t work, but the last part of your explanation is wrong So Just for correctness indeed [ttMassOfparticle[/tt] is the address of the first (Ill defined) array and so it is an integral number. You Don’t really care what is at that address as it’s not referenced

J-M-L: So Just for correctness indeed [ttMassOfparticle[/tt] is the address of the first (Ill defined) array and so it is an integral number.

That makes sense. What is the cause then of "error: size of array 'momentumOfparticles' has non-integral type 'float'" ?

dougp: That makes sense. What is the cause then of "error: size of array 'momentumOfparticles' has non-integral type 'float'" ?

You missed part of the message:

"error: size of array 'momentumOfparticles' has non-integral type 'float [i]'"

It's not saying that the index is a 'float'. It's saying that the index is an array of floats of size 'i'. That array is not an integer.

dougp: That makes sense. What is the cause then of "error: size of array 'momentumOfparticles' has non-integral type 'float'" ?

it's telling you it's a pointer to an array of float which it does not like (but not a float)

johnwasser: It's not saying that the index is a 'float'. It's saying that the index is an array of floats of size 'i'. That array is not an integer.

J-M-L: it's telling you it's a pointer to an array of float which it does not like (but not a float)

It's finally sunk in. Thanks again.