Data types & arrays

Hello, I do have the following code

float sampleRate = 44100.00;
float sec = 1000.00;
float T[] = {};

float quantum() {
float q = (1.00 / sampleRate) * sec;
return q;
}

void setup() {
Serial.begin(9600);
for(int i=0; i<=44100; i++) {
float j = float(i);
float fraction = quantum() * j;
T = fraction;

  • }*
  • Serial.println(T[44100]);*
    }
    void loop() {}
    When I open the Serial gate, it returns the next:
    àüà
    So, What do I do to get the real value?
    Board used: Arduino Uno Rev. 3
float T[] = {};

This declares an empty array so that T[0] will cause a run time error and your sketch to either crash or behave unpredictably.

If you want to declare an empty array then it should be like this:

float T[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};

The above declares an array of size 9. Where all the elements are initialized to the value of 0. Or you can initialize each element to what ever value you please.

So that T[0].......T[8] is valid.

float T[] = {};

The compiler sizes the array, when you don't, based on the number of initializers you supply (0).

A zero element array is useless.

   float j = float(i);

In the old days, single letter names in the range i to m were always integers. There is NO reason to create a float from an int to assign an int to a float.

 Serial.println(T[44100]);

You don't, and can't possibly, have that many elements in the array.

So, What do I do to get the real value?

Provide adequate space in the array so you are storing data in the array, not sh*tting all over memory you don't own.

Also what board are you using? 44,000 samples at 4 bytes each is going to take up way too much space.

hapit:
So, What do I do to get the real value?

Board used: Arduino Uno Rev. 3

Since you are only using one element of the array and that element doesn't rely on any other element you can calculate it directly. The answer is 1000.00

float sampleRate = 44100.00;
float sec = 1000.00;

float quantum() {
  return (1.00 / sampleRate) * sec;
}

void setup() {
  Serial.begin(9600);
  
  unsigned i = 44100;  // Note: Won't fit in a signed int.
  float fraction = quantum() * i;
  Serial.println(fraction);
}

void loop() {}