【发布时间】:2018-11-26 20:36:49
【问题描述】:
好的,所以我有这个代码,它基本上会提示用户输入一个数字,然后根据用户输入的数字,他们会被问到(请输入一个介于 1 和 4 之间的数字)* 用户选择的数字.然后,将比较它们的输入以查看网格中是否有任何匹配项(行和列)。让我给你举个例子:
- 请输入一个数字:3
- 请输入一个介于 1 和 4 之间的数字:2
- 请输入一个介于 1 和 4 之间的数字:1
- 请输入一个介于 1 和 4 之间的数字:2
这是您的网格: (3x3网格填充,因为用户输入3,填充随机数)
4 2 4
3 1 1
4 3 3
这是我的代码示例:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <time.h>
#include <ctime>
using namespace std;
double Atemp = 0;
double Utemp = 0;
double Working = 0;
double Total = 0;
char Answer = 'x';
int Umain;
void printGrid(int &Umain);
void fillIntArray(int array[], int size);
void reverseArray(int array[], int size);
void outputIntArray(int array[], int n);
void compareGrid(int &Atemp);
int main(){
int maxNum = 2;
int intArray[maxNum];
cout << "Please Enter numbers between 1 and 12: ";
cin >> Umain;
do{
if(Umain <=12){
fillIntArray(intArray, maxNum);
//outputIntArray(intArray, maxNum);
printGrid(Umain);
}
}while (Answer == 'y');
return 0;
}
void fillIntArray(int array[], int size){
do{
for (Utemp = Umain; Utemp > 0; Utemp--){
cout << "Please enter a number between 1 and 4: ";
cin >> Atemp;
if(Atemp <=4 && Atemp >=1){
for (int i = Atemp; i < Atemp; i++);
}else{
cout << "Not within limit \n";
}
}
}while (Answer == 'y');
}
void printGrid(int &Umain){
cout<<endl;
cout<<" ";
int i=1,j;
for(j = 0; j <= 4*Umain; j++){
if(j%4==2){
cout<<" ";
}
}
cout<<endl;
for(i = 0; i <= 2*Umain; i++){
for(j = 0; j <= 2*Umain; j++){
if(i%2==0){
if(j==0){
cout<<" ";
}
if(j%2==0){
cout<<" ";
}else{
cout<<"---";
}
}else{
if(j%2==0){
cout<<" | ";
}else cout<< (rand()%4+1);
}
}
if(i%2!=0){
cout<<" ";
}
cout<<endl;
}
cout<<" ";
for(j = 0, i = 1; j <= 4*Umain; j++){
if(j%4==2){
cout<< " ";
}
}
cout<<endl;
}
void compareGrid(int &Atemp){
}
【问题讨论】:
-
你的问题是?
-
如何比较网格的行和列 ??????它的书面,字面意思
-
网格行和列 => 矩阵。这是正确的术语
-
你所说的“比较”是什么意思?比较每个元素?
-
比较用户输入的数字和二维数组中的数字,是否匹配,如果输入的是2 3 2,在行或列中是否有相同的数字按顺序匹配二维数组?我该怎么做?
标签: c++ arrays multidimensional-array