Sum of 2D Array -- Help

I am having trouble writing the code to sum the elements in my array. I need to find the sum of the elements in the row of the array, minus the first column, then the sum of the columns again, minus the first.
The information is taken from an input .txt file--
sample input file: the input sales file ("sales.txt) has the following data

12345 1892.00 0.00 494.00 322.00
32214 343.00 892.00 9023.00 0.00
23422 1395.00 1901.00 0.00 0.00
57373 893.00 892.00 8834.00 0.00
35864 2882.00 1221.00 0.00 1223.00
54654 893.00 0.00 392.00 3420.00

Sample input file: The input name file ("empNames.txt") has the following data

12345 John Smith
32214 Gilbert Hope
23422 Mary Arthur
57373 Sam Bedford
35864 Cristal Benard
54654 Jocelyn Tee
I'm just a little stuck, any steps in the right direction would be helpful! Thank you in advance!

Here is my code ::
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;

struct SalesPersonRecord
{
    int SalesPersonID [6];
    double salebyQuarter[5][5];
    double totalSale[5];
    
};
void getData (double salebyQuarter[5][5])
{
    
    ifstream fin;
    fin.open("sales.txt");
    
    
    for (int r=0; r<6; r++)
    {
        for(int c=0; c<=4; c++)
        {
            fin >> salebyQuarter[r][c];
        }
    }
    
    fin.close();
}

double totalSalesbyPerson (double salebyQuarter[5][5])
{
    int r;
    double totalSale[5];
    for (int r=0; r<5; r++)
    {
        for(int c=1; c<=4; c++)
        {
            totalSale[r] += salebyQuarter[r][c];
        }
    }
    return totalSale[r];
}
double totalSalesbyQuarter (double salebyQuarter[5][5])
{
    int r;
    double totalQuarterSale[5];
    for (int r=0; r<5; r++)
    {
        for(int c=1; c<=4; c++)
        {
            totalQuarterSale[r] += salebyQuarter[r][c];
        }
    }
    return totalQuarterSale[r];
}

void PrintReport(double salebyQuarter[5][5], double totalSale[5], double totalQuarterSale[5])
{
    cout << "\t\t\t\t\t\t\t Night Ya Night Prime Coffee"<< endl;
    cout << "\t\t\t\t\t\t -----Annual Sales Report - 2013 -----"<< endl;
    cout << "Id \t\t\t\t    QT1 \t\t\tQT2\t\t\t\tQT3\t\t\t\tQT4\t\t\t\t Total"<< endl;
    cout << "____________________________________________________________________________________________" << endl;
    
    
    for (int r=0; r<=5; r++)
    {
        for(int c=0; c<=4; c++)
        {
           
            cout << setprecision(2)<< fixed<< salebyQuarter[r][c] << "\t\t\t";
        }
        
        cout << totalSale[r] <<  endl;

    }
    cout <<  endl;

    
    
    
    
}

int main()
{
   // int SalesPersonID[6];
    double salebyQuarter[5][5];
   // double totalSale;
    double totalSale[5];
    double totalQuarterSale[5];
    
   
   
    
     getData(salebyQuarter);
    
    PrintReport(salebyQuarter, totalSale, totalQuarterSale);
    
   return 0;
}

Last edited on
Just wondering, what role is the file "empNames.txt" to play in this program?
Leaving that aside for now, looking at the two files, it seems apparent that the first number on each line is an integer value, the salesperson id. Since you have defined a struct, SalesPersonRecord, should that be used too?

I would have guessed - and this is a guess, I'm just figuring it out from looking at the data, the struct might better be defined like this:
1
2
3
4
5
6
7
struct SalesPersonRecord
{
    int    SalesPersonID;
    double salebyQuarter[4];
    double totalSale;
    string SalesPersonName;
};


Then the program might start out like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;

struct SalesPersonRecord
{
    int    SalesPersonID;
    double salebyQuarter[4];
    double totalSale;
    string SalesPersonName;
};

const int MAXROWS = 6;

void getData (SalesPersonRecord salesrec[])
{
    ifstream fin("sales.txt");

    for (int row=0; row<MAXROWS; row++)
    {
        fin >> salesrec[row].SalesPersonID;

        for (int col=0; col<4; col++)
        {
            fin >> salesrec[row].salebyQuarter[col];
        }
    }

    if (!fin)
    {
        cout << "file error in getData" << endl;
    }
}

int main()
{
    SalesPersonRecord salesrec[MAXROWS];

    getData(salesrec);

    return 0;
}

Of course this change in data structure affects the rest of the processing, hopefully to make it clearer. Rather than just an anonymous 2D array of values, some of which are apples and some are oranges, now there is an array where it should be more clear what each element represents.
Last edited on
Topic archived. No new replies allowed.