1. ## 2-Dimensional Dynamic Array

Hi,
I tried to get my program to count the number of rows in the input data file and then output back the same data with 7 columns and however many rows. It's giving me all zeros in the output file though, which isn't supposed to happens since the input file is just random numbers, e.g.:

123.45 111.23 33.45 66.345 43545.54 543.54 5435.43

Could someone take a look at the code?

Code:
```	int j , i;
int rows = 1;
float num;
int c;

while ((c = fgetc(fp)) != EOF)
{
if (c == '\n')
{
rows++;
printf("%d\n", rows);
}
}

float **data;
data = (float**) malloc(rows * sizeof(int));
for (int i = 0; i < rows; i++)
{data[i] = (float*)malloc(7*sizeof(int));
}

//declares array with 7 columns and number of rows in file

for (j=0; j< rows; j++) //repeats for max number of columns
{
for (i=0; i< 7; i++)
{
fscanf(fp, "%f", &num);

data[j][i] = num;
}
}

for(j = 0; j < rows; j++) //repeats for max number of columns
{
for (i=0; i< 7; i++)
{
num = data [j][i];

fprintf(fp2, "%f ", num);

}
fprintf(fp2, "\n");
}```
Thanks.

2. Code:
`data = (float**) malloc(rows * sizeof(int));`
What on earth is this supposed to do?

Quzah.

3. Oh sorry, those floats are meant to be integers, I was just trying some stuff and I forgot to change it back. Revised code:

Code:
```int j , i;
int rows = 1;
float num;
int c;

while ((c = fgetc(fp)) != EOF)
{
if (c == '\n')
{
rows++;
printf("%d\n", rows);
}
}

int **data;
data = (int**) malloc(rows * sizeof(int));
for (int i = 0; i < rows; i++)
{data[i] = (int*)malloc(7*sizeof(int));
}

//declares array with 7 columns and number of rows in file

for (j=0; j< rows; j++) //repeats for max number of columns
{
for (i=0; i< 7; i++)
{
fscanf(fp, "%f", &num);

data[j][i] = num;
}
}

for(j = 0; j < rows; j++) //repeats for max number of columns
{
for (i=0; i< 7; i++)
{
num = data [j][i];

fprintf(fp2, "%f ", num);

}
fprintf(fp2, "\n");
}```

4. The data you want to read in looks like floating point to me. So you want to allocate floats, not ints.

5. Originally Posted by juice_box
Code:
```...
while ((c = fgetc(fp)) != EOF)
{
...
for (j=0; j< rows; j++) //repeats for max number of columns
{
for (i=0; i< 7; i++)
{
fscanf(fp, "%f", &num);

data[j][i] = num;
}
}```
Ignoring your problems with malloc(), you're reading to the end of the file, and then reading some more.