# Thread: Scanning Arrays from a file

1. ## Scanning Arrays from a file

Code:
```fscanf(ifp, "%d", &num_couples);

for(i=0; i<num_couples; i++)
{
for(j=0; j<num_couples; j++)
{
fscanf(ifp, "%s", man_name[j]);
fscanf(ifp, "%s", woman_name[j]);
fscanf(ifp, "%d", &men_ratings[i][j]);
fscanf(ifp, "%d", &women_ratings[i][j]);
}
}```
Below is what I put for my declarations because the male and female names are supposed to be strings of at most 19 characters and the max number of couples is 10. Also the men and the women will rate each other from 1-10. idk if that is enough info. It's a matching and permutation problem, but does the scanning and dimensions look right at all? I don't know if everything should be a 2d array.

int men_ratings[10][10], women_ratings[10][10];
char man_name[20], woman_name[20];

2. Originally Posted by Hikaru90
Code:
```fscanf(ifp, "%d", &num_couples);

for(i=0; i<num_couples; i++)
{
for(j=0; j<num_couples; j++)
{
fscanf(ifp, "%s", man_name[j]);
fscanf(ifp, "%s", woman_name[j]);
fscanf(ifp, "%d", &men_ratings[i][j]);
fscanf(ifp, "%d", &women_ratings[i][j]);
}
}```
Below is what I put for my declarations because the male and female names are supposed to be strings of at most 19 characters and the max number of couples is 10. Also the men and the women will rate each other from 1-10. idk if that is enough info. It's a matching and permutation problem, but does the scanning and dimensions look right at all? I don't know if everything should be a 2d array.

int men_ratings[10][10], women_ratings[10][10];
char man_name[20], woman_name[20];
If each for loop iterates 10 times, then the inner loop will be iterating 100 (10 x 10) times, which seems a bit much.

You need only two arrays: 1 for names, and one for ratings. You can then separate the men from the women by a simple "flag" of sentinel value:

names:

Joe
Jim
Jack
ZZZ //flag value
Jill
Jenny
Josephine
...
...
...

You can do the same thing with ratings, but use a -99 for your flag value

5 6 7 6 4 8 7 5 8 6 //Joe's ten ratings

//rest of the men's ratings
-99
//start of the womens ratings

Both arrays should have 2 dimensions, so:

Joe 5 6 7 6 ... etc. are both on the same index number

Names will go on the outer loop only, ratings will go on the inner loop, only.