首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C++读入2d数组问题

C++读入2d数组问题
EN

Stack Overflow用户
提问于 2016-03-02 08:41:32
回答 3查看 88关注 0票数 1

这是我第一次使用股票溢出。我使用的是visual 2012,它使用C++,所以代码有点奇怪,对不起。我真的需要一些帮助;我开始非常焦虑,我现在所做的一切都是错误的。

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

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

我还必须通过在getData、calcTotal和getAverage函数中创建指针来包含一个getData函数,但我不知道如何做到这一点(我对函数缺乏经验)。

我还搞乱了expense.txt文件;我编辑了该文件,以便它可以测试程序;该文件具有字符串和浮动,但我不确定如何使用这两种方法打印出数组。

这是我使用的编辑后的expense.txt文件的样子;它是我当前程序读取的文件:

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
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

下面是编辑的代码:

代码语言:javascript
运行
复制
#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;
    }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-03-02 09:52:09

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

您的第一个问题是,您正在处理一个由7行4列组成的表,并且您声明了一个表expenseArray[4][7],该表通常是4行7列.很容易修复,因为您已经很好地声明了常量,只需编写:

代码语言:javascript
运行
复制
const int ROWSPACE = 7;
const int COLUMNSPACE = 4;

但是,您还需要每行和每列的平均值。只需声明

代码语言:javascript
运行
复制
float rowAverage[COLUMNSPACE] = {0}; // initializes the full array to 0...
float colAverage[ROWSPACE] = {0};

并将它们传递给getAverage:

代码语言:javascript
运行
复制
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;
}

你现在有了你所有的价值,你可以打印你的平均值。

顺便说一句,如果您想在读取时将值打印为一个表(即使IMHO在末尾打印会更好),只需更改您的cout输出:

代码语言:javascript
运行
复制
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;
}
票数 0
EN

Stack Overflow用户

发布于 2016-03-02 09:36:24

让我们从以下几个方面开始:

代码语言:javascript
运行
复制
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::vectors而不是数组。

当您计算平均值时,您正在将所有数据之和,然后只除以行数。您应该将和限制在行或列上。

票数 0
EN

Stack Overflow用户

发布于 2016-03-03 06:55:09

我终于让我的程序开始工作了。我没有将平均值和总数打印在数组上,也没有数组中的字符串,但没关系。我很高兴我的计划能成功。

代码语言:javascript
运行
复制
#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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35742156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档