如何在C ++中从二维数组中找到列平均值

时间:2013-09-16 20:27:22

标签: c++ arrays

我试图找到每列的平均值(即{5,5,6,8,4,6,8,8,8,10}并为每行打印:以下是代码。我继续在印刷品上获得0,我确信在某处出现了问题。任何帮助都会很棒。谢谢。

   int ScoreAverage() // average of artist scores
{
    const int A1_SIZE = 5,  A2_ROWSIZE =5, A2_COLSIZE =10;
    string Artist[A1_SIZE]={ "Degas", "Holbien", "Monet", "Matisse", "Valesquez" };
    int Scores[A2_ROWSIZE][A2_COLSIZE] = {{5,5,6,8,4,6,8,8,8,10},{8,8,8,8,8,8,8,8,8,8},
     {10,10,10,10,10,10,10,10,10,10},{5,0,0,0,0,0,0,0,0,0},{5,6,8,10,4,0,0,0,0,0}};

     cout << "\n\n\t-------------------------------------------" << endl;
     cout << "\t\tAverage Scores by Artist"<< endl;
     cout << "\t===========================================" << endl;

    int total = 0;
    double average, faverage;


   for (int x=0; x<5; x++)
   {
     cout << "\n\t" << Artist[x] << "\t\t";

        for (int col = 0; col < 5; col++)
        {

            for (int row = 0; row < 10; row++)
            {
               total+=Scores[row][col];
            }
        average = total/10;
        average = faverage;

        }
        average = total = 0;
        cout << "\tAverage ... " << faverage << endl;
    }
}

6 个答案:

答案 0 :(得分:2)

你可以使用它。

 const int A1_SIZE = 5,  A2_ROWSIZE =5, A2_COLSIZE =10;
 string Artist[A1_SIZE]={ "Degas", "Holbien", "Monet", "Matisse", "Valesquez" };
 int Scores[A2_ROWSIZE][A2_COLSIZE] = {{5,5,6,8,4,6,8,8,8,10},{8,8,8,8,8,8,8,8,8,8},
 {10,10,10,10,10,10,10,10,10,10},{5,0,0,0,0,0,0,0,0,0},{5,6,8,10,4,0,0,0,0,0}};

 cout << "\n\n\t-------------------------------------------" << endl;
 cout << "\t\tAverage Scores by Artist"<< endl;
 cout << "\t===========================================" << endl;

double total = 0;
double average = 0;


for (int i = 0; i < 5; i++)
{
   for (int j = 0; j < 10; j++)
   {
       total += Scores[i][j];
   }
   average = total/10;
   cout << "\n\t" << Artist[i] << "\t\t";
   cout << "\tAverage ... " << average << endl;
   average = 0;
   total = 0;
}

答案 1 :(得分:2)

方法一

int ScoreAverage() // average of artist scores
{
    // not necessary to have 2 row size constants since these are related
    // could make this even better by putting the artist name and scores in a class
    const int ROWSIZE = 5;  
    const int COLSIZE = 10;
    std::string Artist[ROWSIZE] = { "Degas", "Holbien", "Monet", "Matisse", "Valesquez" };
    int Scores[ROWSIZE][A2_COLSIZE] = {
                                            {5,5,6,8,4,6,8,8,8,10},
                                            {8,8,8,8,8,8,8,8,8,8},
                                            {10,10,10,10,10,10,10,10,10,10},
                                            {5,0,0,0,0,0,0,0,0,0},
                                            {5,6,8,10,4,0,0,0,0,0}
                                        };

    std::cout << "\n\n\t-------------------------------------------" << std::endl;
    std::cout << "\t\tAverage Scores by Artist"<< std::endl;
    std::cout << "\t===========================================" << std::endl;

    for (int x = 0; x < ROWSIZE; x++)
    {
        std::cout << "\n\t" << Artist[x] << "\t\t";
        int total = std::accumulate(std::begin(Scores[row]), std::end(Scores[row]), 0);
        double average = static_cast<double>(total) / static_cast<double>(COLSIZE);
        std::cout << "\tAverage ... " << average << std::endl;
    }
}

方法二

class Artist
{
private:
    std::string m_Name;
    std::vector<int> m_Scores;

public:
    // constructors and accessors here

    double Average() const
    {
        int total = std::accumulate(m_Scores.begin(), m_Scores.end(), 0);
        return static_cast<double>(total) / static_cast<double>(m_Scores.size());
    }   
};

void printAverage(const Artist& a)
{
    std::cout << "\n\t" << a.GetName() << "\t\t" << a.Average() << std::endl;
}

int main()
{
    std::vector<Artist> artists(5);
    // fill in artists here
    std::for_each(artists.begin(), artists.end(), printAverage);
    return 0;
}

答案 2 :(得分:1)

int ScoreAverage() // average of artist scores
{
    const int A1_SIZE = 5,  A2_ROWSIZE =5, A2_COLSIZE =10;
    string Artist[A1_SIZE]={ "Degas", "Holbien", "Monet", "Matisse", "Valesquez" };
    int Scores[A2_ROWSIZE][A2_COLSIZE] = {{5,5,6,8,4,6,8,8,8,10},{8,8,8,8,8,8,8,8,8,8},
    {10,10,10,10,10,10,10,10,10,10},{5,0,0,0,0,0,0,0,0,0},{5,6,8,10,4,0,0,0,0,0}};

    cout << "\n\n\t-------------------------------------------" << endl;
    cout << "\t\tAverage Scores by Artist"<< endl;
    cout << "\t===========================================" << endl;

    int total = 0;
    float faverage;


    for (int x=0; x<5; x++)
    {
        cout << "\n\t" << Artist[x] << "\t\t";

        for (int col = 0; col < 10; col++)
        {
            total+=Scores[x][col];
        }
        faverage = (float)total / 10.0f;

        average = total = 0;
        cout << "\tAverage ... " << faverage << endl;
    }
    return 0;
}

应该这样做。

注意:你的“int average”在这里完全没用,我会完全消除它。

编辑:你的索引完全搞砸了......

Edit2:将std :: map添加到等式中

std::map <string, float> artistList;
artistList.insert(Artist[x], fAverage);

然后相应地对列表进行排序(参见HowTo sort std::map?

答案 3 :(得分:1)

看看这一行:

average = faverage;

这覆盖了以前平均计算的内容。更糟糕的是,faverage甚至没有初始化!

答案 4 :(得分:0)

首先,你只有行和列,为什么有三个嵌套循环呢?毫无意义。

用变量替换所有常量大小。

不要按整数划分!

实际打印平均内容,faverage为零因为它从未设置过任何东西!!

答案 5 :(得分:0)

麻烦写了

const int A1_SIZE = 5,  A2_ROWSIZE =5, A2_COLSIZE =10;

你为什么不把它带过去写这个?

for (int x=0; x<A1_SIZE; x++)

for (int row = 0; row < A2_ROWSIZE; row++)

for (int col = 0; col < A2_COLSIZE; col++)

会修复至少一个错误。

相关问题