Matrix Class - Wrong Outputs?! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Matrix Class - Wrong Outputs?!

https://code.sololearn.com/cq24J951TxgI/# I have the following Matrix Class. Now I tried to create a function to Split the Matrix into Sub-Matrices of size m_r * m_c each and save them in a Matrix of Matrix - Matrix** for easy access... friend Matrix** Split(Matrix m,int m_r,int m_c) { if((m.r%m_r==0)&&(m.c%m_c==0)&&(m_r<m.r)&&(m_c<m.c)) { int new_r=m.r/m_r; int new_c=m.c/m_c; Matrix** matrix=new Matrix*[new_r]; for(int i=0;i<new_r;i++) { matrix[i]=new Matrix[new_c]; for(int j=0;j<new_c;j++) matrix[i][j].Reset(m_r,m_c); } for(int i=0;i<m.r-m_r;i++) { for(int j=0;j<m.c-m_c;j++) { for(int k=0;k<m_r;k++) { for(int l=0;l<m_c;l++) { matrix[i][j].mat[k][l]=m.mat[i+k][j+l]; } } } } return matrix; } else { //Do Something else. } } Now, the problem is that the outputs are wrong. Eg - When Input - 1 1 0 0 1 1 0 0 0 0 1 1 0 0 1 1 Output - 1 1 1 1 // Correct 1 0 0 0 1 0 //Wrong 0 0 1 1 0 0 0 0 //Wrong 0 0 1 0 1 1 0 1 //Wrong 1 1 Why is the output wrong? P.S. - I tried Increasing the limits of i and j's loop by 1, but that stops the program.

17th May 2017, 10:13 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
59 Answers
+ 4
SUCCESS!!!! int row = 0; int column = 0; for (int i = 0; i<m.r; i += m_r){ for (int j = 0; j < m.c; j += new_c) { for (int x = i; x < i + new_r; x++){ row++; for (int y = j; y < j + new_c; y++){ column++; matrix[i/2][j/2].mat[row - 1][column - 1] = m.mat[x][y]; } column = 0; } row = 0; } } output: First set 2 2 1 1 Second set 1 1 1 1 Third set 1 1 1 1 Forth set 1 1 2 2
17th May 2017, 4:32 PM
jay
jay - avatar
+ 5
#code of the day code crashes my phone 2000+ lines of code
17th May 2017, 6:40 PM
MR Programmer
MR Programmer - avatar
+ 5
woooooo! I am so happy that I was able to play a small part in your success! now what is the plans for this code? is it part of some larger project? anymore mods planned?
18th May 2017, 8:25 AM
jay
jay - avatar
+ 5
Nice! Well keep us in the loop as to your progress. It is a very neat header file so far
18th May 2017, 8:32 AM
jay
jay - avatar
+ 4
So correct output should be? 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 Edit: I will have a look on my pc after dinner. the code crashes the app on my phone. Not sure if I can help. But I will look
17th May 2017, 9:16 AM
jay
jay - avatar
+ 4
@jay Yes, That's what the output should be. Thank You for your initiative to help...
17th May 2017, 10:12 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 4
@Kinshuk: Could you update the code to include the code you have. I have put in the above friend function, but dont know how to use it to get output lol. Rather than spend forever trying to get to the same point you are at. It would help me heaps. Then I can concentrate on the problem at hand. :) told you I might not be much help :)
17th May 2017, 10:39 AM
jay
jay - avatar
+ 4
https://code.sololearn.com/cccB9W06ocb6/?ref=app Here is the code after the update...
17th May 2017, 10:53 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 4
Oh Srry Lemme update that...
17th May 2017, 11:01 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 4
Mine loops twice for some reason now... so confuse
17th May 2017, 2:55 PM
jay
jay - avatar
+ 4
I removed the i loop and it behaves for (int j = 0; j < m.c; j += new_c) { for (int x = j; x < j + new_r; x++) { for (int y = j; y < j + new_c; y++) { cout << x << " " << y << endl; } } } Output: 0 0 0 1 1 0 1 1 2 2 2 3 3 2 3 3
17th May 2017, 3:02 PM
jay
jay - avatar
+ 4
imma dumb ass lol -- the i is rows, so it the x.... hahahha its :1:20am here.. sooo for (int i = 0; i<m.r; i += m_r){ for (int j = 0; j < m.c; j += new_c) { for (int x = i; x < i + new_r; x++) { for (int y = j; y < j + new_c; y++) { cout << x << " " << y << " = " << m.mat[x][y] << endl; } } } } OUtput 0 0 = 2 0 1 = 2 1 0 = 1 1 1 = 1 0 2 = 1 0 3 = 1 1 2 = 1 1 3 = 1 2 0 = 1 2 1 = 1 3 0 = 1 3 1 = 1 2 2 = 1 2 3 = 1 3 2 = 2 3 3 = 2
17th May 2017, 3:20 PM
jay
jay - avatar
+ 4
So how does the new matrix work (the one we are passing the data too?
17th May 2017, 3:24 PM
jay
jay - avatar
+ 4
@jay Congo! You did it! Thanks!
17th May 2017, 4:33 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 4
i wouldnt have slept! I had to have one last go before I went to bed! You are truly welcome! I loved the challenge! Keep up the awesome questions!
17th May 2017, 4:36 PM
jay
jay - avatar
+ 4
nope yours code
18th May 2017, 4:33 AM
MR Programmer
MR Programmer - avatar
+ 4
It is pretty awesome. Having played with it myself I can testify to its greatness
18th May 2017, 4:36 AM
jay
jay - avatar
+ 4
@Kinshuk. I had a thought this morning. Could we replace the 2 in the assignment formula with new_r and new_c respectively?
18th May 2017, 4:39 AM
jay
jay - avatar
+ 4
so it would be: matrix[i/new_r][j/new_c] Edit: Not tested. just a thought
18th May 2017, 4:41 AM
jay
jay - avatar
+ 3
Just finished dinner. Going to look now.. Fish and Chips if you are curious
17th May 2017, 10:15 AM
jay
jay - avatar