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

Home Forums CPP scientific computing discussion reading data from file

• # reading data from file

Posted by on August 26, 2023 at 10:45 am

Hi, my question is about reading data from a data file and assign it to variable.

When we reading datas from an array at a given data file, what is the sequence of reading datas? Firstly lets have A[3][3] matrice and reading [0][0] element. What is the next element and how it specified?

Thanks.

Best Regards.

2 Members · 2 Replies
• 2 Replies
• ### 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.

Member
August 26, 2023 at 4:31 pm

I got it, thanks.

Best regards.