0

我正在尝试查找每列的平均值(即 {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;
    }
}
4

6 回答 6

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;
}
于 2013-09-16T20:43:13.947 回答
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;
}
于 2013-09-16T20:51:52.963 回答
1

看看这一行:

average = faverage;

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

于 2013-09-16T20:32:41.633 回答
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?

于 2013-09-16T20:30:18.990 回答
0

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

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

不要除以整数!

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

于 2013-09-16T20:35:18.980 回答
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++)

至少会修复一个错误。

于 2013-09-16T20:37:40.227 回答