array

Apr 19, 2015 at 6:15am
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 ;)
Apr 19, 2015 at 10:13am
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?
Apr 19, 2015 at 10:20am
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).
Apr 19, 2015 at 10:52am
how do i do that? :)
Apr 19, 2015 at 12:46pm
Last edited on Apr 19, 2015 at 12:49pm
Apr 19, 2015 at 1:11pm
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;
}
Apr 19, 2015 at 1:16pm
@keskiverto

I think 90 degrees clockwise and transpose are same
http://en.wikipedia.org/wiki/Transpose#/media/File:Matrix_transpose.gif
Apr 19, 2015 at 6:14pm
Original:
ab
cd

Transposed:
ac
bd

CW rotated (as I understand it):
ca
db

VaMpZzz should tell us which is correct.
Apr 20, 2015 at 5:36am
Original:

ab
cd

CW rotated
ca
db

is the one i need :))
Last edited on Apr 20, 2015 at 5:37am
Apr 20, 2015 at 3:18pm
my bad :D I thought 90 degree cw and transpose are same :D
Apr 23, 2015 at 5:22am
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;
}
Apr 23, 2015 at 5:36am
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.