Find answers, ask questions, and connect with our <br>community around the world.

Home Forums CPP scientific computing discussion reading data from file

  • Nishant

    Member
    August 26, 2023 at 2:57 pm

    Hi Baybarskahn,

    Data is always read sequentially from a data file. It depends how you are going to store that data in the array (for example). There are two possibilities for a 2-dimensional array,

    1. Row-major ordering – In this case the values being read are stored in a row-by-row fashion. For example,

    A[0,0], A[0,1], A[0,2]

    A[1,0],  A[1,1,], A[1,2]

    A[2,0], A[2,1], A[2,2]

    In order to store the values as above you should read the data using the following for loops,

    for(int i=0; i<3; i++){

    for(int j=0; j<3; j++) {

    std::cin >> data;

    A[i][j] = data;

    }

    }

     

    2. Column major ordering – If you reverse the order of for loops (or alternately swapped the array indices from A[i][j] to A[j][i]) then the data will be stored in a column major format i.e.

    A[0][0], A[1,0], A[2,0], A[0,1], A[1,1], A[2,1],  A[0,2], A[1,2], A[2,2].

     

    Hope that helps. Please let me know if you have any further questions.

  • AbdulKadir

    Member
    August 26, 2023 at 4:31 pm

    I got it, thanks.

    Best regards.

Log in to reply.

error: Content is protected !!