Sorry, I only answered 1/3 of your question.
You never give
i a value -- hence when you use it, the compiler complains that it is not initialized. It's value could be
anything when your program runs.
The variable-length array problem is because the array size has to be known at the time of compilation.
i and
j are automatically invalid, but so are
ci and
cj -- because their initializer is not a compile-time constant.
Hence, it is a "variable length array" -- the length of the array is not known at compile time.
IIRC, C++14 will support VLAs. Until then, you must compile such things with compiler-specific extensions.
Or, just do it the standard C++ way and use a vector.
1 2 3 4
|
vector <int> ac( ci );
if (ci > 0)
cout << ac[0] << endl;
|
Or dynamic allocation and all the attendant boilerplate:
1 2 3 4 5 6 7 8 9 10 11 12
|
int* ac = new int[ ci ];
try
{
if (ci > 0)
cout << ac[0] << endl;
}
catch (...)
{
delete [] ac;
throw;
}
delete [] ac;
|
Hope this helps.