我有一个问题要问。如何让它在ARRAY的每一行打印10个元素。
例如:
88 11 88 11 88 11 88 11 88 11
11 88 11 88 11 88 11 88 11 88
88 11 88 11 88 11 88 11 88 11
11 88 11 88 11 88 11 88 11 88
88 11 88 11 88 11 88 11 88 11
11 88 11 88 11 88 11 88 11 88
88 11 88 11 88 11 88 11 88 11
11 88 11 88 11 88 11 88 11 88
88 11 88 11 88 11 88 11 88 11
11 88 11 88 11 88 11 88 11 88
这是我目前的代码:
int main()
{
int test[ARRAY_SIZE][ARRAY_SIZE];
for(int i = 0; i < ARRAY_SIZE; i++){
for (int j = 0; j < ARRAY_SIZE; j++){
if ((i+j)%2 == 0){
test[i][j] = 88;
}
else if ((i+j)%2 == 1){
test[i][j] = 11;
}
}
}
}
我应该添加什么代码才能让每行打印10个元素?请帮帮我D:先谢谢! :)
答案 0 :(得分:0)
#include <iostream>
#define ARRAY_SIZE 10
int main()
{
int test[ARRAY_SIZE][ARRAY_SIZE];
for(int i = 0; i < ARRAY_SIZE; i++){
for (int j = 0; j < ARRAY_SIZE; j++){
if ((i+j)%2 == 0){
test[i][j] = 88;
}
else if ((i+j)%2 == 1){
test[i][j] = 11;
}
}
}
for(int i = 0; i < ARRAY_SIZE; i++){
for (int j = 0; j < ARRAY_SIZE; j++){
std::cout << test[i][j] << " ";
}
std::cout << "\n";
}
}
希望这有帮助。
答案 1 :(得分:0)
这看起来像是分配/作业
您的代码只是填充数组
cout
或者来自stdio.h或conio.h的printf
,不确定现在是否已经使用这些算法超过十年了 10
个值
cout << endl;
或printf("\n");
int k=0;
if (k>=10) { k=0; cout <<endl; }
10
更改为+/- 1以匹配每行10个值栅格目标设备
int x=x0,y=y0; // start position for output
k
时,还要添加x+=width_for_single_value;
k
设置为零添加y+=height_for_single_value; x=x0;
列对齐
printf("%4i ",test[i][j]);
int val=123;
if (val<10) cout << "0";
if (val<100) cout << "0";
if (val<1000) cout << "0";
if (val<10000) cout << "0";
cout << val << " ";