gpt4 book ai didi

C++读入二维数组问题

转载 作者:太空宇宙 更新时间:2023-11-04 13:19:42 25 4
gpt4 key购买 nike

这是我第一次使用库存溢出。我正在使用使用 C++ 的 visual studio 2012,所以代码有点奇怪,我很抱歉。我真的需要一些帮助;我开始变得非常焦虑,我现在所做的一切都以错误告终。

我必须编写一个程序,该程序必须使用文本文件打印出二维数组,找出每行和每列的平均值和总和,然后将其全部打印在表格中。

我在我的代码中使用了 Serge 的响应,并且我在程序中添加了一个 calcTotal 函数,但我不确定如何为每一列和每一行打印它;目前它只打印一行。

我还必须通过在 getData、calcTotal 和 getAverage 函数中创建指针来包含一个 printTable 函数,但我不确定该怎么做(我对函数没有经验)。

我还搞砸了 expense.txt 文件;我编辑了文件,以便它可以测试程序;该文件同时具有字符串和 float ,但我不确定如何使用这两者打印数组。

这是我使用的经过编辑的 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

这是我应该使用的原始 expense.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;
}
}

最佳答案

至少你的程序编译没有警告,但是......它不符合你的要求!

恕我直言,你的第一个问题是你正在处理一个 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;
}

关于C++读入二维数组问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35742156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com