Я не специалист в C ++, но я делаю все возможное, у меня есть этот код, и я пытаюсь попросить пользователя ввести число от 1 до 12, и независимо от числа, которое они вводят, их 2D-массив будет скорректирован на их число, для пример:
Последние несколько дней я смотрел на множество примеров, но я не мог обвести вокруг себя и применить его к моему коду. Я был бы очень признателен, если кто-то может мне помочь, это то, что я добирался до сих пор:
#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 :(
";
}
}
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;
}
Всего 1 ответ
Вы можете использовать std::vector
и не заботиться о alllocation памяти, или вы создаете динамический массив и выделяете память на основе данного ввода.
Для динамического массива смотрите здесь