Create An Array and Iterative Formula Program in C

How to create an array and calculate its elements using an iterative formula in C?

Can you explain how to print the array and the numbers with odd integer parts separately?

Final answer:

This C program demonstrates how to create an array, calculate its elements using an iterative formula, and print out all elements and those with odd integer parts separately.

Explanation:

This question requires us to write a gcc C program, compile and run it in Ubuntu. The program should create an array named 'zeta' of size 30 that holds floating point numbers. The first 3 elements are 1.1, 2.2, and 3.3. The nth element of the array is calculated using the iterative formula zeta(n)=0.5*zeta(n−3)+0.3*zeta(n−2)+0.2*zeta(n−1).

The program should then use a loop to print the complete array of 30 elements, with each value separated by a comma. On the next line of the output, the program should print only the numbers in the array that have an odd integer part. The odd integer part can be obtained by casting the float to an integer and checking if the result is odd.

An example of a simple implementation in C could look like the following:

#include
float zeta[30];
int main(){
int i;
zeta[0] = 1.1;
zeta[1] = 2.2;
zeta[2] = 3.3;
for(i=3;i<30;i++){
zeta[i] = 0.5*zeta[i-3] + 0.3*zeta[i-2] + 0.2*zeta[i-1];
}
for(i=0;i<30;i++){
printf('%f,', zeta[i]);
}
printf('
');
for(i=0;i<30;i++){
if((int)zeta[i]%2 != 0){
printf('%f,', zeta[i]);
}
}
return 0;
}

It's essential to have knowledge about C language, algorithms, loops, and arrays to solve this problem.

← Rapid development of programs advantages and drawbacks Nested if statement mastering conditional logic in programming →