C++ 编程/练习/静态数组
外观
- 包含 <iostream>
使用名称空间 std;
void enterNumbers(int arr[], int n) {
cout << "Enter " << n << " numbers:\n"; for (int i = 0; i < n; ++i) cin >> arr[i];
}
void countAndTotal(int arr[], int n, int& pos, int& neg, int& total) {
pos = neg = total = 0; for (int i = 0; i < n; ++i) { if (arr[i] > 0) pos++; else if (arr[i] < 0) neg++; total += arr[i]; }
}
int main() {
int n, arr[100], pos, neg, total; cout << "Enter the number of elements: "; cin >> n;
enterNumbers(arr, n); countAndTotal(arr, n, pos, neg, total);
cout << "Positives: " << pos << "\nNegatives: " << neg << "\nTotal: " << total << endl;
return 0;
}