Write a program that declares an array squares of 50 components of type double. Initialize the array so that the first 25 components are equal to the square of the index variable, and the last 25 components are equal to the square root of the index variable. Output the array so that 10 elements per line are printed using two decimal places. All array operations are to be done in main().
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
double list[50];
double j;
int i;
for (i = 0; i < 25; i++)
list[i] = i*i;
for (i = 25; i < 50; i++)
j = i;
list[i] = sqrt(j);
for (i = 0; i < 50; i++)
cout << list[i] << " ";
return 0;
}
I realize that the sqrt() function must use a float or double. I am iterating the array with an integer, and as far as I know it must be an integer so I cant change "i". I am trying to convert "i" into something I can use but it is not working. I have also tried static cast and that didn't work either.
ahh, i get it you can't use the index variable for the the array and your for loop. you need a separate array variable. cheers! list[i] will never work.