Click here to Skip to main content
15,941,444 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a matrix saved in a .txt file, it has values, I created a function that takes row and number as parameters, then it returns the value obtained from the matrix.txt file.
I have a 2D array containing pairs of rows/columns, they are not ordered also they are random pairs, How could I iterate overe this 2D array and pass each pair to the function getList(rw,cl) to obtain such a file as result.

result.txt
row,column,its value
row,columm,its value
etc.

Thanks for any help, notice please I am still beginner, so no advances tips

What I have tried:

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

const int col_nbr = 7;
const int row_nbr = 3;

int static getValueOf[col_nbr][row_nbr];      
    
void loadMatrix(){

int x, y;


ifstream in("/storage/emulated/0/matrix.txt");

//This my matrix .txt file saved in pc    
/*        
11 12 13 14 15 16 17
21 22 23 24 25 26 27
31 32 33 34 35 36 37
*/      
        
        
        

if (!in){
    cout << "Cannot open file.\n";
    return;
}

  for (x = 0; x < row_nbr; x++){       
     for (y = 0; y < col_nbr; y++){
            
       in >> getValueOf[x][y];
       }
        }

in.close();
}

int getList(int rw,int cl){

return getValueOf[rw-1][cl-1];
}


int main(){

loadMatrix();

//I would take each pair row/columm from this 2D array coords, then I pass the row and the columm to the function getList(rw,cl) to get a list of the coorespondent values from the matrix.txt

int coords [4][2] = {{3,0},{2,1},{6,2},{1,1}};
//int coords has 4 rows and 2 columms
  

    cout<<getList(1,1)<<endl;  //11
    cout<<getList(3,7)<<endl;  //37
    
    
   

return 0;
}
Posted
Updated 3-Jul-18 22:48pm
Comments
Mohibur Rashid 3-Jul-18 20:54pm    
I am kinda skeptical right now. You can create a nested loop for reading from file but cannot create a single loop?
Patrice T 3-Jul-18 22:03pm    
not clear!
give details of problem.
khaledce 4-Jul-18 6:47am    
@ppoymorphe, ok, then let's forget everything above for a moment and say, we have a function getList(row,columm), it returns a singe int value; and a 2D array in the .cpp file, it has 4 couples of pair row/column.
How to iterate getList(row,column) via two nested for-loops over the 2D array, in order to get the correspondent values for these couples, to be stored in a new 1D array or in external file, doesn't matter

1 solution

What about abandoning global variables and using modern C++ features and library?
(pass your data file as stdin, using input redirection).
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <array>
using namespace std;

using Matrix = vector < vector <int > >;

int main()
{
  Matrix m;
  string line;
  while ( getline( cin, line) )
  {
    vector <int> row;
    istringstream iss(line);
    while ( iss )
    {
      int i;
      iss >> i;
      row.push_back(i);
    }
    m.push_back( row );
  }

  array< array <int, 2>, 4> arr{{{3,0},{2,1},{6,2},{1,1}}};

  for ( const auto & a : arr)
  {
    int r = a[1]; // it looks there is some confusion on 'rows' and 'columns' interpretation...
    int c = a[0];
    cout << "M[" << r << "," << c << "]= " << m[r][c] << "\n";
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900