First 2-D Array

Pages: 123
Andy,

I have made the corrections that you have suggested. From my perspective the code appears to be good to go. With the exception of the "WorstSalesman" it's printing the same ID for each row, which I am good with that as long as thats the only bug.

Other than that I'm unsure what would need to altered. Here's where I am so far
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;
const int MAX=15;

int read(float[][MAX]);
void print(float[][MAX],int);
void header();
float sum(float[][MAX],int);
float AverageMinusLowest(float[][MAX], int);
float Average(float[][MAX], int);
void Legend();
void TotalMonthly(float[][MAX]);
string BonusEligible(float[][MAX], int);
float WorstSalesman(float[][MAX]);
void MonthlyHeader();

ifstream inf;
ofstream out;

int read(float ary[][MAX])
{
	int r=0;

    for(int id;inf >> id ;r++)
   {
	   if(r == MAX)
		   cout << "Table Overflowed\n";

	   ary[r][0]=id;

        for(int c=1; c<MAX-1; c++)
        {
            inf >> ary[r][c];
        }
    }
    return r;
}

void header()
{
    out << "\t\t\tWILD and WOOLEY Sale's Report\n"
        << "|S-ID|"
        << setw(7) << "|LYA|"
        << setw(11) << "|CMS|"
        << setw(11) << "|CYA|"
        << setw(8) << "|BNS|"
        << setw(9) << "|WSM|\n";
}

void print(float ary[][MAX], int rows)
{
    header();
	out << fixed;

    for(int r=0; r<rows; r++)
    {
		out << setprecision(0) << setw(5) << ary[r][0]
            << setprecision(2) << setw(10) << ary[r][MAX-2]
            << setw(11) << sum(ary,r)
            << setw(10) << AverageMinusLowest(ary,r)
            << setw(6) << BonusEligible(ary,r)
            << setprecision(0) << setw(9) << WorstSalesman(ary) << endl;
    }
}

float sum(float ary[][MAX], int rows)
{   float total=0;

    for(int c=1; c<MAX-2;c++)
    {
        total += ary[rows][c];
    }
    return total;
}

float Average(float ary[][MAX], int rows)
{
    const float MONTHS=12.0;
    return (sum(ary,rows)/MONTHS);
}

float AverageMinusLowest(float ary[][MAX],int rows)
{
    const float MONTHS=11.0;
    float small=ary[rows][1], cnt = sum(ary,rows);

    for(int c=1; c<MAX-2; c++)
         {
             if(ary[rows][c]<small)
             {
                 small=ary[rows][c];
             }
         }
    return ((cnt - small) / MONTHS);
}

string BonusEligible(float ary[][MAX],int rows)
{
    if(Average(ary,rows) - ary[rows][MAX-2] >= (ary[rows][MAX-2] * .1))
        return "YES";
    else
        return "NO";
}

float WorstSalesman(float ary[][MAX])
{
    const float TWO = 2.0;

    float WorstSalesmanSales = ary[0][0],
    WorstSalesmanAverage = (ary[0][MAX-2] + Average(ary,0) /TWO);

    for(int r=0; r<MAX; r++)
        if(((ary[r][MAX-2] + Average(ary,r)) /TWO) < WorstSalesmanAverage)
        {
            WorstSalesmanAverage = (ary[r][MAX-2] + Average(ary,r)) /TWO;
            WorstSalesmanSales = ary[r][0];
        }
    return WorstSalesmanSales;
}

void Legend()
{
    out << "\n\tLEGEND:\n"
        << "SID = Salesmen ID\n"
        << "LYA = Last Year's Average\n"
        << "CMS = Current Monthly Sale's\n"
        << "CYA = Current Yearly Average(Minus Lowest)\n"
        << "BNS = BonusEligible\nWSM = Worst Salesman\n";
}
void MonthlyHeader()
{
    out << endl << setw(64) << " Total Monthly Sales "
        << endl << setw(5) << "Jan" << setw(10) << "Feb" << setw(12) << "March"
        << setw(10) << "April" << setw(9) << "May" << setw(10) << "June"
        << setw(10) << "July" << setw(9) << "Aug" << setw(11) << "Sept"
        << setw(10) << "Oct " << setw(9) << "Nov" << setw(11) << "Dec\n";
}

void TotalMonthly(float ary[][MAX])
{
    Legend();
    MonthlyHeader();

    for(int c=1; c<MAX-2;c++)
    {
        float total=0;

        for(int r=0; r<MAX-2;r++)
        {
            total+=ary[r][c];
        }
    out << setprecision(2) << fixed << total << " ";
    }
   out << endl;
}

int main()
{
    inf.open("Wildin.txt");
    if(!inf)
        cout << "error opening file";

    out.open("Wildout.txt");

    float salesmen[MAX][MAX],total;
	int rows;

    rows = read(salesmen);
    print(salesmen, rows);
    WorstSalesman(salesmen);
    TotalMonthly(salesmen);

   return 0;
}

/*As you can see the Worst prints on each row, which is because of the print 
function, if I take the worst out of that loop, then it prints once, but it isn't 
included into the table. */
|S-ID|  |LYA|      |CMS|      |CYA|   |BNS|   |WSM|
 2021  17353.20  231375.50  20856.84   YES     7538
 1178  26701.30  267013.09  23579.82    NO     7538
 5141  14267.20  171206.14  15220.20    NO     7538
 8347  20758.70  226458.14  20406.27    NO     7538
 6577  14371.70  172460.39  15530.11    NO     7538
 2617  20719.30  248632.06  22314.61    NO     7538
 2891  20793.20  207932.12  18770.16    NO     7538
 7538  12939.40  172525.61  15537.75   YES     7538
 2571  15701.60  157016.02  14150.16    NO     7538
 9872  18020.80  196590.48  17412.92    NO     7538
 5250  19350.80  232209.98  20886.53    NO     7538
 4422  18636.80  248490.58  22235.27   YES     7538
 1864  25219.70  275123.78  23682.34    NO     7538

	LEGEND:
SID = Salesmen ID
LYA = Last Year's Average
CMS = Current Monthly Sale's
CYA = Current Yearly Average(Minus Lowest)
BNS = BonusEligible
WSM = Worst Salesman

                                            Total Monthly Sales 
  Jan       Feb       March     April      May      June      July      Aug       Sept      Oct       Nov       Dec
227175.56 259368.69 240468.30 242611.91 217217.11 236757.36 241315.38 236814.31 243304.70 215930.61 188861.62 257208.44 



Last edited on
Topic archived. No new replies allowed.
Pages: 123