#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    2
    Rep Power
    0

    reading in matrices as an array[i,j]


    If i have a data file which the first line contains the row column information of a matrix, followed by on the next line
    the actual matrix data.

    Im having trouble reading in the values of the matrix into an array

    ive been told the [] has to be overloaded 2 times,


    int row, int col

    infile>>row>>col;

    for (i=0,i<row, i++)
    for (j=0,j<col, j++)

    would this be sufficient enough to read in this matrix
    -from data file
    5 3 //row col

    2 3 4
    8 5 9
    7 3 7
    8 3 8
    9 2 4

    and i would like to store this in array size (row*col) -1

    Can anyone offer me any hints or suggestions on how i would go about doing this.

    my main goal is to be able to read in matrixes from a data file, add, subtract them, view any [i,j] position of any matrix

    thanks
    sorry for being a N00b
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    68
    Rep Power
    11
    Assuming you're using vanilla arrays, the file appears to be a simple read:
    Code:
    file>> rows >> cols;
    
    int **matrix = new int*[rows];
    for (int i = 0; i < rows; i++) {
      matrix[i] = new int[cols];
      for (int j = 0; j < cols; j++)
        file>> matrix[i][j];
    }

IMN logo majestic logo threadwatch logo seochat tools logo