I am working on writing a class to define an NxM matrix. A matrix should be printed in the form:
[0 1 2]
[3 4 5]
[6 7 8]

private:
		//matrix class member variables
		unsigned int rows;
		unsigned int cols;
		float * data;
// constructor
matrix::matrix(int r, int c)
{
	rows=r;
	cols=c;
	data=new float[r*c];
	defaultFill(); //function to set the contents of data to all 0s
}
//print function to output matrix in correct format
void matrix::print()
{
	
	for (int i=0; i<(rows); i++)
	{
		cout<<"[ ";
		for (int j=0; j<cols; j++)
		{
			cout<<data[(i*rows)+j]<<" ";
		}
		cout<<"]"<<endl;
					
	}
	cout<<"]";

}

The problem that I am having is with the print function. When the matrix is square, it works as desired. However, when the number of rows is greater than the number of columns (like a 4x3 matrix), the shell crashes after outputting "["
When the number of columns is greater than the number of rows (like a 3x4 matrix), the output is:
[1 2 3 4]
[4 5 6 7]
[7 8 9 0]

when it SHOULD be:
[1 2 3 4]
[5 6 7 8]
[9 0 0 0]

I'm having difficulty finding the error(s) in my code and would really appreciate it if someone could help me figure out exactly what I am doing wrong so that I can fix it.

Recommended Answers

All 7 Replies

Line 23. cout<<data[(i*rows)+j]<<" ";
Why do You use variable rows here? Think about it:)

line #23:
it should be

cout<<data[(i*cols)+j]<<" ";/*not i*rows*/

Thanks for the tip, that makes a lot more sense. Changing line 23 resulted in correct output for the 3x4 matrix, but the shell still crashes when I try to output the 4x3 matrix.
Do you see something in this code that would cause that to happen?

use debugging tool(e.g. gdb) for debugging your code. U wil know where exactly the error occured.
U can post your complete code here.
But I prefer U to find the problem yourself. Just debug it.

Thanks for the help. Using the debugger and trial and error, I was able to fix the print function. However, I don't understand conceptually why it works.
This is my new code:

void matrix::print()
{
	if (rows>=cols)
	{
		for (int i=0; i<(rows); i++)
		{
			cout<<"[ ";
			for (int j=0; j<cols; j++)
			{
				cout<<data[(i*cols)+j]<<" ";
			}
			cout<<"]"<<endl;
					
		}
	}
	else
	{
		int i=0;
		while (i<rows)
		{
			for (int j=0; j<cols; j++)
		{
			cout<<"[";
		for (int k=0; k<rows; k++)
				cout<<data[(k*cols)+j]<<" ";
			cout<<"]"<<endl;
			i++;
		}
		}
	}
	

}

The if statement is basically just my previous code that worked as long as the number of columns wasn't greater than the number of rows. The else statement is a modified algorithm that outputs a matrix with more columns than rows. I just don't understand why the while statement makes a difference and allows the algorithm to produce the desired results.

Try this. Works good for me (Code::Blocks 8.02 with default compilator]

#include <iostream>

using namespace std;
class matrix
{
private:
		//matrix class member variables
		unsigned int rows;
		unsigned int cols;
		float * data;
		void defaultFill()
        {
            for(int i=0;i<rows*cols;++i)
                data[i]=i;
	    }
public:
    // constructor
    matrix::matrix(int r, int c)
    {
        rows=r;
        cols=c;
        data=new float[r*c];
        defaultFill();
    }
    //print function to output matrix in correct format
    void matrix::print()
    {
        for (int i=0; i<(rows); i++)
        {
            cout<<"[ ";
            for (int j=0; j<cols; j++)
            {
                cout<<data[(i*cols)+j]<<" ";
            }
            cout<<"]"<<endl;
        }

    }
};

int main()
{
    matrix m(4,3);
    m.print();
    cout<<"\n";
    matrix m2(3,4);
    m2.print();
    cout << "Hello world!" << endl;
    return 0;
}

i have this code i want to print out the output as matrix !

0 1 2 3

1 2 3 4

2 3 4 5

3 4 5 6

4 5 6 7

and then assign the third row to 0 using a for loop !

this is the code :-

# include <iostream.h>
int main ()
{
int BL [5][4];
int i,j;
for (i=0;i<5;i++)
      for (j=0;j<4;j++)
     {
      BL [i][j]=i+j;
      cout<< BL [i][j];
      }
return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.