简体   繁体   English

C ++读入二维数组问题

[英]C++ read-in 2d Array problems

this is the first time I'm using stock overflow. 这是我第一次使用库存溢出。 I'm using visual studio 2012 which uses C++, so the code's a bit odd, my apologies. 我使用的Visual Studio 2012使用的是C ++,因此我的代码有点奇怪,我很抱歉。 I can really use some help; 我真的可以帮忙; I'm starting to get really anxious and everything I'm doing now is ending up in errors. 我开始变得非常焦虑,现在我所做的一切都以错误告终。

I've got to write a program that has to print out a 2d array using a text file, find the average and the total sum for each row and column, and print it all out in a table. 我必须编写一个程序,该程序必须使用文本文件打印出二维数组,找到每一行和每一列的平均值和总和,然后将其全部打印在表格中。

I've used Serge's response in my code, and I've added a calcTotal function to the program, but I'm unsure how to have it print for each column and each row; 我在代码中使用了Serge的响应,并在程序中添加了calcTotal函数,但是我不确定如何在每一列和每一行打印它。 currently it only prints for one row. 当前它仅打印一行。

I must also include a printTable function through creating pointers in the getData, calcTotal, and getAverage functions, but I'm unsure how to do so (I'm inexperienced with functions). 我还必须通过在getData,calcTotal和getAverage函数中创建指针来包括一个printTable函数,但是我不确定该怎么做(我对函数没有经验)。

I also messed up with the expense.txt file; 我还弄乱了Expense.txt文件; I edited the file so it can test the program; 我编辑了文件以便可以测试程序。 The file has both strings and floats, but I'm unsure how I would print the array out using both. 该文件既有字符串又有浮点数,但是我不确定如何同时使用两者打印数组。

This is what the edited expense.txt file that I used looks like; 这就是我使用的经过编辑的expense.txt文件的样子; It's the one that the current program I have reads: 这是我当前阅读的程序:

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

This is the original expense.txt that I'm supposed to use: 这是我应该使用的原始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

Here's the edited code: 这是编辑后的代码:

#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;
    }
}

Let's start with: 让我们从:

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';
}

Then you can use std::vector s instead of arrays. 然后,您可以使用std::vector而不是数组。

When you calculate the average you are summing all the data and then dividing only by the number of rows. 计算平均值时,您将所有数据求和,然后仅除以行数。 You should limit the sums to the rows or the columns instead. 您应该将总和限制为行或列。

Well at least you program compiles without warnings, but... it does not meet your requirements! 好吧,至少您的程序编译时没有警告,但是...它不符合您的要求!

IMHO your first problem is that you are processing an table of 7 rows of 4 columns and that you declared a table expenseArray[4][7] which normally is 4 rows of 7 columns... Easy to fix as you have nicely declared constants, just write: 恕我直言,您的第一个问题是您要处理一个7行4列的表,并且声明了一个表expenseArray[4][7] ,该表通常是4行7列的表...易于expenseArray[4][7] ,因为您已经很好地声明了常量, 写吧:

const int ROWSPACE = 7;
const int COLUMNSPACE = 4;

But you also need averages per row and column. 但是,您还需要每行和每列的平均值。 Just declare 只是声明

float rowAverage[COLUMNSPACE] = {0}; // initializes the full array to 0...
float colAverage[ROWSPACE] = {0};

and pass them to getAverage: 并将它们传递给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;
}

You now have all your values and you can print your averages 现在,您拥有了所有值,可以打印平均值

BTW, if you want to print the values as a table when reading (even if IMHO it would nicer to print at the end) just change your cout outputs: 顺便说一句,如果您想在读取时将值打印为表格(即使恕我直言,则最好在末尾打印),只需更改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;
}

I finally got my program working. 我终于让我的程序开始工作了。 I don't have the averages and the totals printed onto the array, and I don't have the strings in the array, but that's ok. 我没有将平均值和总数打印到数组上,并且数组中也没有字符串,但这没关系。 I'm just glad I got the program working. 我很高兴我能执行该程序。

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM