Hello CodeNovice01,
You have made some great progress with your code. There are still some changes that would help.
againtry wrote: |
---|
use simple statements with sensible self-documenting variable names. |
The "
self-documenting variable names. can not be stressed enough. Understanding your problems better now this would help you to be less confusing when writing code. I will keep changing variable names until you grasp the concept. The repetition should help.
In your "print" function you have the line:
<< setw(20) << bonus(ary, r)
. In order to print this return value of the function you need to include the header file "<string>" for this to work. What you do not see is the return value of the function is stored in a tempary variable made by the compiler which the "out" or "cout" uses to print the value. Without the "<string>" header file "out" or "cout" does not know what to do with this variable. So I put
#include <string>
after <iomanip>.
A personal note: I found that putting the include files in alphabetical order helps to remind you if you have missed something. My exception here is:
1 2
|
#include <iostream>
#include <iomanip>
|
Since there is no point for including "iomanip" without "iostream" it seemed the most logical order. technically speaking the order of the include files should make no difference. On a rare occasion it does.
Your "read function could use a couple of changes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
int read(float ary[][COL])
{
int row = 0;
for (int id; inf >> id; row++)
{
if (row == MAXROWS)
{
cout << "Table Overflowed\n";
break;
}
ary[row][0] = id;
for (int c = 1; c < COL; c++)
{
inf >> ary[row][c];
}
}
// <--- Added blank line.
return row;
}
|
The if statement is the biggest change. You have copied from what
dutch posted early on, but you did not get all of it.
Without the {}s and "break" statement if the if statement ever becomes true you will print the error message, but continue on with the for loops and that is not what you want.
Your "print" function should start more like this for now:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
void print(float ary[][COL], const int ROWS)
{
out << fixed;
header(); // <--- Moved here.
for (int row = 0; row < ROWS; row++)
{
out << fixed << setprecision(0) << setw(10) << ary[row][0]
<< setprecision(2) << setw(20) << ary[row][13]
<< setw(20) << sum(ary, row)
<< setw(25) << AverageMinusLowest(ary, row)
<< setw(20) << bonus(ary, row)
<< setprecision(0) << setw(20) << worst(ary, row) // <--- setw value may need to be reduced.
<< endl;
}
}
|
This should work better for you as a start. There will be more that is needed in this function.
The "average" function is OK for the most part, but could be reduced to this:
1 2 3 4 5 6 7 8 9 10 11 12
|
float Average(float ary[][COL], const int ROW)
{
const float YEAR = 12.0;
float sum = 0;
for (int col = 1; col < COL - 1; col++)
{
sum += ary[ROW][col];
}
return sum / YEAR;
}
|
By putting the line
avg= sum/year;
This may work, but it is not the best way to do it. I have seen code done your way produce the wrong answer.
After thinking about it the function could be shortened to just this:
1 2 3 4 5 6
|
float Average(float ary[][COL], const int ROW)
{
const float YEAR = 12.0;
return (sum(ary, ROW) / YEAR);
}
|
You already have the function that sums the row you do not need to duplicate code that you already have just use it.
First the function sum is called then divided by YEAR before the answer is returned.
It is always a good idea to use what you have rather then duplicating code. The same concept can be used in other functions.
For this function you could do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
float AverageMinusLowest(float ary[][COL], const int ROW)
{
const float YEAR = 11.0;
float small = ary[ROW][1], sum = 0;
for (int col = 1; col < COL - 1; col++)
{
if (ary[ROW][col] < small)
{
small = ary[ROW][col];
}
sum += ary[ROW][col];
}
return ((sum - small) / YEAR);
}
|
Or shortened:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
float AverageMinusLowest(float ary[][COL], const int ROW)
{
const float YEAR = 11.0;
float small = ary[ROW][1], arySum = sum(ary, ROW);
for (int col = 1; col < COL - 1; col++)
{
if (ary[ROW][col] < small)
{
small = ary[ROW][col];
}
}
return ((arySum - small) / YEAR);
}
|
I had to change the variable name "sum" because it was a conflict with the function call "sum".
"bonus" can be done like this:
1 2 3 4 5 6 7
|
string bonus(float ary[][COL], int rows)
{
if (Average(ary, rows) - ary[rows][COL - 1] >= (ary[rows][COL - 1] * .1))
return "YES";
return "NO";
}
|
When the if statement is true "yes" is returned and the rest of the function is skipped. When the if statement is false the only thing left to return is "no". Just because you have an if statement the else statement is not always required.
The worst function I am still trying to figure out, but I do see the way you are using it is wrong. The column for "Worst Salesman" should only have one ID number in it or should be printed as one line after the table. But the instructions allude to it being part of the table.
In the "" function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
float TotalMonthly(float ary[][COL], int rows)
{
float total{};
for (int c = 1; c < COL - 1; c++)
{
for (int r = 0; r < rows; r++)
{
total += ary[r][c];
}
}
return total;
}
|
I think this is a better start although I have not worked with it yet to know if it is correct. What I can see is the you are returning "total" to soon. This should be done after the for loops not inside the outer for loop. And "total" should be defined outside the for loops so you have something to return at the end.
As for "PrintTotalMonthly" I have not worked to that part yet, so I do not know exactly what it is printing or when.
Over all good job. You have made improvements and advancements. Let's hope you can remember them and nor fall backwards.
In the mean time I will keep working on what you have done and let you know what I have come up with.
Andy