Filling a 2d array from file, where the parameters are the first two values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Umair Nasir
    New Member
    • Nov 2010
    • 8

    #1

    Filling a 2d array from file, where the parameters are the first two values

    The data file is
    6 3
    6 7 8
    1 2 3
    7 8 9
    3 4 5
    8 9 0
    1 6 8
    ------------
    #include <stdio.h>
    #include <stdlib.h>

    int
    main (void)
    {
    int a, b,i=0,j=0, x[i][j];
    FILE *f1;

    f1 = fopen ("mat1.dat", "r");

    fscanf (f1, "%d%d", &a, &b);

    printf ("Rows: %d, Columns: %d\n", a, b);



    for (int i=0; i<a; i=i+1)
    {
    for (int j=0; j<b; j=j+1)
    {
    fscanf (f1, "%d", &x[i][j]);
    }
    printf ("\n");
    }

    fclose (f1);



    return (0);
    }

    It doesnt work. How do i read the matrix and ignore the first two values? Is the code correct?
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    What is your question?

    Comment

    • Umair Nasir
      New Member
      • Nov 2010
      • 8

      #3
      I keep getting
      1 6 8
      1 6 8
      1 6 8
      1 6 8
      1 6 8
      1 6 8

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Take a look at Arrays Revealed.

        Are you using C or C++?
        If C, do you know if you are using C99?

        You declare x as having 0 rows and 0 columns, but then you go on to store values in lots of rows and columns.

        Comment

        • Umair Nasir
          New Member
          • Nov 2010
          • 8

          #5
          when i put int x[i][j], it doesnt run for some reason

          Comment

          • Umair Nasir
            New Member
            • Nov 2010
            • 8

            #6
            must i use calloc?

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Uisng x[i][j] is OK.

              What's not OK is that your array does not have 6 elements of 3 ints per element to match your incoming data.

              Comment

              • Umair Nasir
                New Member
                • Nov 2010
                • 8

                #8
                The first two, 6 and 3 are the parameters, so how do i input the numbers without that?

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  Did you read the article "Arrays Revealed" that donbock ask you to do?

                  The solution to your problem is about the last example in the article- and it's coded for you already.

                  Take a look and post again if you are still stuck.

                  Comment

                  • Umair Nasir
                    New Member
                    • Nov 2010
                    • 8

                    #10
                    I am really not understanding it. Why doesnt it work when i put it as x[i][j] and why does it only scan the last line of the file? It was done as such in class, and i dont know if i need to use pointers

                    Comment

                    • weaknessforcats
                      Recognized Expert Expert
                      • Mar 2007
                      • 9214

                      #11
                      Yes you will need to use pointers. The dimension ofyour array is 6 rows of 3 int. That makes 18 int.

                      Your first job is to allocate an array of 18 int.

                      Then you typecast a pointer to that array as a pointer to an array of 6 elements with 3 int per element.

                      This is truly coded for you in that Arrays Revealed article.

                      Comment

                      • Umair Nasir
                        New Member
                        • Nov 2010
                        • 8

                        #12
                        I have tried that, the problem im having isnt with creating such an array. Once i have created it, it doesnt seem to work, even with the use of pointers. The problem is scanning the numbers from the file.

                        Comment

                        • Umair Nasir
                          New Member
                          • Nov 2010
                          • 8

                          #13
                          #include <stdio.h>
                          #include <stdlib.h>

                          int
                          main (void)
                          {
                          int nrows, ncols,i,j;
                          int *numbers;
                          FILE *f1;

                          f1 = fopen ("mat1.dat", "r");

                          fscanf (f1, "%d%d", &nrows, &ncols);

                          printf ("Rows: %d, Columns: %d\n", nrows, ncols);

                          /* allocating the array of integers */
                          numbers = (int *) calloc (nrows*ncols, sizeof(int));

                          /*for (i=0; i<nrows; i=i+1)
                          {
                          for (j=0; j<ncols; j=j+1)
                          {
                          fscanf (f1, "%3d", numbers[i*ncols+j]);
                          }
                          }
                          */
                          for (i=0; i<nrows; i=i+1)
                          {
                          for (j=0; j<ncols; j=j+1)
                          {
                          printf ("%3d ", numbers[i*ncols+j]);

                          }
                          printf ("\n");
                          }

                          free (numbers);
                          fclose (f1);



                          return (0);
                          }
                          In this, everything works other than the inputting of the data from the file. I can create a 6/3 matrix. But when its being filled, it wont run

                          Comment

                          • weaknessforcats
                            Recognized Expert Expert
                            • Mar 2007
                            • 9214

                            #14
                            Try this:

                            Code:
                            fscanf (f1, "%d", &numbers[i*ncols+j]);
                            With fscanf, you need the address of the target.

                            Comment

                            Working...