Even number outputter
Make a program that has an array of floats of size 100. Use a for-loop to fill the number of the array with the value that's half of their index.
So that farray[50] == 25.f, farray[5] == 2.5f, and so on.
Using another for-loop, loop through the array and print the value, index, and inferred index (based on the value).
Make sure that the index and the inferred index matches.
Expected output
index: 0
value: 0
inferred-index: 0
---
index: 1
value: 0.5
inferred-index: 1
---
...
index: 98
value: 49
inferred-index: 98
---
index: 99
value: 49.5
inferred-index: 99
Answer
#include <iostream>
int main() {
float farray[100];
// Fill the array
for (int i = 0; i < 100; ++i) {
farray[i] = i * 0.5f;
}
// Output the array
for (int i = 0; i < 100; ++i) {
std::cout << "index: " << i << "\n";
std::cout << "value: " << farray[i] << "\n";
std::cout << "inferred-index: " << farray[i]*2 << "\n";
std::cout << "---\n";
}
}