[英]How to Find Column Average from 2D Array in C++
我試圖找到每列的平均值(即{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;
}
}
你可以用這個
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;
}
方法一
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;
}
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;
}
應該做。
注意:您的“平均”在這里完全沒有用,我將其完全消除。
編輯:您的索引完全搞砸了...
Edit2:將std :: map添加到方程式中
std::map <string, float> artistList;
artistList.insert(Artist[x], fAverage);
然后對列表進行相應的排序(請參見如何對std :: map進行排序? )
看一下這一行:
average = faverage;
這覆蓋了先前平均計算的結果。 更糟糕的是, faverage
甚至還沒有初始化!
首先,您只有行和列,所以為什么要有三個嵌套循環? 沒有意義。
用變量替換所有常量大小。
不要除以整數!
實際上打印平均值的內容,平均數為零,因為它從未設置為任何值!
麻煩寫了
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++)
將解決至少一個錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.