在2D数组中查找平均值:FIXED
Find Average in 2D array: FIXED
下面是我上一个程序的后续内容。它不会为平均值打印正确的信息。请帮忙。
打印
Listed below is your totals within each Motion Picture Company
----------------------------------------------------------------
Name |Paramount |Century Fox |Warner |WaterFront |Total
Alyssa | 20| 30| 0| 40| 0| |90
Total: 90
Average: inf
Max: 40
我的代码:我正在尝试将平均总数添加到我的打印数组中。在必填字段中似乎返回了一些未知信息,我似乎不明白为什么。我希望有人能尽快帮助我!
#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
我的getData()函数的部分输入:
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
中有条目数(假设您想要列平均值)。所以您只需要在column(j)循环的末尾有这个。
average = total / columns;
然后打印出来。或者,更简单地说,您可以打印出total / columns
。
相关文章:
- 计算数组c++的平均值
- 计算平均值,不包括上次得分
- 彩色图像的卤化物处理平均值
- 如何创建一个函数来计算并返回平均值、最大值和最小值
- 按平均值替换数组中的元素
- 如何返回多个矢量对象的平均值?C++
- 将随机生成的数字添加到数组 + 对这些数组求平均值
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- 制作具有平均值的随机数生成器
- C++ 函数,用于查找传入的 N 个数字的平均值、总和、最小值和最大值
- 如何使用对象制作分数列表并获得平均值
- 浮点格式数组的平均值
- 在二叉搜索树C++中计算平均值
- C 复合赋值运算符 ^= 平均值
- 我们如何并行运行算法的 n 个实例并以有效的方式计算结果函数的平均值?
- 我可以计算多线程数的平均值吗?
- 计算另一个图像像素满足条件的像素值的平均值
- 做一个unordered_multimap键的平均值是更好的方法吗?
- 使用数学地板找到平均值
- 在2D数组中查找平均值:FIXED