The book that I'm reading has end of chapter exercises. And for this one it wants me to
"Define a two-dimensional array, data[12][5], of type double. Initialize the elements in the first column with values from 2.0 to 3.0 inclusive in steps of 0.1. If the first element in a row has
the value x, populate the remaining elements in each row with the values 1/x, x^2 , x^3 , and x^4 . Output
the values in the array with each row on a separate line and with a heading for each column."
So I wrote this, which isn't working as the output either equal to 1 or 0. Any ideas what I did wrong?
Code:
Code:
#include <stdio.h>
#include <math.h>
int main(void)
{
double data[12][5]={0}; /*initialize array*/
for(int i = 0; i < 12; i++) /*loop 12 times*/
{
data[i][0]= 2.0+(0.1*i); /*first element should go up by 0.1 for each dimension(??)*/
for(int i2 = 0; i2 < 5; i2++)
{
if(i2 == 0)
{
data[i][i2]+= 0; /*leave unchanged*/
}
if(i2 == 1)
{
data[i][i2]= 1/data[i][0]; /*divide 1 by the value held in data[i][0]*/
}
else
{
data[i][i2]= pow(data[i][0], i2); /*set it the the appropriate power*/
}
}
}
printf("data-1|data-2|data-3|data-4|data-5\n"); /*output*/
for(int i = 0; i < 12; i++)
{
printf("%.4lf|%.4lf|%.4lf|%.4lf|%.4lf\n", data[i][0], data[i][1], data[i][2], data[i][3],data[i][4]);
}
return 0;
}
Output:
Code:
data-1|data-2|data-3|data-4|data-5
1.0000|1.0000|1.0000|1.0000|1.0000
1.0000|1.0000|1.0000|1.0000|1.0000
1.0000|1.0000|1.0000|1.0000|1.0000
1.0000|1.0000|1.0000|1.0000|1.0000
1.0000|1.0000|1.0000|1.0000|1.0000
1.0000|1.0000|1.0000|1.0000|1.0000
1.0000|1.0000|1.0000|1.0000|1.0000
1.0000|1.0000|1.0000|1.0000|1.0000
1.0000|1.0000|1.0000|1.0000|1.0000
1.0000|1.0000|1.0000|1.0000|1.0000
1.0000|1.0000|1.0000|1.0000|1.0000
1.0000|1.0000|1.0000|1.0000|1.0000