C++ 在二维数组中查找平均值:已修复
这是我之前节目的后续节目。它无法打印正确的平均信息。请帮忙 印刷品 我的代码:我试图在打印数组中添加平均总数。在必填字段中似乎返回了一些未知信息,我似乎不知道为什么。我希望有人能尽快帮助我C++ 在二维数组中查找平均值:已修复,c++,arrays,2d,average,C++,Arrays,2d,Average,这是我之前节目的后续节目。它无法打印正确的平均信息。请帮忙 印刷品 我的代码:我试图在打印数组中添加平均总数。在必填字段中似乎返回了一些未知信息,我似乎不知道为什么。我希望有人能尽快帮助我 #include <iostream> #include "motionPicture.h" #include <iomanip> //to use setw #include <cstring> #include <string> using namespa
#include <iostream>
#include "motionPicture.h"
#include <iomanip> //to use setw
#include <cstring>
#include <string>
using namespace std;
int motionPicture :: count = 0;
motionPicture :: motionPicture() {
//initalize the table
for (int i =0; i < rows; i++){
for (int j=0; j<columns; j++){
table[i][j] =0;
}//end j for loop
}//end i for loop
}//end constructor
平均值应通过将总数除以条目数来计算。假设您想要一个列平均值,那么您有总计和列中的条目数。所以你只需要把它放在j列循环的末尾
average = total / columns;
然后打印出来。或者,更简单地说,您可以打印出总计/列。太多不相关的代码,无法深入研究。那么我应该只显示我的打印数组吗?请参阅“帮助中心”中有关生成数组的内容。不应包含注释掉的代码,也不应包含花哨的格式,除非是关于格式的问题,或者欢迎使用我们的存储系统获取您的帐户信息。如果输入不是您的问题,那么最好将数据硬编码到程序中。无论最小的程序是什么,它演示了您想要询问的问题的方面,这就是代码示例应该是什么。确保您的示例*编译并演示了问题*如果我理解正确,这就是你要找的?只是我的打印数组?@AlyssaCooke好吧,你的程序确实需要编译。你已经编辑并缩小了它。但那个编辑删除了表的定义…这很重要。另外,您需要多少类别?这里有4家公司,你能用两个来演示一个问题吗?一它们可以被称为A和B,而不将问题与额外的域数据混淆吗?是否需要破折号?当你将问题缩减到基本要素时,问题就会变得更容易发现……非常精确地解释你期望的与你得到的……以及可以运行以演示问题的最小代码量。我希望所有列的平均值相加。我试过这个代码,但当我有派拉蒙=20,分福克斯=30,总数是50,它给了我平均值:10,我想这是有意义的,因为有5列。剩下的=0,所以从技术上讲这是正确的。我想这是我能做的最好的了,除非这个人只为所有栏目输入信息。谢谢。我想就这么简单。我试图找出一些太复杂的代码。通常情况下,它很简单,但是如果一个零条目并不意味着要包含在平均值中,那么你需要记录有多少条非零条目,然后除以它。在这种情况下,当计数为零时,必须有一个特殊情况,以避免被零除的错误。
int rowNum=0;
rowNum++;
string1[count] = name;
count++;
cout <<"Input the data in the following format."<<endl;
cout <<"Motion Picture Number <space> Cost of movies "<<endl;
cout <<"Enter -1 <space> 0 to end"<<endl<<endl;
cin >> motion >> cost;
while (motion != -1) {
table[rowNum-1][motion-1] +=cost;
cout <<"Enter Motion Picture <space> Cost of movies" <<endl;
cin >> motion >>cost;
}//end cost while
void motionPicture :: printArray() {
//need to pass an int in array element variable
int size =0;
size =sizeof(table)/ sizeof(table[0][0]);
double max = table[0][0];
double min = table[0][0];
cout <<"Listed below is your totals within each Motion Picture Company"<<endl;
cout <<"----------------------------------------------------------------"<<endl;
cout<<"Name" << setw(15)<<'|'<<setw(5)<< "Paramount" <<setw(5)<<'|'<<setw(5)<< "Century Fox" <<setw(5)<<'|'<<setw(5)<<
"Warner"<<setw(5)<<'|'<<setw(5) <<"WaterFront" <<setw(5)<<'|'<<setw(5)<<"Total"<<endl<<endl;
for (int i= 0; i <count; i++){
double total =0;
double average = 0;
cout<<string1[i]<<setw(5)<<'|';
for (int j=0; j<columns; j++) {
//cout <<string1[i]<<"\t";
cout <<setw(10)<<table[i][j]<<'|';
//input total
total += table[i][j];
// double totalSales = total;
// average = i[i][j] + j[i][j]'//totalSales/(i*j);
average= total/table[i][j];
if (table[i][j] > min)
max = table[i][j];
else if (table[0][0] < min)
min = table[i][j];
}//end j loop
cout <<setw(20)<<'|'<<total<<"\n";
cout<<endl;
cout <<"Total: "<<total<<endl;
cout<<"Average: " <<average/table[rowNum][motion]<<endl;
cout<<"Max: " <<max<<endl<<endl;
}//end i loop
}//end print
void motionPicture :: printArray() {
//need to pass an int in array element variable
int size =0;
size =sizeof(table)/ sizeof(table[0][0]);
double max = table[0][0]++;
double min = table[0][0]++;
cout <<"Listed below is your totals within each Motion Picture Company"<<endl;
cout <<"----------------------------------------------------------------"<<endl;
cout<<"Name" << setw(10)<<'|'<<setw(5)<< "Paramount" <<setw(5)<<'|'<<setw(5)<< "Cent Fox" <<setw(6)<<'|'<<setw(5)<<
"Warner"<<setw(8)<<'|'<<setw(5) <<"WaterFront" <<setw(4)<<'|'<<setw(5)<<"Total"<<endl<<endl;
for (int i= 0; i <count; i++){
double total =0;
double average = 0;
cout<<string1[i]<<setw(8)<<'|';
for (int j=0; j<columns; j++) {
//cout <<string1[i]<<"\t";
cout <<setw(7)<<table[i][j]<<setw(7)<<'|'<<setw(7);
//input total
total += table[i][j];
average = total/columns;
if (table[i][j] > min)
max = table[i][j];
else if (table[0][0] < min)
min = table[i][j];
}//end j loop
cout <<setw(5)<<total<<"\n";
cout<<endl;
cout <<"Total: "<<total<<endl;
cout<<"Average: " <<average<<endl;
cout<<"Max: " <<max<<endl<<endl;
}//end i loop
}//end print
Listed below is your totals within each Motion Picture Company
----------------------------------------------------------------
Name |Paramount |Cent Fox |Warner |WaterFront |Total
Alyssa | 22 | 20 | 0 | 0 | 42
Total: 42
Average: 10.5
Max: 22
Wes | 40 | 60 | 0 | 0 | 100
Total: 100
Average: 25
Max: 60
average = total / columns;