我必须编写一个程序,必须使用文本文件打印出2d数组,找到每行和每列的平均值和总和,然后将它们全部打印在表格中。
我在我的代码中使用了Serge的响应,并且我已经为程序添加了一个calcTotal函数,但是我不确定如何为每个列和每一行打印它;目前它只打印一行。
我还必须通过在getData,calcTotal和getAverage函数中创建指针来包含printTable函数,但我不确定如何这样做(我对函数缺乏经验)。
我也搞乱了expense.txt文件;我编辑了文件,以便测试程序;该文件有字符串和浮点数,但我不确定如何使用两者打印出数组。
这就是我用过的编辑后的expense.txt文件的样子;这是我读过的当前程序:
434.92 233.76 322.25 1442.98
610.55 233.21 144.75 1232.20
343.21 224.76 128.90 987.00
278.23 98.43 177.34 899.32
522.32 109.78 233.45 1232.45
132.98 221.43 119.56 1090.30
109.56 342.87 298 1154
这是我应该使用的原始费用.txt:
Department-Name Electric Copier Phone Miscellaneous
Bookkeeping 434.92 233.76 322.25 1442.98
Sales 610.55 233.21 144.75 1232.20
Service 343.21 224.76 128.90 987.00
Customer-Relations 278.23 98.43 177.34 899.32
Marketing 522.32 109.78 233.45 1232.45
Media 132.98 221.43 119.56 1090.30
Human-Resources 109.56 342.87 298 1154
这是编辑过的代码:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
const int ROWSPACE = 7;
const int COLUMNSPACE = 4;
float rowAverage[COLUMNSPACE] = {0};
float colAverage[ROWSPACE] = {0};
float getAverage(float averageArray[][COLUMNSPACE], int size, float rowAverage[], float colAverage[]);
float calcTotal(float sumArray[][COLUMNSPACE], int sz, float rowAverage[], float colAverage[]);
void getData(float expense[][COLUMNSPACE], int ROWSPACE);
int _tmain(int argc, _TCHAR* argv[])
{
float average;
float total;
float expenseArray[ROWSPACE][COLUMNSPACE];
getData(expenseArray,ROWSPACE);
average=getAverage(expenseArray,ROWSPACE,rowAverage, colAverage);
cout<<"The average of the expenses is: " << average <<endl<<endl;
total= calcTotal(expenseArray, ROWSPACE, rowAverage, colAverage);
cout<<"The total of the expenses is: " << total <<endl<<endl;
system("pause");
return 0;
}
float getAverage(float averageArray[][COLUMNSPACE], int size, float rowAverage[], float colAverage[])
{
int i,j;
float sum = 0, average;
for(i = 0; i<size; i++)
{
for(j=0; j<COLUMNSPACE; j++)
{
sum+=averageArray[i][j];
rowAverage[i] += averageArray[i][j];
colAverage[j] += averageArray[i][j];
}
rowAverage[i] /= COLUMNSPACE;
}
for(j=0; j<COLUMNSPACE; j++) {
colAverage[j] /= size;
}
average=sum/(size * COLUMNSPACE);
return average;
}
float calcTotal(float sumArray[][COLUMNSPACE], int sz, float rowAverage[], float colAverage[])
{
int i,j;
float sum = 0, total;
for(i = 0; i<sz; i++)
{
for(j=0; j<COLUMNSPACE; j++)
{
sum+=sumArray[i][j];
rowAverage[i] += sumArray[i][j];
colAverage[j] += sumArray[i][j];
}
total=sum;
return total;
}
}
void getData(float expense[][COLUMNSPACE], int ROWSPACE)
{
ifstream expenseFile;
ofstream outFile;
int i, j;
expenseFile.open("Expense.txt");
outFile.open("newFile.txt");
outFile<<"The expenses are: \n";
for(i = 0; i<ROWSPACE; i++)
{
for(j = 0; j<COLUMNSPACE; j++)
{
expenseFile>>expense[i][j];
cout<<expense[i][j]<<"\t";
outFile<<expense[i][j]<<"\t";
}
cout << endl;
outFile << endl;
}
}
答案 0 :(得分:0)
让我们从:
开始for(i = 0; i<ROWSPACE; i++)
{
for(j = 0; j<COLUMNSPACE; j++)
{
expenseFile>>expense[i][j];
cout<<expense[i][j]<<'\t';
// ^^^^
outFile<<expense[i][j]<<"\t";
}
// now newline
cout << '\n';
outfile << '\n';
}
然后您可以使用std::vector
而不是数组。
计算平均值时,您将对所有数据求和,然后仅除以行数。您应该将总和限制为行或列。
答案 1 :(得分:0)
至少你的程序编译时没有警告,但......它不符合你的要求!
恕我直言,你的第一个问题是你正在处理一个包含7行4列的表,并且你声明了一个表expenseArray[4][7]
,通常是4行7列...很容易修复,因为你已经很好地声明了常数,只需写:
const int ROWSPACE = 7;
const int COLUMNSPACE = 4;
但是你还需要每行和每列的平均值。只需声明
float rowAverage[COLUMNSPACE] = {0}; // initializes the full array to 0...
float colAverage[ROWSPACE] = {0};
并将它们传递给getAverage:
float getAverage(float averageArray[][COLUMNSPACE], int size,
float rowAverage[], float colAverage[])
{
int i,j;
float sum = 0, average;
for(i = 0; i<size; i++)
{
for(j=0; j<COLUMNSPACE; j++)
{
sum+=averageArray[i][j]; // sum for global average
rowAverage[i] += averageArray[i][j]; // sum for row averages (one value per row)
colAverage[j] += averageArray[i][j]; //sum for column averages
}
rowAverage[i] /= COLUMNSPACE; // compute row averages
}
for(j=0; j<COLUMNSPACE; j++) {
colAverage[j] /= size; // compute column averages
}
average=sum/(size * COLUMNSPACE); // total number of values is size * COLUMNSPACE
return average;
}
您现在拥有所有值,并且可以打印平均值
顺便说一句,如果您想在阅读时将值打印为表格(即使恕我直言,最后打印更好),只需更改cout
输出:
for(i = 0; i<ROWSPACE; i++)
{
for(j = 0; j<COLUMNSPACE; j++)
{
expenseFile>>expense[i][j];
cout<<expense[i][j]<<"\t"; // one tab after each column
outFile<<expense[i][j]<<"\t";
}
cout << endl; // one line per row
outFile << endl;
}
答案 2 :(得分:0)
我终于让我的程序正常运行了。我没有将平均值和总数打印到数组上,而且我没有在数组中使用字符串,但这没关系。我很高兴我让这个程序正常运作。
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
const int ROWSPACE = 7;
const int COLUMNSPACE = 4;
float rowAverage[ROWSPACE] = {0};
float colAverage[COLUMNSPACE] = {0};
float rowTotal[ROWSPACE] = {0};
float colTotal[COLUMNSPACE] = {0};
float getAverage(float averageArray[][COLUMNSPACE], int size, float rowAverage[], float colAverage[]);
float calcTotal(float sumArray[][COLUMNSPACE], int sz, float rowTotal[], float colTotal[]);
void getData(float expense[][COLUMNSPACE], int ROWSPACE);
void printTable();
int _tmain(int argc, _TCHAR* argv[])
{
printTable();//Prints the data.
system("pause");
return 0;
}
float getAverage(float averageArray[][COLUMNSPACE], int size, float rowAverage[], float colAverage[])//Finds the sums of the rows and columns.
{
int i,j;
float sum = 0, average;
cout<<"These are the row averages: \n";
for(i = 0; i<size; i++)
{
for(j=0; j<COLUMNSPACE; j++)
{
sum+=averageArray[i][j];//Finds the overall average
rowAverage[i] += averageArray[i][j]; //Finds each row's average
colAverage[j] += averageArray[i][j]; //Finds each column's average.
}
rowAverage[i] /= COLUMNSPACE;
cout<<rowAverage[i]<<"\t"; //prints the row averages
}
cout<<endl;
cout<<"These are the column averages: \n";
for(j=0; j<COLUMNSPACE; j++)
{
colAverage[j] /= size;
cout<<colAverage[j]<<"\t"; //prints the column averages
}
average=sum/(size * COLUMNSPACE);
return average;
}
float calcTotal(float sumArray[][COLUMNSPACE], int sz, float rowTotal[], float colTotal[])
{
int i,j;
float sum = 0, total;
cout<<"These are the row totals: \n";
for(i = 0; i<sz; i++)
{
for(j=0; j<COLUMNSPACE; j++)
{
sum+=sumArray[i][j]; //Finds the overall total
rowTotal[i] += sumArray[i][j]; //Finds the row totals
colTotal[j] += sumArray[i][j]; //Finds the column totals
}
cout<<rowTotal[i]<<"\t"; //prints out row totals
}
cout<<"\nThese are the column totals: \n";
for(j=0; j<COLUMNSPACE; j++) {
cout<<colTotal[j]<<"\t"; //Prints out column totals
}
total=sum;
return total;
}
void getData(float expense[][COLUMNSPACE], int ROWSPACE)
{
ifstream expenseFile;
ofstream outFile;
int i, j;
expenseFile.open("Expense1.txt"); //reads in the file
outFile.open("newFile.txt"); //creates thew new file
outFile<<"The expenses are: \n";
for(i = 0; i<ROWSPACE; i++) //creates the array from the file
{
for(j = 0; j<COLUMNSPACE; j++)
{
expenseFile>>expense[i][j];
cout<<expense[i][j]<<"\t";
outFile<<expense[i][j]<<"\t";
}
cout << endl; //closes the expense file
outFile << endl; //closes the new file
}
}
void printTable() //prints out the data
{
float average;
float total;
float expenseArray[ROWSPACE][COLUMNSPACE];
getData(expenseArray,ROWSPACE);
cout<<endl;
average=getAverage(expenseArray,ROWSPACE,rowAverage, colAverage);
cout<<endl;
total= calcTotal(expenseArray, ROWSPACE, rowTotal, colTotal);
cout<<endl;
}