# Thread: Two dimensional array not changing values

1. ## Two dimensional array not changing values

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```

2. You should try to not do so much at once. Write code that initializes the first column first and once you get that working, proceed with the rest of the program.

Edit : Consider something more like this :
Code:
```#include <stdio.h>
#include <stdlib.h>

int main( void )
{
double x[ 12 ][ 5 ] = { { 0 } };

for ( size_t i = 0; i < 12; ++i )
{
x[ i ][ 0 ] = 2 + .1 * i;
}

for ( size_t i = 0; i < 12; ++i )
{
printf( "%.4lf\n", x[ i ][ 0 ] );
}

return 0;
}```
This code was compiled with :
Code:
`gcc -std=c11 -Wall -Wextra -pedantic -O3 -o array array.c`
and the output is :
Code:
```2.0000
2.1000
2.2000
2.3000
2.4000
2.5000
2.6000
2.7000
2.8000
2.9000
3.0000
3.1000```