How to increment a for loop with a decimal value in C
c, for-loop, increment, loops
Solution
Simple Solution: Multiple by your desired gain
double gain = 0.1;
for (k=0; k<BUFFER_LEN; k++) {
buffer[k] = sin(gain * 2*pi*(f/fs)*k); //sine generation
}
No need to change your k loop, BUFFER_LEN & no floating point issues. 1/Gain does not need to be an integer.
Your original problem was likely due to:
int k;
for (k=0; k<BUFFER_LEN; k += 0.1) {
buffer[k] = sin(2*pi*f/fs*k); //sine generation
}
In this case, the `k += 0.1` did `k = (int) (k + 0.1)` which truncates the sum back to the original k. Thus your loop runs forever.
Problem
I have this code ``` for (k=0; k<BUFFER_LEN; k++){ buffer[k] = sin(2*pi*f/fs*k); //sine generation ``` my loop increments by 1 each time - so k will be 1, 2, 3, 4, 5.... etc for each calculation I would like the loop to increment by 0.1 each time for example, so my sine calculation is more accurate? What would be the simplest way to achieve this? I tried incrementing by 0.1 in that for loop but dont think this is allowed as the program times out edit: here is a solution ``` int i, k; float z=0.1; for(i = 0; i < BUFFER_LEN; i++){ // fill the buffer buffer[k] = sin(2*pi*f/fs*z); // sine wave value generation z = z + 0.1; } ```