c++ - 为每个奇数 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;
}
解决方案
推荐阅读
- java - 启动配置 all_client 引用不存在的项目库
- tomcat - 在 Apache Solr 4.4 中更改 synonyms.txt 文件后是否需要重新启动所有 tomcat solr 节点
- mongodb - SortByCount 并显示每个文档
- bash - 如何正确地将 Bash 数组分配给另一个变量?
- jquery - 选择带有“是”字样的列表选项时尝试显示 Div - Woocommerce
- events - Acumatica - 调用删除事件时更新其他订单的最佳解决方案是什么?
- postgresql - Postgres 对特定数据进行匿名/随机化所有具有数据域的数据
- arrays - perl - 访问数组数组哈希中的元素
- reactjs - 网站首次加载时反应状态未准备好
- import - 没有名为“Self”的模块