I did this but the issue is that "new_num_cols" from 19-23 doesn't seem to register under the print function (it's creating a red error line)'
I don't necessarily need fout; I don't even know if using it is correct.
Also, I already have random values in the matrix; it's adding them that's the issue.
I'm trying to understand what you're saying but it's kind of confusing. It would be much easier if you could have coded this...
Any chance you could rewrite/fix all the code for me (so that it accomplishes the task)? I'm usually very intrigued to learn more but I have almost no time remaining and this assignment's score literally makes or breaks my final grade. I'd really appreciate it...
show your updated code (don't edit your first post)
> I tried using your edited code, but the program doesn't output anything
well, you haven't defined the `print()' function, so of course nothing would be printed.
> I thought I set random values for the matrices
no, you didn't
you filled another unrelated vector.
> I get the idea of the issue but where should I call srand then?
then you don't understand the issue.
you have to call srand() only once, so put it someplace where you are sure that it'll only execute once
(of course, before you construct your matrices)
> The code on 21-26 adds spaces to the matrices
add spaces...
no, the matrices are not modified.
> The print function is meant to output a Matrix object
so you don't know how to output a matrix
¿do you know how to output a vector?
¿do you know how to output one variable?
> how am I meant to add the matrices when they have random numbers?
same way you add matrices that have non-random numbers: element by element.
> The sum isn't working.
¿how do you know?
¿how do you see the operands matrices and the result matrix?
> My code simply generates a third matrix with random numbers instead of adding them (I think)
¿how did you verify it?
> it's creating a red error line
try to compile your program
post the error messages
> It would be much easier if you could have coded this
the funny part, we already did it
> this assignment's score literally makes or breaks my final grade
fail the class
try again next year
I removed the srand(time(0)) from my code since main.cpp has it already. The values are now randomly assigned.
Is it just cout<<values[i][j] to output a matrix object then? Or what?
I understand how to add, but what code should I use because the one I have doesn't add the matrices or in your words, the "unrelated vector"?
When I run my initial program, it shows 3 matrices, each with different values ranging from 0-9. The third should be a sum but it isn't.
That's the thing, compiling isn't working. A red underscore thing pops up - sometimes requires a class while other times saying that the operand doesn't match.
I'll repeat, show your updated code
(all the code, in a new post)
> Is it just cout<<values[i][j] to output a matrix object then? Or what?
that'll print the element to the screen, yes
¿is `values' the same as `this->values'? (¿did you get rid of the local variable?)
> That's the thing, compiling isn't working
if your program doesn't compile, then you can't run it
if you can't run it, then you can't see the matrices
perhaps you are executing an old version, so the output is meaningless
> sometimes requires a class while other times saying that the operand doesn't match.
copy the error message verbatim
> That last response is just mean :(
completely intentional
void Matrix::print(ofstream& fout) const {
for (int i = 0; i < this->num_rows; i++) {
for (int j = 0; j < this->num_cols; j++) {
fout << this->values[i][j] << ' ';
}
fout << '\n';
}
}
this-> may be omitted
check out the out.txt file
also, may bite you later so
1 2 3 4 5 6 7
Matrix Matrix::operator+(const Matrix& M2) const {
if (num_rows != M2.num_rows || num_cols != M2.num_cols) {
cout << "Dimensions don't match!" << endl;
return *this; //you said that you'll return a Matrix, so you should return a matrix
//(otherwise you'll have undefined behaviour, luckily a crash)
//throw(42); //as an alternative, you may throw an exception
}
Also, I just made the changes and ran it; but still, it doesn't output anything onto the console? Just a blank screen and then "press any button to exit"...
you don't get output to console, but to a file, as per your requeriment:
«void print(ofstream& fout) const outputs a Matrix object to a file as requested by the user.»
to show on the console you'll have to change the prototype of that method
ofstream to ostream in both declaration and implementation void print(ostream& fout) const
then you may do A.print(cout); in your main()
but that was not what you were asked
¿did you check the file?