array

can anyone help me with this task? :)

With random generator help fill a two- dimensional array of NxN with numbers in the range of 10-99 , and discard it to the screen . Turn an array of 90 ° clockwise . Expelos the score screen .

thank you ;)
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
#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{

    srand(time(0));
    const int N=3;
    int max = 99;
    int min = 10;
    int a[N][N];
        for(int i=0; i<N; i++)
        for(int j=0; j<N; j++)
                a[i][j] = rand() % (max - min)+ min;
    for(int i=0; i<N; i++)
    {
        for(int j=0; j<N; j++)
                cout << a[i][j] << " ";
        cout << endl;
    }

    system("PAUSE");
    return EXIT_SUCCESS;
}


i got it so far how do i rotate 90 ° clockwise?
It is index math.

The first row of the original array are the elements from the top left to the top right.
The first row of the rotated array are the elements from the bottom left to the top left (of the original matrix).
how do i do that? :)
Last edited on
Transpose is different from rotate.

The following code does show neither, but contains a hint about calculating indices:
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
#include <iostream>
#include <chrono>
#include <random>

int main()
{
  unsigned seed1 = std::chrono::system_clock::now().time_since_epoch().count();
  std::mt19937 generator( seed1 );
  std::uniform_int_distribution<int> distribution( 10, 99 );

  constexpr size_t N = 3;
  int a[N][N];

  for( size_t row=0; row < N; ++row )
    {
      for( size_t col=0; col < N; ++col )
        {
          a[row][col] = distribution(generator);
        }
    }


  for( size_t row=0; row < N; ++row )
    {
      for( size_t col=0; col < N; ++col )
        {
          std::cout << a[row][col] << ' ';
        }
      std::cout << '\n';
    }

  std::cout << '\n';

  for( size_t row=0; row < N; ++row )
    {
      for( size_t col=0; col < N; ++col )
        {
          std::cout << a[N-row-1][col] << ' ';
        }
      std::cout << '\n';
    }

  return 0;
}
@keskiverto

I think 90 degrees clockwise and transpose are same
http://en.wikipedia.org/wiki/Transpose#/media/File:Matrix_transpose.gif
Original:
ab
cd

Transposed:
ac
bd

CW rotated (as I understand it):
ca
db

VaMpZzz should tell us which is correct.
Original:

ab
cd

CW rotated
ca
db

is the one i need :))
Last edited on
my bad :D I thought 90 degree cw and transpose are same :D
this is how i did this not sure if its the right way but it works :)

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>
using namespace std;

int main()
{

    srand(time(0));
    const int N=3;
    int max = 99;
    int min = 10;
    int a[N][N];
        for(int i=0; i<N; i++)
        for(int j=0; j<N; j++)
                a[i][j] = rand() % (max - min)+ min;
    
    cout << "arry \n"<< endl;
                
    for(int i=0; i<N; i++)
    {
            for(int j=0; j<N; j++)
                cout << a[j][N-i-1] << " ";
        cout << endl;
       
    
   }
cout << endl;
cout << "arry 90 cw  \n\n"<< endl;
    for(int i=0; i<N; i++)
    {
        for(int j=0; j<N; j++)
                cout << a[i][j] << " ";
        cout << endl;
}
cout << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
and another variant
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>
using namespace std;

int main()
{

    srand(time(0));
    const int N=3;
    int max = 99;
    int min = 10;
    int a[N][N];
        for(int i=0; i<N; i++)
        for(int j=0; j<N; j++)
                a[i][j] = rand() % (max - min)+ min;
    
    cout << "arry \n"<< endl;
                
    for(int i=0; i<N; i++)
    {
            for(int j=0; j<N; j++)
                cout << a[i][j] << " ";
        cout << endl;
       
    
   }
cout << endl;
cout << "arry 90 cw  \n\n"<< endl;
    for(int i=0; i<N; i++)
    {
        for(int j=0; j<N; j++)
                cout << a[N-j-1][i] << " ";
        cout << endl;
}
cout << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}


Topic archived. No new replies allowed.