【发布时间】:2015-08-13 00:44:15
【问题描述】:
这是我一直在学习用 C++ 制作的数组。有什么方法可以让我进一步简化吗?
cout
int value[5] = { 17, 34, 51, 68, 85 }; // Creates an integer array list that contain a series of 5 values
for (int loop = 0; loop < 5; loop++) {
cout << "Integer value " << loop << ": " << value[loop] << endl;
}
cout << endl << "Array of double values" << endl; // First endl creates a blank line
cout << "=======================" << endl;
double numbers[10] = { 12.1, 24.2, 36.3, 48.4, 60.5, 72.6, 84.7, 96.8, 212.9, 3.0 }; // Creates an double integer array list that contains a series of 10 values
for (int loop = 0; loop < 10; loop++) { // Loops array
// Adding example " numbers[loop] = 77; " would set every number to 77
cout << "Element at index " << loop << ": " << numbers[loop] << endl;
}
cout << endl << "Initializing with zero values" << endl; // First endl creates a blank line
cout << "=======================" << endl;
for (int loop = 0; loop < 1; loop++) { // Loops array
// Adding " numbers[loop] = 77; " would set every number to 77
cout << "Element at index " << loop << ": " << numbers[loop] << endl;
}
cout << endl << "Initializing with strings" << endl; // First endl creates a blank line
cout << "=======================" << endl;
// Array of strings
string text = ( "Chair", "Table", "Plate" ) ;
; for (int loop = 0; loop < 3; loop++) { // Loops array
cout << "Element at index " << loop << ": " << text[loop] << endl;
return 0;
【问题讨论】:
-
不确定你想做什么
-
你可以删除
int numberArray[8] = {};它没有任何用处。没有真正进行计算,那么简化是什么意思? -
基于范围的 for 循环?
-
通过简化,我的意思是如何进一步压缩代码。我只是在制作一个基本的整数数组。我删除了 numberArray。
-
您可以编写一个打印数组的函数。这就是你要找的东西吗?