Hello :)

(Too many questions about this)!

Basically, I'm trying to display the contents of a matrix without using "cout" in my class. (It's a bad method)

I've tried to return an array, but, that failed miserably.. So I've got a method in my class:

double Matrix::read(int theRow, int theColumn)
{
    return matrix[theRow*rows*theColumn];
}

(This basically returns the elements of matrix according to theRow, theColumn)

In order to display the complete matrix, I have to use this algorithm:

matrix[i*rows+j]

i = for loop
j = for loop
e.g.

void Matrix::displayMatrix()
{
    for (int i=0; i<columns; i++){
        for (int j=0; j<rows; j++){
           cout<< matrix[i*rows+j] << "\t";
        }
        cout<<"\n";
    }
    
}

Is there a way I can replicate this inside main.. Something like this:

for(int i=0; (i < columns); i++)
    {
        for(int j=0; (j < rows); j++)
        {
            cout << m.read(ALGORITHM) << "\t";
            
        }
        cout << endl;
        
    }

If that makes sense? Hope someone can help, it's been annoying me!

>>I'm trying to display the contents of a matrix without using "cout" in my class

Huh?? How do you plan to display something without using either cout or printf()? one way to accomplish that in MS-Windows is to call WriteFile() instead of cout.

>>I'm trying to display the contents of a matrix without using "cout" in my class

Huh?? How do you plan to display something without using either cout or printf()? one way to accomplish that in MS-Windows is to call WriteFile() instead of cout.

Sorry about the confusion..

I'm going to display it in the main, obviously. Just not in the class. I'll just return the array inside the class then I can build multiple mains (forms, console etc..)

You can return it as a string and then print the string outside of the method/class...

I know what you're suggesting, I just cannot see why it would be efficient to read the data as a double, and then convert it to a string? Could I still do my calculations on the /double/ ?

You can return it as a pointer to an array of double. It all depends on your intentions. Strings in this case would be for cosmetic purposes. If you're still doing calculations, do the most efficient thing possible. --that might include doing some calculations inside the class.

Thanks :)

If I returned it as a pointer to a double though:

double matrix = image1.read(); // store the values of the pointers
    
cout << matrix[1]; // print the values

And the method:

double Matrix::read()
{
    return *matrix;
}

Throws an error: error: invalid types ‘double[int]’ for array subscript

Does double matrix have to be an array as well to allow for "matrix[0]" etc?

Edited 4 Years Ago by phorce: n/a

Are you trying to return A double or ALL of the doubles?
Are you trying to return a pointer to the matrix?
Your return-type says you only want to return A double.
...but your return STATEMENT says something else.

Edited 4 Years Ago by thines01: n/a

All of the doubles. I'm trying to print this:

0, 1, 1, 1, 1, 1, 1, 1, 1, 0,
0, 1, 1, 0, 0, 1, 0, 0, 1, 1,
1, 0, 0, 0, 1, 1, 1, 0, 0, 0,
1, 1, 1, 0, 1, 0, 0, 0, 1, 1,
0, 0, 1, 1, 1, 1, 0, 0, 1, 1,
1, 0, 1, 0, 0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 1, 1, 1, 1

But without doing it in the class, just in main, so I need to return it as an array, and then two for loops to print it out :)

something like this, note the two stars, one star for each dimension in the matrix. Assumes matrix was defined something like double matrix[m][n]; If it is only declared as double matrix[m*n]; then use only one star below instead of two.

double** Matrix::read()
{
    return matrix;
}

then somewhere in main()
double** matrix = image1.read();

Edited 4 Years Ago by Ancient Dragon: n/a

Hello :)

(Too many questions about this)!

Basically, I'm trying to display the contents of a matrix without using "cout" in my class. (It's a bad method)

I've tried to return an array, but, that failed miserably.. So I've got a method in my class:

double Matrix::read(int theRow, int theColumn)
{
    return matrix[theRow*rows*theColumn];
}

(This basically returns the elements of matrix according to theRow, theColumn)

In order to display the complete matrix, I have to use this algorithm:

matrix[i*rows+j]

i = for loop
j = for loop
e.g.

void Matrix::displayMatrix()
{
    for (int i=0; i<columns; i++){
        for (int j=0; j<rows; j++){
           cout<< matrix[i*rows+j] << "\t";
        }
        cout<<"\n";
    }
    
}

Is there a way I can replicate this inside main.. Something like this:

for(int i=0; (i < columns); i++)
    {
        for(int j=0; (j < rows); j++)
        {
            cout << m.read(ALGORITHM) << "\t";
            
        }
        cout << endl;
        
    }

If that makes sense? Hope someone can help, it's been annoying me!

Hi

Do you really know what you want to do ?

If you want to access the elements of the matrix why not overloading the

operator ()

if you are not using a real matrix in your class but just a one dimensional array

overload this like

double& operator()(int i, int j){
   return matrix[i*rows+j]; // in my point of view it should be matrix[i*cols+j]
}

and then in your main

for (int i=0; i<m.getColumns(); i++){
        for (int j=0; j<m.getRows(); j++){
           cout<< m(i,j)<< "\t";
        }
        cout<<"\n";
    }

hope it helps

Edited 4 Years Ago by therockon7throw: n/a

I think I'd go for overloading operator<< for your matrix class. That way you can just do things like:

Matrix m;

...

/* Write the matrix to the terminal */
std::cout << m << std::endl;

/* Store the matrix in a file */
std::ofstream outFile( "my_file.txt" );
outFile << m << std::endl;

/* Make a string with the matrix in it */
std::stringstream ss;
ss << m << std::endl;
std::string matrixAsString = ss.str();

You can output to the terminal, a file or get the matrix into a std::string using the same function. This method also doesn't require you to expose the internals of your Matrix class.

Edited 4 Years Ago by ravenous: Added comments to code

This article has been dead for over six months. Start a new discussion instead.