Reputation: 81
I have an assignment that requires us to read a CSV file, and store each row of data in a single index in a double
array. However, unless I'm mistaken, it's not possible to store multiple values in a single index in an array in C. Instead, I would have to use a struct
for that.
Can anyone confirm if it is possible store multiple values in a single index in an array in C?
Here is the input CSV file:
1.5,-1.5,3
-1,1.5,3
1,-1,7
1,1,3
-1.5,-1.5,3
-1,0.5,3
0.5,0.5,3
-1,-1,8
-3,1,8
-1.7,1,8
1.8,1,0
eg. for the first line, can I store 1.5
and -1.5
and 3
in array[0]
?
Upvotes: 0
Views: 1028
Reputation: 123558
You’ll want to use a 2-dimensional array:
#define ROWS 9
#define COLS 3
...
double data[ROWS][COLS];
and read each row as
if ( fscanf( input, "%lf,%lf,%lf", &data[i][0], &data[i][1], &data[i][2] ) != 3 )
{
// error during input, handle as appropriate
}
Thus, each data[i]
is itself a 3-element array of double
.
Upvotes: 1