Here's the full assignment:
The ABC Corporation wishes to review a group of its products based on their performance in the prior fiscal year. The company has the sales data for these products for each quarter in the year. Having heard of your programming experience, the company has hired you to write a complete C program, including comments to do the following:
1. Read in a parameter value n which will indicate how many items should be analyzed. This will be followed by n groups of data each containing information about 1 product. Each group of information will contain the name of the item and then 4 numbers indicating the profit (or loss) made on that item during the 4 quarterly reporting periods. For example, one group of data could be:
Diskettes 32 63 -12 84 or Printers -23 564 45 752. Print the data as it is read in, in the form of a table with one row for each item and one column for each reporting period.
3. Determine the total profit for each item throughout the year. Also determine the total profit for the company and the average profit for each item (total profit/4). Print these values with appropriate messages.
4. For each of the 4 quarters determine which item had the greatest profit for that quarter. Print out the quarter number, item with the most profit and the amount of profit for that item.
5. For each quarter determine how many items had a profit in that quarter and how many items had a loss in that quarter and print those results as well. For each quarter the sum of these two numbers should equal the number of items processed.
6. For each item determine if the profit for that item steadily increased, steadily decreased, or went up and down during the course of the year. Print these results as well.
Data: For this assignment you may use interactive data entry or an external file (easier). Make up your own data and use approximately 10 to 15 items some always profitable some partially and some that lost money. Please cover all possibilities.
Style: Each part of the program should be written using one or more functions. You should decide what parameters to send to these functions.
#include <iostream>
#include <iomanip> //Formatting
#include <fstream> //File operations
using namespace std;
class products {
public:
string item;
int sales[4];
double total;
double avg;
};
void printData (products instance);
void GreatestProfits (products [], int);
void ProfitLoss (products [], int);
int main()
{
int n;
int abctotal = 0;
ifstream datafile;
datafile.open("AnnualReport.txt"); //Open input file
datafile >> n;
products records[n];
for (int i=0; i<n; i++){
datafile >> records[i].item; //Read in product name
datafile >> records[i].sales[0]; //Read in said product sales for each quarter...
datafile >> records[i].sales[1];
datafile >> records[i].sales[2];
datafile >> records[i].sales[3];
//Calculate product's total annual profit:
records[i].total = records[i].sales[0] + records[i].sales[1] + records[i].sales[2] + records[i].sales[3];
//Calculate product's average quarterly profit:
records[i].avg = records[i].total/4;
}
cout << left
<< setw(14) << "ITEM"
<< setw(6) << "Q1" << setw(6) << "Q2" << setw(6) << "Q3" << setw(6) << "Q4"
<< setw(7) << "GROSS" << setw(7) << "AVG" << endl
<< "--------------------------------------------------" << endl;
for (int i=0; i<n; i++)
printData (records[i]);
for (int i=0; i<n; i++)
abctotal = abctotal + records[i].total;
cout << "\n" << "ABC Corp. total annual profits: " << abctotal << endl;
cout << "\n" << "Greatest profit per quarter:" << endl;
GreatestProfits (records, n);
cout << endl;
ProfitLoss (records, n);
datafile.close();
return 0;
}
void printData (products instance){
cout << setw(14) << instance.item
<< setw(6) << instance.sales[0]
<< setw(6) << instance.sales[1]
<< setw(6) << instance.sales[2]
<< setw(6) << instance.sales[3]
<< setw(7) << setprecision(0) << fixed << instance.total
<< setw(7) << setprecision(1) << fixed << instance.avg
<< endl;
return;
}
void GreatestProfits (products array[], int id){
string GreatestProfitProd[4]; //Holds name of product with greatest profit for the quarter
int ProfitForProd[4]; //Holds profit for said product
for (int q=0; q<4; q++){
//Initialize values for quarter:
GreatestProfitProd[q] = "";
ProfitForProd[q] = array[0].sales[q];
for (int r=0; r<id; r++) //Iterate through product sales for quarter
if (array[r].sales[q] > ProfitForProd[q]){
ProfitForProd[q] = array[r].sales[q];
GreatestProfitProd[q] = array[r].item;
}
cout << " " << "Q" << q+1 << "\t" << GreatestProfitProd[q] << "\t" << ProfitForProd[q] << endl;
}
return;
}
void ProfitLoss (products array[], int id){
int Profits[4]; int Losses[4];
cout << setw(5) << "" << setw(9) << "Profits" << setw(7) << "Losses" << endl;
cout << " ---------------" << endl;
for (int q=0; q<4; q++){
Profits[q]=0; Losses[q]=0;
for (int r=0; r<id; r++){
if (array[r].sales[q] > 0)
Profits[q]++;
else if (array[r].sales[q] < 0)
Losses[q]++;
} //end products loop
cout << "Q" << q+1 << ":\t" << Profits[q] << "\t" << Losses[q] << endl;
} //end quarters loop
return;
}
void ProfitsOverTime (products array[], int id){
string Inc = "Steadily INCREASED";
string Dec = "Steadily DECREASED";
string Fluc = "FLUCTUATED";
cout << "Function of Profits Over Time:" << endl;
for (int r=0; r<id; r++){
cout << array[r].item << ":\t";
for (int q=1; q<4; q++){
if (array[r].sales[q] > array[r].sales[q-1]){
while (array[r].sales[q] > array[r].sales[q-1]){
continue;
}
}
}
}
}
And here's my output
ITEM Q1 Q2 Q3 Q4 GROSS AVG
--------------------------------------------------
Diskettes 32 63 -12 84 167 41.8
Printers -23 564 45 75 661 165.2
Keyboards 65 34 33 49 181 45.2
Motherboards 40 44 52 56 192 48.0
Projectors 48 31 27 24 130 32.5
Soundcards 58 63 82 71 274 68.5
Speakers 66 50 39 42 197 49.2
Headsets -13 29 17 20 53 13.2
Enclosures 20 -8 5 11 28 7.0
Monitors 90 29 82 72 273 68.2
Calculators 14 28 17 32 91 22.8
ABC Corp. total annual profits: 2247
Greatest profit per quarter:
Q1 Monitors 90
Q2 Printers 564
Q3 Soundcards 82
Q4 84
Profits Losses
---------------
Q1: 9 2
Q2: 10 1
Q3: 10 1
Q4: 11 0
You see my problem? I'm pretty much all lost/braindead on step 6.
Then for step 4, for some unknown reason the item name doesn't print... but that is the correct greatest profit value in correlation with the 4th quarter..... soooo any ideas? Thanks a ton in advance.