C++求整型数组的和
#include <iostream> using namespace std; int main() { int a[5] = { 1, 3, 5, 7, 9 }; int sm = 0; //注意这里要给默认值 for (int i = 0; i < 5; i++) { sm += a[i]; cout << "a[i]=" << a[i] << ", sm=" << sm << endl; } return 0; }
#include <iostream> using namespace std; int main() { int a[5] = { 1, 3, 5, 7, 9 }; int sm = 0; //注意这里要给默认值 for (int i = 0; i < 5; i++) { sm += a[i]; cout << "a[i]=" << a[i] << ", sm=" << sm << endl; } return 0; }