Generation

generate functionSat, 29 Jul 2023

calculate moving average of an integer over 10 values.

int moving_average(int a) { static int values[10] = {0}; static int index = 0; values[index] = a; int sum = 0; for (int i = 0; i < 10; ++i) { sum += values[i]; } index = (index + 1) % 10; return sum / 10; } moving_average(1); // 1 moving_average(2); // (1 + 2) / 2 moving_average(3); // (1 + 2 + 3) / 3 ...

Want to kickstart your project?Use the new AI Studio to create your code