我有以下格式打印时间的代码:00 01 02 03 04 05 06 07 08 09 10 11...只是第一个数字 (0) 需要高于第二个数字 (0)。 . 下面是我的
#include <iostream>
using namespace std;
void printArray (int arg[], int length) {
for (int n=0; n<length; ++n)
cout << arg[n] << ' ';
cout << '\n';
}
int main()
{
int ftime[99] = {0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,2};
int ttime[99] = {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0};
cout << "Time: ";printArray(ftime,21);
cout << " ";printArray(ttime, 21);
return 0;
}
现在打印出以下内容:
Time: 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
这正是我想要的,但我需要一直这样做到 99,并且想知道是否有比我这样做更简单的方法来初始化 ftime 和 ttime 数组。任何输入将不胜感激!
最佳答案
只需给它一个循环即可。
#include <iostream>
using namespace std;
void printArray (int arg[], int length) {
for (int n=0; n<length; ++n) {
cout << arg[n] << ' ';
}
cout << '\n';
}
int main()
{
int ftime[100];
int ttime[100];
for (int i = 0; i < 100; i++) {
ftime[i] = i / 10;
ttime[i] = i % 10;
}
cout << "Time: ";
printArray(ftime,100);
cout << " ";
printArray(ttime,100);
return 0;
}
关于c++ - 初始化数组 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26660971/