Scanning Arrays from a file

This is a discussion on Scanning Arrays from a file within the C Programming forums, part of the General Programming Boards category; 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", ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    25

    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. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Hikaru90 View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21