CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 23 of 23

Thread: Matrix input

  1. #16
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Matrix input

    What about

    Code:
    std::transform(r1->begin(), r2->begin(), r1->end(), back_inserter(r), [](int i, int j)
    transform requires an iterator - not a const_iterator. That's probably what the compiler is complaining about.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  2. #17
    Join Date
    May 2015
    Posts
    500

    Re: Matrix input

    Thanks kaud,

    tried this, still the same error is throwing up !

  3. #18
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Matrix input

    Sorry. I looked at the code and didn't see the same problem you didn't see. You're got the order of the parameters wrong. Should be:

    Code:
    std::transform(r1->begin(), r1->end(), r2->begin(), back_inserter(r), [](int i, int j)

    This compiles and runs without error in debug node with VS2019:

    Code:
    #include <vector>
    #include <sstream>
    #include <algorithm>
    #include <iostream>
    #include <iterator>
    
    
    using namespace std;
    using Matrix = vector<vector<int>>;
    
    Matrix setmatrix(const int RR, const int CC)
    {
    	Matrix mat;
    	string in;
    	std::cout << "Values: ";
    
    	getline(cin, in);
    
    	std::istringstream iss(in);
    
    	for (int r = 0; r < RR; r++)
    	{
    		vector<int> vec;
    		copy_n(istream_iterator<int>(iss), CC, back_inserter(vec));
    
    		mat.push_back(vec);
    	}
    
    	return mat;
    }
    
    
    Matrix addMatrix(const Matrix& mat1, const Matrix& mat2)
    {
    	Matrix mat;
    
    	{
    		for (auto r1 = cbegin(mat1), r2 = cbegin(mat2); r1 != cend(mat1); r1++, r2++)
    		{
    			vector<int> r;
    
    			std::transform(r1->cbegin(), r1->cend(), r2->cbegin(), back_inserter(r), [](int i, int j)
    			{
    				return(i + j);
    
    			});
    
    
    			mat.push_back(r);
    
    		}
    	}
    
    	return mat;
    }
    
    void dispaymatrix(const Matrix& mat)
    {
    	for (auto r : mat)
    	{
    		for (auto c : r)
    		{
    			cout << c;
    		}
    
    		cout << "\n";
    	}
    }
    
    int main()
    {
    	int CC(0), RR(0), M(0);
    
    	cout << "Number of matrices: ";
    	cin >> M;
    
    	for (int m = 0; m < M; m++)
    	{
    		std::cout << "Matrix " << m + 1 << '\n';
    
    		std::cout << "Rows Columns: ";
    		cin >> RR >> CC;
    
    		cin.ignore(1000, '\n');
    
    		const Matrix mat1 = setmatrix(RR, CC);
    
    		const Matrix mat2 = setmatrix(RR, CC);
    
    		Matrix mat3 = addMatrix(mat1, mat2);
    
    		dispaymatrix(mat3);
    	}
    
    	return 0;
    }
    [I added some prompts so I knew what was being asked!]

    Code:
    Number of matrices: 2
    Matrix 1
    Rows Columns: 2 2
    Values: 1 2 3 4
    Values: 2 3 4 5
    
    c:\MyProgs>test12
    Number of matrices: 2
    Matrix 1
    Rows Columns: 2 2
    Values: 1 2 3 4
    Values: 2 3 4 5
    35
    79
    Matrix 2
    Rows Columns: 2 2
    Values: 1 2 3 4
    Values: 5 6 7 8
    68
    1012
    Last edited by 2kaud; November 6th, 2020 at 08:45 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #19
    Join Date
    May 2015
    Posts
    500

    Re: Matrix input

    Very Sorry it was my mistake (was sleepy and parameters wrongly placed for transform i.e end(*r1), needs to be before begin(*r2) ): The Now following code works.
    Code:
    Matrix addMatrix( const Matrix & mat1,  const Matrix & mat2)
    {
    	Matrix mat;
    
    	{
    		for (auto r1 = cbegin(mat1), r2 = cbegin(mat2); r1 != cend(mat1); r1++, r2++)
    		{
    			vector<int> r;
    
    			std::transform(begin(*r1), end(*r1), begin(*r2), back_inserter(r), []( int i,  int j)
    			{
    				return(i+j);
    
    			});
    
    			mat.push_back(r);
    
    		}
    	}
    
    	return mat;
    }

  5. #20
    Join Date
    May 2015
    Posts
    500

    Re: Matrix input

    Thanks a lot kaud again for your help and patience.
    I also learnt new things from this error, like cbegin

    thankyou

    Sorry, I also corrected the following line in my code std::istringstream iss(in); before geline() ! which was wrong ..

    Code:
    Matrix setmatrix(const int RR, const int CC)
    {
    	Matrix mat;
    	string in;
    
    	getline(cin, in);
    
    	std::istringstream iss(in);
    	vector<int> vec;
    
    	for (int r=0; r< RR; r++)
    	{
    		vector<int> vec;
    		copy_n(istream_iterator<int>(iss), CC, back_inserter(vec));
    
    		mat.push_back(vec);
    	}
    
    	return mat;
    }
    Last edited by pdk5; November 6th, 2020 at 08:44 AM.

  6. #21
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Matrix input

    So did I in the code in my post #18!

    As well as begin/end, cbegin/cend, there are also rbegin (reverse iterator - starts at the end and iterators to the start) & rend. Use them just like begin/end and also use iterator increment (not decrement as might think). Then there is crbegin/crend - which is the const iterator version of rbegin/rend.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #22
    Join Date
    May 2015
    Posts
    500

    Re: Matrix input

    Thanks a lot kaud, for the advice on iterators,

  8. #23
    Join Date
    May 2015
    Posts
    500

    Re: Matrix input

    Btw, this was a exercise program, and i didnot open fully. It looks like i need to use operator overloading and so i modified the above program and now it works ok

    The main was given as part of the exercise, so i just wrote the other part:

    Code:
    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    #include <vector>
    #include <sstream>
    #include <algorithm>
    #include <iostream>
    #include <iterator>
    
    using namespace std;
    
    class Matrix
    {
    public:
        vector<vector<int>> a;
    
        Matrix operator+(Matrix & other)
        {
            Matrix mat;
    
            for (auto r1 = begin(a), r2 = begin(other.a); r1 != end(a); r1++, r2++)
            {
                vector<int> r;
    
                std::transform(begin(*r1), end(*r1), begin(*r2), back_inserter(r), [](int i, int j)
                {
                    return(i + j);
                });
                mat.a.push_back(r);
    
            }
            return mat;
        }
    };
    
    int main () {
       int cases,k;
       cin >> cases;
       for(k=0;k<cases;k++) {
          Matrix x;
          Matrix y;
          Matrix result;
          int n,m,i,j;
          cin >> n >> m;
          for(i=0;i<n;i++) {
             vector<int> b;
             int num;
             for(j=0;j<m;j++) {
                cin >> num;
                b.push_back(num);
             }
             x.a.push_back(b);
          }
          for(i=0;i<n;i++) {
             vector<int> b;
             int num;
             for(j=0;j<m;j++) {
                cin >> num;
                b.push_back(num);
             }
             y.a.push_back(b);
          }
          result = x+y;
          for(i=0;i<n;i++) {
             for(j=0;j<m;j++) {
                cout << result.a[i][j] << " ";
             }
             cout << endl;
          }
       }  
       return 0;
    }

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured