【发布时间】:2015-12-15 19:42:22
【问题描述】:
当我把所有东西都放在main 中时,这个查找负元素平均值的代码可以正常工作。问题是当我尝试将其拆分为功能时。如何连接 cinarray 和 negative_average 函数中的元素?
#include <iostream>
using namespace std;
int main()
{
cinarray();
negative_average();
}
int cinarray()
{
int A[3][3];
int i, j;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++) {
cout << "\n A[" << i + 1 << "][" << j + 1 << "]=";
cin >> A[i][j];
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++)
cout << A[i][j] << "\t";
cout << "\n";
}
// compute average of only negative values
int negative_average()
{
int negCount = 0;
int average = 0;
for (int x = 0; x < 3; ++x) {
for (int y = 0; y < 3; ++y) {
if (A[x][y] < 0) {
++negCount;
average += A[x][y];
}
}
}
if (negCount > 0) {
average /= negCount;
cout << "Average of only negative values \n" << average;
}
}
}
还有一件事为什么错误列表显示我需要“;”
int negative_average()
{ //here
int negCount = 0;
int average = 0;
【问题讨论】:
-
谁给了 +1 !!!
标签: c++ arrays function average