matrix using std::vector

This is a discussion on matrix using std::vector within the C++ Programming forums, part of the General Programming Boards category; Hi everyone.. My desired output for this code is: 0 1 0 1 0 1 0 1 0 1 0 ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91

    Question matrix using std::vector

    Hi everyone..
    My desired output for this code is:

    0 1 0 1 0
    1 0 1 0 1
    0 1 0 1 0
    1 0 1 0 1
    0 1 0 1 0
    where n will be the value for the n x n matrix, which in this case is n=5.

    I have achieved to layout the matrix pattern but all the elements displayed are
    0. How can I get it to display an alternate pattern of 0 and 1?

    Thanks..

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main()
    {
                    int n = 0;
    	int i = 0, j = 1;
    
    	vector<int> matrix;
    	
    	cout << "Enter a value for n where n >= 2: ";
    	cin >> n;
    	cout << endl;
    
    	matrix.resize(n);
    	
    	if (n % 2 != 0)
    	{
    		for (i = 0; i < n; ++i)
    		{
    			for (j = 0; j < (n/2); ++j)
    				cout << matrix[i] << " " << matrix[j] << " ";
    			cout << matrix[i] << endl;
    		}
    		cout << endl;
    	}
    	else
    		for (i = 0; i < n; ++i)
    		{
    			for (j = 0; j < (n/2); ++j)
    				cout << matrix[i] << " " << matrix[j] << " ";
    			cout << endl;
    		}
    		cout << endl;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,954
    If that is all you want to print, wouldn't it be easier to use nested for loops and not bother with a container in the first place?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91
    Hi laserlight..

    Thanks for the quick response. This is what I came up with. It works the way I wanted it to but do I really need to use a matrix for this? I guess, what I was trying to say is what is the most efficient way to write this program? I will greatly apreciate your opinion.

    Thanks again..

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main()
    {
    	int n = 0;
    	int bit0 = 0, bit1 = 1;
    	int i = 0, j = 0;
    
    	vector<int> matrix;
    	
    	cout << "Enter a value for n where n >= 2: ";
    	cin >> n;
    	cout << endl;
    
    	matrix.resize(n);
    	
    	if (n % 2 != 0)
    	{
    		for (i = 0; i < (n/2); ++i)
    		{
    			for (j = 0; j < (n/2); ++j)
    				cout << bit0 << " " << bit1 << " ";
    			cout << bit0 << endl;
    			for (j = 0; j < (n/2); ++j)
    				cout << bit1 << " " << bit0 << " ";
    			cout << bit1 << endl;
    		}
    		for (i = 0; i < (n/2); ++i)
    		{
    			cout << bit0 << " " << bit1 << " ";
    		}
    		cout << bit0 << endl;
    	}
    	else
    		for (i = 0; i < (n/2); ++i)
    		{
    			for (j = 0; j < (n/2); ++j)
    				cout << bit0 << " " << bit1 << " ";
    			cout << endl;
    			for (j = 0; j < (n/2); ++j)
    				cout << bit1 << " " << bit0 << " ";
    			cout << endl;
    		}
    		cout << endl;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,954
    This is what I came up with. It works the way I wanted it to but do I really need to use a matrix for this?
    Good to see you solved the problem yourself
    As I stated, you do not need to use a container.

    I guess, what I was trying to say is what is the most efficient way to write this program?
    Nested loops with some simple arithmetic will do:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int n = 0;
        cout << "Enter a value for n where n >= 2: ";
        cin >> n;
    
        cout << '\n';
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                cout << ((i + j) &#37; 2) << ' ';
            }
            cout << '\n';
        }
    }
    Incidentally, with the above solution n < 2 will still be okay, even if n is negative.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 04:57 PM
  3. Gauss-Jordan Matrix Inversion in C++
    By Max_Power82 in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2006, 07:31 PM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21