I need help with reading in a text file that has string integers for the first row and for the first column. I need to have everything print out in a table format. The program I have reads in a edited text file that only has the floats, and works fine. The program is meant to read in the text file, find the average and totals for each row and column, and print it all out in a table, but I'm unsure how to go about this. The table is supposed to print like so:
Department-Name Electric Copier Phone Miscellaneous sumOfRows totalOfRows
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
sumOfColumns
totalofColumns
I was told that I could input two string arrays, one for the first row and one for the first column, but I'm unsure how I could print it out in a table format.
Here is the file:
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 is my code:
#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 (I have Expense1 as the floats only, and Expense with the strings and the floats)
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];
cout<<"Electric Copier Phone Miscellaneous\n";
getData(expenseArray,ROWSPACE);
cout<<endl;
average=getAverage(expenseArray,ROWSPACE,rowAverage, colAverage);
cout<<endl;
total= calcTotal(expenseArray, ROWSPACE, rowTotal, colTotal);
cout<<endl;
}
Any suggestions?