-2

I am stuck into read 2D array from text file, but I couldn't make it. I have tri[][] array data which you see in code. I need read it from text instead of defining in program. And I insert that data into the program.

enter image description here

Code:

// C++ program for Dynamic
// Programming implementation of
// Max sum problem in a triangle 
#include<bits/stdc++.h>
using namespace std;
#define N 4
int sumRec[N];
bool checkPrimeNumber(int n)
{
    //cout << n;
    bool flag = true;
    for(int a = 2; a <= n/2; ++a)
    {
      if(n%a == 0)
      {
          flag = false;
      }
    }
    //if number is 1 return false. 1 is not prime
    if(n == 1)
        flag = false;
    return flag;
}


int maxPathSum(int tri[][N], int row, int col, int sum)
{
    bool isPrime = checkPrimeNumber(tri[row][col]);
    if(isPrime == true){
        //Do nothing..
    }
    else if(row + 1 >= N){
        sum = sum + tri[row][col];
     if(sumRec[col]<sum)
         sumRec[col]= sum;  
    }
    else{
        sum = sum + tri[row][col];
        maxPathSum(tri, row + 1, col, sum);
        maxPathSum(tri, row + 1, col + 1, sum);
    }
}

int main()
{
   int maxSum = 0;
   int tri[N][N] = {  {1, 0, 0, 0},
                      {8, 4, 0, 0},
                      {2, 6, 9, 0},
                      {8, 5, 9, 3}};

   maxPathSum(tri, 0, 0, 0);

    for (int i=0; i < N ; i++)
    {
        if(sumRec[i] >= maxSum)
            maxSum = sumRec[i];
    }
    cout << maxSum;
   return 0;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 2
    Recommendations: Show your work. If you have 5 days of research and failed attempts include the most promising of it. Avoid images in posts. Unless the question is about the image (pixels in wrong spot, wrong colour) or the image describes something difficult to represent in text (complicated diagram, for example) use text. [Use caution with `using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Do not include anything from `bits`. They are implementation headers and not intended for direct inclusion. – user4581301 Mar 27 '18 at 14:21
  • Wow, searching for 5 days. I searched for "c++ read file matrix" and got a bunch of responses. You can get a lot of responses from a search of "c++ read file array 2d". – Thomas Matthews Mar 27 '18 at 15:29

1 Answers1

1

Assuming you know N beforehand, it is not too complicated:

int tri[N][N]
ifstream ifs("yourfile.dat");
for (size_t i=0; i < N; i ++)
  for (size_t j=0; j < N; j++) 
    ifs >> tri[i][j];

This (simple) approach works for files of the following shape, newlines and spaces separate tokens. A file with N=5 would look like:

1 2 3 4 5
2 3 4 5 6 
3 1 4 2 5
6 2 4 2 1
6 2 3 4 5

Regarding your followup-comment: Reading a triangular matrix is possible in very much the same way. As said, the software reads any sequence of integers seperated by spaces, tabs, newlines or similar and does not care about the format beyond that. In fact, you can have a triangular file or whatever. For a triangular matrix, however, the 2D array might not be the optimal storage...

To be concrete: For a triangular matrix in the file, change the for according to the numbers you expect, e.g.

for(i=0; i<N; i++) 
  for j=0; j <=i; j++) 
     cin >> tri[i][j];

will roughly do it: The outer for goes over the expected lines, the inner goes for 1 for the first, 2 for the second and so on. However, you can also add a

tri[j][i] = tri[i][j]; 

to the loop in order to fill both sides of the square storage space with a symmtric matrix made out of your diagonal matrix.

It is still advisable to clearly define your file format and your memory storage concept in order to write a parser that can acutally fail for files not having the right format and shape.

user3640029
  • 165
  • 4
  • @user4581301 ı really thank for your response. The problem is first line have one element, second line is two element, third one has tree element as you see below. I need to read this from text file. If you write zeros to gaps to equilize row and column, your answer reads correctly. But it is not allow to write zeros. – salih dağdur Mar 30 '18 at 14:13
  • Just updated the answer to concretely cover a triangular matrix. – user3640029 Mar 31 '18 at 13:16