Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何根据用户调整二维网格大小? 不是C++专家,但我尽我所能,我有这个代码,我试图让用户输入一个1到12之间的数字,并且不管它们输入的是什么数字,它们都会被调整到它们的数量,例如: 请输入一个数字:3 程序将输出一个3x3大小的2D数组,如果他们输入“4”,它将是一个4x4网格,等等_C++_Arrays - Fatal编程技术网

如何根据用户调整二维网格大小? 不是C++专家,但我尽我所能,我有这个代码,我试图让用户输入一个1到12之间的数字,并且不管它们输入的是什么数字,它们都会被调整到它们的数量,例如: 请输入一个数字:3 程序将输出一个3x3大小的2D数组,如果他们输入“4”,它将是一个4x4网格,等等

如何根据用户调整二维网格大小? 不是C++专家,但我尽我所能,我有这个代码,我试图让用户输入一个1到12之间的数字,并且不管它们输入的是什么数字,它们都会被调整到它们的数量,例如: 请输入一个数字:3 程序将输出一个3x3大小的2D数组,如果他们输入“4”,它将是一个4x4网格,等等,c++,arrays,C++,Arrays,在过去的几天里,我看了很多例子,但我无法将其应用到我的代码中。如果有人能帮助我,我将不胜感激,这就是我到目前为止所做的: #include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #include <iomanip> #include <time.h> #include <vector> using namespace s

在过去的几天里,我看了很多例子,但我无法将其应用到我的代码中。如果有人能帮助我,我将不胜感激,这就是我到目前为止所做的:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <time.h>
#include <vector>

using namespace std;

double Atemp = 0;
double Utemp = 0;
double Working = 0;
double Total = 0;
char Answer = 'x';
int Umain;
const int ROWS = ?;
const int COLS = ?;

typedef float Table[ROWS][COLS];


void displayOverview ();

void playOrQuit();

void outputSubSquare(Table table, int x, int y);
void fillTableWithRands(Table table);
void outputTable(Table table);

void fillIntArray(int array[], int size);
void reverseArray(int array[], int size);
void outputIntArray(int array[], int n);

int main(){

    displayOverview();

    playOrQuit();

    int maxNum = 4;
    int intArray[maxNum];
    fillIntArray(intArray, maxNum);
    outputIntArray(intArray, maxNum);

    srand(time(NULL));
    Table myTable;

    fillTableWithRands(myTable);
    outputTable(myTable);

    return 0;
}

void displayOverview(){

}

void playOrQuit(){

}

void fillIntArray(int array[], int size){

    do{
        cout << "Please Enter numbers between 1 and 12: ";
        cin >> Umain;

        if(Umain <= 12){
            for (Utemp = Umain; Utemp > 0; Utemp--){
                cout << "Please enter a number between 1 and 4: ";
                cin >> Atemp;

                for(int i = Atemp; i<0;i++){
                    cin >> array[i];
                }
            }
        }
        else{
            cout << "Not within limit :(\n";
        }
    }

    while (Answer == 'y');
}

void fillTableWithRands(Table table){
    for(int i = 0; i<ROWS; i++){
        for (int j = 0; j<COLS; j++){
            table[i][j]  = rand()%4+ 1;
        }
    }
}


void outputTable(Table table){
    for(int i = 0; i<ROWS; i++){
        for (int j = 0; j<COLS; j++){
            cout << table[i][j] << " ";
        }
    cout << endl;
    }
}


void outputIntArray(int array[], int n){
    for(int i = 0; i<n; i++){
        printf("%d", array[i]);
    }
    cout << endl;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
双Atemp=0;
双Utemp=0;
双工作=0;
双倍合计=0;
char Answer='x';
国际货币基金组织;
常量int行=?;
常数int COLS=?;
typedef浮点表[行][COLS];
void displayOverview();
void playOrQuit();
void outputSubSquare(表格,整数x,整数y);
用RANDS(表格)填充表格;
无效输出(表格);
无效填充数组(int数组[],int大小);
void reversearlay(int数组[],int大小);
void outputIntArray(int数组[],int n);
int main(){
displayOverview();
playOrQuit();
int maxNum=4;
int intArray[maxNum];
fillIntArray(intArray,maxNum);
输出阵列(intArray,maxNum);
srand(时间(空));
表myTable;
用RANDS填充表格(myTable);
可输出(myTable);
返回0;
}
void displayOverview(){
}
void playOrQuit(){
}
无效填充数组(整数数组[],整数大小){
做{
cout>Umain;
如果(Umain 0;Utemp--){
cout>Atemp;
for(int i=Atemp;i>array[i];
}
}
}
否则{
cout

您可以使用
std::vector
而不必关心内存的所有位置,也可以创建一个动态数组并根据给定的输入分配内存


对于动态数组,请查看std::vector网格(行,std::vector(cols))
最大的问题——你可以使用
std::vector
吗?如果不可以,如果
vector
不允许,我可以建议你围绕一个动态分配的数组创建一个简单的矩阵类吗?这里有一个很好的例子:如何使用std::vector,我应该把它放在哪里?如果这是一个愚蠢的问题,很抱歉,但我从来没有使用过std::vector。“LailaMurish,但我从来没有使用过ST::向量。如果没有告诉你如何创建动态的2维数组,你怎么完成任务?”关于<代码>矢量< /C> >的用法太大了,这是通过阅读好的C++书籍和教程最好的。是的,我刚才问,我们不允许使用向量,我不这样做。明白为什么吗?(顺便说一句,感谢你的帮助,非常感谢)。