C++ 什么';我的代码有问题(需要帮助修复)

C++ 什么';我的代码有问题(需要帮助修复),c++,arrays,random,matrix,multidimensional-array,C++,Arrays,Random,Matrix,Multidimensional Array,所以我试图创建一个大小为N=6的随机整数的平方矩阵。例如,对于N=4,可能的矩阵为: 1 4 9 3 6 3 9 3 4 5 1 2 0 1 3 4 然后,我将对数字进行排序并存储回填充每行的矩阵中: 0 1 1 1 2 3 3 3 3 4 4 4 5 6 9 9 我已经写了一些代码,但是在生成随机数方面仍然有一些错误。任何帮助都将不胜感激 #include <iostream> #include <iomanip> #include <ctime> #i

所以我试图创建一个大小为N=6的随机整数的平方矩阵。例如,对于N=4,可能的矩阵为:

1 4 9 3
6 3 9 3
4 5 1 2
0 1 3 4
然后,我将对数字进行排序并存储回填充每行的矩阵中:

0 1 1 1
2 3 3 3
3 4 4 4
5 6 9 9
我已经写了一些代码,但是在生成随机数方面仍然有一些错误。任何帮助都将不胜感激

#include <iostream>
#include <iomanip>
#include <ctime>
#include <cassert>

using namespace std;

const size_t N = 6;
const int MIN_VAL = 10;
const int MAX_VAL = 99;

unsigned random(double rangeMin, double rangeMax);
void print2d(int **, size_t, size_t);
void selectionSort2d(int **, size_t, size_t);

void selectionSort2d(int **a, size_t rows, size_t cols) {
    size_t minX, minY, x = 0, y = 0, i, k;
    int t;

    while (x < rows) {
        minX = x; minY = y;
        i = x; if ((k = (y+1) % cols) == 0) ++i;
        while (i < rows) {
            while (k < cols) {
                if (a[i][k] < a[minX][minY]) {
                    minX = i; minY = k;
                }
                ++k;
            }
            ++i; k = 0;
        }
        t = a[minX][minY];
        a[minX][minY] = a[x][y];
        a[x][y] = t;

        if ((y = (y + 1) % cols) == 0) ++x;
    }
}

void print2d(int **a, size_t rows, size_t cols) {
    cout << endl;
    for (size_t i = 0; i < rows; i++) {
        for (size_t j = 0; j < cols; j++) {
            cout << setw(3) << a[i][j];
        }
        cout << endl;
    }
    cout << endl;
}

unsigned random(double rangeMin, double rangeMax) {
    double maxN;
    assert(rangeMin <= rangeMax);
    maxN = rangeMax - rangeMin + 1;
    return (unsigned)(((rand() / (double)RAND_MAX) * maxN) + rangeMin);
} 

int main(int argc, char *argv[]) {
    int **intArray;
    time_t t;

    // Allocate
    intArray = new int*[N];
    for (size_t i = 0; i < N; i++) {
        intArray[i] = new int[N];
    }

    // Randomize
    srand((unsigned)time(&t));
    for (size_t i = 0; i < N; i++) {
        for (size_t j = 0; j < N; j++) {
            intArray[i][j] = random(MIN_VAL, MAX_VAL);
        }
    }

    // Display
    cout << endl << "Random:";
    print2d(intArray, N, N);

    // Sort
    selectionSort2d(intArray, N, N);

    // Display
    cout << "Sorted:";
    print2d(intArray, N, N);

    // Free
    for (size_t i = 0; i < N; i++) {
        delete [] intArray[i];
    }
    delete [] intArray;

    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
常数大小N=6;
const int MIN_VAL=10;
常数int MAX_VAL=99;
无符号随机(双量程最小值、双量程最大值);
无效打印2D(整数**,大小t,大小t);
无效选择或2D(整数**,大小t,大小t);
无效选择或2D(整数**a,大小行,大小列){
尺寸minX,minY,x=0,y=0,i,k;
int t;
while(x<行){
minX=x;minY=y;
i=x;如果((k=(y+1)%cols)==0)++i;
while(i
#include <cstdlib>
#包括

您遇到了什么错误?您尝试了什么?我遇到了3个错误。第55行:错误“rand”未在此范围内声明。第55行:错误“rand_MAX”未在此范围内声明。第69行:错误“srand”未在此范围内声明。我运行了您的代码,效果很好。没有错误。谢谢大家。我只是忘记了包含需要添加的cstdlib使用rand。已经醒了太久:)谢谢。我几乎不敢相信我忘记包含cstdlib了。>_