i think i did a decent job, can you please check?
Posted by shaynjam@reddit | learnprogramming | View on Reddit | 11 comments
this is a question gemini generated for me.
The Scenario: You are building a Tetris clone or a top-down game. You have a 3x3 block (a 2D array of numbers). The player hits the "Rotate" button. You need to rotate the entire grid 90 degrees clockwise.
example:
starting-> 1 2 3
4 5 6
7 8 9
after rotation-> 7 4 1
8 5 2
9 6 3
heres my programme for it:
#include <iostream>
using namespace std ;
int main()
{
int x=0 ;
char opinion = 'a' ;
int store[9];
int m=0;
int textureMap[3][3]=
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
};
for ( int i =0 ; i < 3 ; i++)
{
for (int j = 0 ; j<3 ; j++)
{
cout << textureMap[i][j]<< " ";
}
cout<< endl ;
}
cout << endl ;
cout<< " do you wanna rotate?(Y/N): ";
std::cin>> opinion ;
if (opinion == 'Y' || opinion == 'y')
{
for ( int a =0 ; a <3 ; a++)
{
for (int b = 2 ; b>=0 ; b= b - 1 )
{
store[x] = textureMap[b][a] ;
x=x+1;
}
cout << endl ;
}
for ( int i = 0 ; i< 3; i++)
{
for (int j = 0 ; j<3 ; j++)
{
{textureMap[i][j] = store[m];}
cout << textureMap[i][j]<< " ";
m=m+1;
}
cout << endl ;
}
}
std::cin.clear();
std::cin.ignore(1000, '\n') ;
std::cin.get();
return 0 ;
}
Queasy_Hotel5158@reddit
Nice work — your rotation logic is actually correct.
I like that you figured out the index pattern yourself using:
textureMap[b][a]because that’s the key idea behind matrix rotation.
Your program also keeps the original matrix separate while building the rotated version in
store[], which avoids overwriting values accidentally. That’s a smart approach for beginners.One small improvement: instead of using a 1D
store[9], you could directly write into another3x3array to make the code easier to read and more scalable later.Overall though, this is a good implementation and shows you understand nested loops + coordinate transformations well.
shaynjam@reddit (OP)
I really appreciate the feedback! I didn't think of the 3x3 array maybe because I only learnt 2d array yesterday and basically I'm just a beginner with about 4 weeks of practice so I'm not that farsighted rn
lukkasz323@reddit
you're doing good, keep going
RajjSinghh@reddit
By eye it looks fine. I'm not at my computer so I can't see it it runs properly, but there are a few things I'd do to generalise it.
I'd pull this rotation logic into its own function. Something like
void rotate(int array[3][3]) {so that this code can be used again. There's no need to do that here since it's a trivial program, but it's a good habit to get into.But then I'd also try to generalise. What if I want to rotate a 4x4 array? Or 5x5? Instead of passing an
int[3][3]we can use templates so we don't need to specify the length and width of our array.So it's not a problem with what you wrote, but more that you can now use different features of C++ to make the code more applicable to other situations.
shaynjam@reddit (OP)
I do get your point but this exercise already took me an hour to successfully write 😂 I know it's a lot but I'm just a beginner right now I have roughly 4 weeks of experience out of which I must have practiced for roughly 2.5 weeks I didn't think this far as I'm just a novice but I will keep this in mind now! Thanks !
RajjSinghh@reddit
The good news is that since the logic works, the hard bit is done. I'm more suggesting organisational things. Once you take a break, you should come back and look at what I'm saying.
Once you know about functions this gets a lot easier. What if you want to rotate your array twice, or rotate a different array? Using a function your rotate would look something like
So that in main you can do
For situations where you may want to reuse that code to rotate other arrays.
Guilty-Property-2999@reddit
The rotation logic itself works but using a separate store array for 9 elements feels bit overcomplicated when you could do the rotation in place with just temporary variables
8Erigon@reddit
Yes you can use templates, std::array or std: span for geralisation.
Or simple just a normal array (-pointer) and a size as a parameter.
I‘d also recommend doing it for all sizes!
shaynjam@reddit (OP)
Being honest I have no idea what those are 😂 I just started learning c++ and it's my first language. But thanks for the reply I really appreciate it
8Erigon@reddit
Put variable decleration to where you need them.
Not at the start of the programm/function. Were not doing C with pre 2000 standarts.
shaynjam@reddit (OP)
Yeah my bad I will keep that in mind! Thanks