在2D数组中查找平均值:FIXED

Find Average in 2D array: FIXED

本文关键字:平均值 FIXED 查找 2D 数组      更新时间:2023-10-16

下面是我上一个程序的后续内容。它不会为平均值打印正确的信息。请帮忙。

打印

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