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

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    2

    Question 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!
    Last edited by six_degreez; 07-24-2008 at 05:09 PM. Reason: typo

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    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. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  3. Editing a data file
    By Strait in forum C++ Programming
    Replies: 7
    Last Post: 02-05-2005, 04:21 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. File Database & Data Structure :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 02-24-2002, 11:47 AM