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

int main() {

  FILE* file = fopen ("fourth.txt", "r");
  int i = 0;
  int rows=0;
  int rows2=0;
  int columns=0;
  int counter=0;
  int length=0;
  int multiply=0;

  fscanf (file, "%d", &i);
  rows=i;

  printf("the rows are %d\n", i);

  int matrix[rows][rows];
  length=rows*rows;

  if (rows<=0) {

      printf("error, this file cannot be processed");
      return 0;
  }

  do
  {
      if (fscanf(file, "%d", &i)==1) {

          if (counter<length) {

              matrix[rows2][columns]=i;
              counter++;
              columns++;

              if (columns==rows) {
                  rows2++;
                  columns=0;
              }
          }
          multiply=i;
      }
  } while (!feof(file));

  fclose (file);

  for ( int c = 0; c < rows; c++ ) {
      for ( int j = 0; j < rows; j++ ) {
          printf("matrix[%d][%d] = %d\n", c,j, matrix[c][j] );
       }
  }

  printf("the multiply is %d", multiply);

I am trying to create a matrix exponential (matrix multiplication in another words). However before I perform the actual mathematical process, I have to read numbers as input from a txt file and store them in a 2d array. Above is what I have from the code so far, however, I am struggling to actually input the numbers into the array because when I was testing my 2d array out at the very end, I was getting odd results.

for example: 1. Inputs:

3
123
456
789
2

The 3 represents the number of rows, which is the same as the number of columns for this particular 2d array because the matrix is square, meaning all rows and columns are the same.

The 2 represents the exponent, which is the number of times I have to multiply the original matrix by itself.

However my output is

matrix[0][0] = 123
matrix[0][1] = 456,
matrix[0][2] = 789
matrix[1][0] = 2
matrix[1][1] = 4214800
matrix[1][2] = 0
matrix[2][0] = 3
matrix[2][1] = 0
matrix[2][2] = 0

Expected output has to be:

123
456
789

in a 2d array

Is there a reason why?

Also how would I modify the code so no matter what the input is in the txt file regarding different rows and columns, I could still format the proper 2d array. Thanks

Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

0

In your input text file, the digits in a line are not separated in any way. If you need multi-digit numbers, consider separating them by a symbol like space or semicolon.

If the matrix has only single digit whole numbers (ie, numbers from 0 to 9), it would be fine.

But if there the 3 numbers in a line are, say, 21, 13 and 4, in the input file it would be just 21134.

There's no way to find if it was 211,3,4 or 21,1,34 or 2,113,4 or .......

With the fscanf() in your program, each %d will read the entire line considering it to be a single number and assign it to the variable i.

Instead of reading the size first into i and then copying it to rows like

fscanf (file, "%d", &i);
rows=i;

you could directly read into rows

fscanf( file, "%d", &rows);

And read the values like

for(rows2=0; rows2<rows; ++rows2)
{
    if( fscanf(file, "%1d%1d%1d", &matrix[rows2][0], &matrix[rows2][1], &matrix[rows2][2]) != 3)
    {
        perror("Error");
        break;
    }
}

Then read the value for multiply at the end like

fscanf(file, "%d", &multiply);

You may check the return value of fscanf() to check if any error occurred.

You used !feof(file) to check end of file. I don't know much but have a look here.

J...S
  • 5,079
  • 1
  • 20
  • 35