Thread: Matrix Multiplication ( Accessing data from a file ) HELP

1. Matrix Multiplication ( Accessing data from a file ) HELP

HI I am a new c programmer, working on a program that multiplies matrices.

the program is suppose to read number from a file,

The first line of a file contains an integer number, say n. This number is then followed by 2n rows of integer numbers. Each row contains n integer numbers. The first n rows of numbers represent the values in a matrix A. The second n rows of numbers represent the values of the matrix B. Matrix A and matrix B are both n x n . Write a C progam that will multiply these two matrices together forming a result matrix that is also n x n. The value of n will not be greater than
50. file comes in through argv[1].

The puzzling part of this problem is how am i going to read the first line from the file and malloc up space for the matrix? secondly, if the first line of my file is the dimensions of my matrix, please tell me what c function can be used to read the first line of my file for the matrix dimension and then reads the next subsequent lines as data for my matrix.

Any suggestions would be greatly appreciated!

Thankyou!

2. Well from what I undeerstand read the nuber n then do something like: malloc(n*2*sizeof(int));
Or, if its sqaured: malloc(n*n*sizeof(int));
Or, if its 2 arrays squared: malloc(n*n*2*sizeof(int));
I'm not really sure what youre on about here, lol.

Edit: If its reaading from a file you have problems with: http://faq.cprogramming.com/cgi-bin/...&id=1043284392

3. You would do it exactly as you said it. Read the first number, and then start malloc()ing. Since the data sounds very well formatted, I would consider using for all read operations in your program fscanf() or, perhaps more preferably, fgets() + sscanf().

Write functions to do the work for you. For example, write a function to read a matrix from a file. That way you can call it twice and read two matrices quite easily.