Thread: matrix using std::vector

  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
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 05:57 PM
  3. Gauss-Jordan Matrix Inversion in C++
    By Max_Power82 in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2006, 08: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