带随机数的数组并旋转它

时间:2015-11-19 16:09:11

标签: c++ arrays

我有一个编程类的作业,我必须创建一个填充了10到99之间随机数的NxN数组。在屏幕上打印,然后旋转90度再打印。

基本上这就是我需要的:

Original:
1 2 3 
4 5 6
7 8 9

Rotated
7 4 1
8 5 2
9 6 3

创建原始数组没有问题。但我无法弄清楚如何旋转它。

这是我到目前为止所得到的:

#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=3;
    const int M=3;
    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;
    //Place holder for rotated array
    system("pause");
    return 0;
}

请帮助:(

0 个答案:

没有答案