以下是我之前计划的后续行动。它不会打印平均值的正确信息。请帮忙。
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
答案 0 :(得分:0)
average
应该通过将总数除以条目数来计算。您有total
中的总数和columns
中的条目数(假设您想要列平均值)。所以你只需要在列(j)循环结束时使用它。
average = total / columns;
并打印出来。或者,更简单地说,您可以打印出total / columns
。