查找随机数组中各个行和列的平均值

时间:2016-04-12 20:48:45

标签: c++ arrays

我必须制作一个程序,创建一个10x10的随机数0-9数组,找到每个行和列的平均值以及整个数组。我有正确的阵列,但我仍然坚持如何选择单行或列,然后找到它的平均值,任何提示?

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

const int numRows = 10;

const int numCols = 10;
int val[numRows][numCols];
int i, j;
double sum = 0, avgR, avgC;

void randNum() // Creates 10x10 array of random nums 0-9
{

    srand(2622); // random seed 

    for (i = 0; i < numRows; i++) // Creates random rows
    {
        cout << endl << endl;

        for (j = 0; j < numCols; j++) // Creates random columns
        {
            val[i][j] = rand() % 10;

            cout << setw(6) << val[i][j];
        }
    }
    cout << endl << endl;

}


void randAvg() // finds average of each row and column
{
    for (i = 0; i < numCols; i++)
    {
        sum += val[i][j];
    }

    avgR = sum / numRows;
    cout << "     " << avgR << endl;
}

int main() // calls each function
{

    randNum();
    randAvg();



}

2 个答案:

答案 0 :(得分:0)

在任何一种情况下,您都必须遍历所有数组条目。在构建列平均值时,您遍历每个列并在每个迭代中迭代所有行,计算平均值。 在行平均值期间,您遍历所有行,并在每个迭代中计算所有列的平均值:

void randAvg()
{

    for (int i = 0; i < numCols; i++)
    {
        float sum = 0.f;
        for (int j = 0; j < numRows; j++) {
            sum += val[i][j];
        }
        float avg = sum / numRows;
        cout << "Average of column " << i << ": " << avg << endl;
    }

    for (int i = 0; i < numRows; i++)
    {
        float sum = 0.f;
        for (int j = 0; j < numCols; j++) {
            sum += val[j][i];
        }
        float avg = sum / numRows;
        cout << "Average of row " << i << ": " << avg << endl;
    }

    float sum = 0.f;
    for (int i = 0; i < numRows; i++)
    {
        for (int j = 0; j < numCols; j++) {
            sum += val[j][i];
        }
    }
    float avg = sum / (numRows*numCols);
    cout << "Overall average: " << avg << endl;
}

免责声明:未经测试的代码应该可以使用。

答案 1 :(得分:0)

二维数组有一行和一列。当您在循环中访问此类数组的每个元素时,最好使用循环反映该结构。

所以这在你的代码中:

for (i = 0; i < numCols; i++)
{
        sum += val[i][j];
}
  1. 尝试访问阵列中每行(由j给出)的单个列(由i给出)
  2. 由于调用j函数现在使randNum()的值变为 10 ,因此您正在访问阵列之外的一个元素(技术上,当您在最后一行)这是不好的。这就是为什么使用全局变量在函数之间共享信息不是一个好主意。
  3. 有了这些:

    //assuming sum and avgR are declared
    
    //average of a row
    for (int i = 0; i < numRows; i++)//iterate each row(one loop for the row)
    {
        sum=0;//reset sum for each row
        for (int j = 0; j < numCols; j++)//iterate each column in each row(one loop for the column)
                sum += val[i][j];//will sum up the contents of the current row
    
        avgR=sum/numCols;//give you the average of each row
    }
    
    //average of a column
    for (int j = 0; j < numCols; j++)//iterate each column
    {
        sum=0;//reset sum for each column
        for (int i = 0; i < numRows; i++)//iterate each row for each column
                sum += val[i][j];//will sum up the contents of the current column
    
        avgR=sum/numRows;//give you the average of each column
    }
    

    我相信你能够弄清楚如何获得整个阵列的平均值。