首页 > 解决方案 > 为每个奇数 j 索引查找多维数组模式

问题描述

好的,所以我试图为每个奇数的 j(包括 0)插入一个空格字符,问题是 0 算作偶数(我希望它算作奇数,因此可以放置一个空格字符)和除了玩奇数或偶数j之外,我正在努力寻找解决方案。目标是使数组中的每个元素都有一个字母,并为下一个索引提供一个空格字符。该函数负责填充数组。

void createBoard(char arr [DIM][DIM], int size){
  //ASCII number for capital A
  char x = 65;
    for(int i = 0; i<size; i++){
    for(int j = 0; j<size; j++){
      if(j%2==0){
        arr[i][j] = x++;
      }
      else{
     arr[i][j] = 32;
      }
 cout << "Element at x[" << i << "][" << j << "]: ";
          cout << arr[i][j] << endl;

  }
  
  }
}

这是主要功能。

const int DIM = 7;
int main()
{
char arr [DIM][DIM];
int bsize;
char answer;
do{
        cout << "Please enter the size of the board [1-7]: ";
        cin >> bsize;
        if(!cin){
            cout << endl << "Invalid entry";
            break;
        }
        if (bsize<=DIM && bsize>=1){
    createBoard(arr,bsize);
        }
        else{
        cout << endl << "Invalid size";
        cout << endl << "Do you want to try again [y-n]?: ";
        cin >> answer;
        }
// As long as the answer is 'y' (in upper or lower case), keep looping      
}while(answer=='Y'||answer=='y');
    
    return 0;
}

标签: c++arraysmultidimensional-array

解决方案


推荐阅读