Which one you want? C, C++ or C#?
Main Topics
Browse All TopicsI am having trouble reading items into a multidimensional array. I have a file that is always in this format below:
3
1.2 2.4
3.5 1.2
0.4 5.6
The top line indicates how many items there are (3) in the file and will be stored to a non-array variable. The remaining lines 2-4 are the items needing to be processed into the array. So 1.2 and 2.4 need to go in array[1][1] and 3.5 and 1.2 go in array[2][2] and so forth so that they correspond to each other.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Is that what you want to happen ?
Are you sure you need a 2D array ? Why not put the two values in a struct, and make a 1D array of this struct ? It provides a closer coupling of related data, plus it will make the code more clear.
Can you show the code you already have, so we know what it is that you need help with exactly ?
>> sscanf(line, "%3f2", &size_arr);
What did you mean by %3f2 ?
What's line ? Is it supposed to be an array of char ?
Did you mean to read from the file using fscanf ? Or did you mean to use fgets first to read a line from the file into a line buffer ?
Did you mean to use %d for an int ?
>> double a[size_arr][size_arr];
Every row contains two values, so there's no need to have more than 2 columns in the 2D array.
You'll also have to allocate the array dynamically, using calloc for example :
http://www.cplusplus.com/r
Don't forget to free the allocated memory as soon as you don't need it any more :
http://www.cplusplus.com/r
>> for(i=0; i <= size_arr; i++)
>> for (i=0; i <= size_arr; i++)
These nested for loops use the same loop counter. I'm sure that's not what you intended.
You don't even need a loop here, since all you want to do, is read two values from the line and put them in the 2D array at the correct location.
>> a[i][j] = fi;
You can't simply assign the FILE* ... you'll need to get the values from the line, and store the values.
>> double arr[num_arr][num_arr];
As I said, you'll need to allocate memory dynamically. See my previous post for more details.
You'll also need only 2 columns. There are num_arr rows and 2 columns.
>> for(i=0; i <= num_arr; i++)
>> for(j=0; j <= num_arr; j++)
Same here : num_arr rows, and 2 columns only.
Also note what Let_Me_Be said about the upper limit of the loop counter. The last element in an array of size n, is at index (n - 1), not at index n.
>> This concrete feature should be provided by all up to date C compilers.
There's a difference between "should" and reality.
gcc has a C99 compilation mode (which is not fully conforming yet : http://gcc.gnu.org/c99stat
Or worse, afaik, MSVC doesn't support it at all, let alone that it conforms to C99.
So, I wouldn't yet depend on C99 being supported by all compilers ;)
Business Accounts
Answer for Membership
by: Infinity08Posted on 2009-07-16 at 08:04:40ID: 24869861
I'm not sure I understand what you're trying to do.
>> So 1.2 and 2.4 need to go in array[1][1]
You put two values in the same position in the array ? Are you using a struct for that ? Could you show the code you already have ?
>> and so forth so that they correspond to each other.
What do you mean by "correspond" ?
Is there any reason that you're only filling in items on the diagonal of the 2D array ?