【问题标题】:How to compare grid rows and columns with user input?如何将网格行和列与用户输入进行比较?
【发布时间】: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


【解决方案1】:

一方面,可变大小数组不是 C++ 的一部分。这部分是错误的:

int maxNum = 2;
int intArray[maxNum];

通过添加constexpr来修复它:

constexpr int maxNum = 2;
int intArray[maxNum];

其次,这部分一般没有意义,最后的分号真的是怀疑:

for (int i = Atemp; i < Atemp; i++); // why?

假设您已将用户输入收集到一个名为 Attempts 的大小为 3 的数组中,而 intArray 是大小为 3x3 的矩阵,您可以通过以下方式将尝试与所谓的 2D 的行/列进行比较数组:

bool Contains() {
    for (size_t iCol = 0; iCol != 3; ++iCol) {
        for (size_t iRow = 0; iRow != 3; ++iRow) {
            if(intArray[iCol][iRow] != Attempts[iRow]) {
                return false;
            }
        }
    }
    return true;
}

更改iColiRow 的位置以检查行或列

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多