Fix the program
This program isn't compiling. Think about why, and come with a solution to fix it.
#include <iostream>
int main()
{
int size = 5;
float farray[size];
farray[0] = 1;
farray[1] = 3;
farray[2] = 5;
farray[3] = 7;
farray[4] = 9;
for (int i = 0; i < 5; i++)
std::cout << farray[i] << " - ";
}
1 - 3 - 5 - 7 - 9 -
Answer
The array size isn't known at compile time.
The size of the array has to be known at compile time, and the value 5 is assigned to size when the program runs, not when it compiles.
You might be able to tell what the value of size is when the code execution reaches float farray[size] but without running the program, the compiler can't be sure.
To fix this program we would simply use an array size that's known at compile time.
#include <iostream>
int main()
{
int size = 5;
float farray[5];
farray[0] = 1;
farray[1] = 3;
farray[2] = 5;
farray[3] = 7;
farray[4] = 9;
for (int i = 0; i < 5; i++)
std::cout << farray[i] << " - ";
}