Array with random numbers and rotating it

Nov 18, 2015 at 1:21pm
Hi! :)

I would much appreciate some help with my assignment for programming class.
We have to populate a two dimensional NxN array with random numbers from 10 to 99 and print it on the screen. So far so good :)

Now this array must be rotated by 90 degrees clockwise and again printed on screen. Here is where I'm stuck. I've read that it can be done by transposing and reversing, but how?

Here is what I have so far (just the random number generation and original array printing).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;

void Init_Array (int *a, int N, int M)
{
    for(int i=0; i<N*N; i++)
        *(a+i) = rand()%(99-10)+10;
}

void Print_Array(int *a, int N, int M)
{
    for(int i=0; i<N*N; i++)
    {
        cout << *(a+i) << " ";
        if ((i+1)%N==0) cout << endl;
    }
    cout << endl;
}

int main()
{
    time_t t;
    srand((unsigned) time(&t));
    const int N=9;
    const int M=9;
    int a[N][M];
    Init_Array(*a,N,M);
    cout << "Original Array: " << endl;
    cout << endl;
    Print_Array(*a,N,M);
    cout << endl;
    cout << "Rotated Array: " << endl;
    cout << endl;
    //placeholder for rotated array
    system("pause");
    return 0;
}
Nov 18, 2015 at 1:35pm
Nov 18, 2015 at 1:43pm
Just for your information, the instructions that you are receiving in your class are outdated. Use std::vector instead of c-style arrays.
Last edited on Nov 18, 2015 at 1:44pm
Nov 18, 2015 at 1:56pm
@Kevin C He is studying in school. It's an assignment. They have to use Arrays. They probably havent gotten to vectors yet.
Nov 18, 2015 at 3:33pm
Yes, I thought that also, that is exactly why I wrote it, to inform the OP. I think he has the right to know that information.

std::vector should be taught before c-style arrays.
Nov 18, 2015 at 10:02pm
@Thomas1965
Thanks, but I guess I suck at this. :(
I can't figure out from the link how to do it.
There are tips how to rotate array with one line, but I need it like this:

Original:
1 2 3
4 5 6
7 8 9

Rotated:
7 4 1
8 5 2
9 6 3
Nov 19, 2015 at 5:00pm
OK. I've changed the code. Now I'm not using functions.
I've got it to rotate, but to the wrong side - it's rotating counter-clockwise, but I need it clockwise. Just need a small pointer to why is that.

Here is new code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;

int main()
{cout << endl;
	cout << "Original array:" << endl;
	cout << endl;
    time_t t;
    srand((unsigned) time(&t));
    const int N=3,M=3;
    double a[N][M];
    for(int i=0; i<N; i++)
        for(int j=0; j<M; j++)
                a[i][j] = rand()%(99-10)+10;
    for(int i=0; i<N; i++)
    {
        for(int j=0; j<M; j++)
                cout << a[i][j] << " ";
        cout << endl;
    }
    cout << endl;
 	cout << "Rotated array:" << endl;
	 cout << endl;   
int tmp;
for (int i=0; i<N/2; i++){
        for (int j=i; j<N-i-1; j++){
                tmp=a[i][j];
                a[i][j]=a[j][N-i-1];
                a[j][N-i-1]=a[N-i-1][N-j-1];
                a[N-i-1][N-j-1]=a[N-j-1][i];
                a[N-j-1][i]=tmp;
        }
}

for(int i=0; i<N; i++)
    {
        for(int j=0; j<M; j++)
                cout << a[i][j] << " ";
        cout << endl;
    }
cout << endl;    
system("pause");
return 0;
}
Topic archived. No new replies allowed.