hi i need help scanning values from a file into a 2d dynamic array
i attached the file i need to scan
and heres my code so far
the array should be 7X6
Code:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "linear_algebra.h"
int main(){
int n,i,j;
FILE * ifile;
ifile = fopen ("points.csv","r");
fscanf(ifile, "%d\n", &n);/*read in the number of points n.*/
printf("%d\n", n);
double **a; /* this is the array of points name */
int num_rows; /* this variable will be used for the first dimension */
int num_col; /* this variable will be used for the second dimension */
num_rows = n;
num_col = 6;
/* allocate storage for an array of pointers */
a = malloc(num_rows * sizeof(double *));
/* for each pointer, allocate storage for an array of doubles */
for (i = 0; i < num_rows; i++) {
a[i] = malloc(num_col * sizeof(double));
}
/* assign the value to each element */
for (i = 0; i < num_rows; i++) {
for (j = 0; j < num_col; j++) {
fscanf(ifile,"%lf\n", &a[i][j]);
}
}
/* now show the contents that were assigned */
for (i = 0; i < num_rows; i++) {
for (j = 0; j < num_col; j++) {
printf("%lf",a[i][j]);
}
}
/* now for each pointer, free its array of dbles */
for (i = 0; i < num_rows; i++) {
free(a[i]);
}
/* now free the array of pointers */
free(a);
fclose(ifile);
return 0;
}