数组的长度可以是变量;
int num[7] = {8,4,2,1,23,344,12 }; double sum = 0; for (int i = 0; i < sizeof(num)/sizeof(num[0]); i++) { cout << num[i] << endl; sum += num[i]; } cout << sum << endl; cout << sum/7 << endl;
//求数组最大值 int max = nums[0]; int numsLen = sizeof(nums) / sizeof(nums[0]); int maxIndex = 0; for (int i = 0; i < numsLen; i++) { if (nums[i] > max) { max = nums[i]; maxIndex = i; } } cout << "该数组的最大值是:" << max << "\n" << "最大值的索引是:" << maxIndex << endl;
1