Here's my full assignment below, and then the code I have so far to follow. I'm up to Step 4 of the assignment... need some insight to kickstart my flow. Much appreciated.

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 75

2. 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 print (products instance);

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 r=0; r<n; r++)
		print (records[r]);

	for (int r=0; r<n; r++)
		abctotal = abctotal + records[r].total;

	cout << "\n" << "ABC Corp. total annual profits = " << abctotal << endl;

    datafile.close();
	return 0;
}

void print (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;
}

Recommended Answers

All 9 Replies

Iterate through each item's sales for quarter 1. Store item with most sales.
Iterate through each item's sales for quarter 2. Store ...
For each of 4 stored items, print Quarter: Item, amount of sales.

Are you trying to do this using some faster method?

Iterate through each item's sales for quarter 1. Store item with most sales.
Iterate through each item's sales for quarter 2. Store ...
For each of 4 stored items, print Quarter: Item, amount of sales.

Are you trying to do this using some faster method?

No, I realize that much. I can't organize it in my head. I need this spelled out for me, more than that. Thanks though. Couldn't have guessed, right? I need details.

Try something like:

// Create an array, one element per quarter, to hold the index into the records array of
// the product with the highest profit.  This is compared to the profits of the other   
// products.
int GreatestProfitProd[4];

// Create an array, one element per quarter, to hold the profit for the product that has
// the highest profit.
int ProfitForProd[0];

// Loop through the quarters
for  (q = 0; q < 4; q++)
{
// Initialize values to the first product
GreatestProfitProd[q] = 0;
ProfitForProd[q] = records[0].sales[q]; 

	// Loop through the products
	for (ProdIdx = 1; ProdIdx < n; ProdIdx ++) // Start at 1, since we already saved the value for Product 0
	{

		if (records[ProdIdx].sales[q] > records[GreatestProfitProd].sales[q])
{
GreatestProfitProd[q] = ProdIdx;
ProfitForProd[q] = records[ProdIdx].sales[q];
}
} // END Product Index Loop
} // END Quarter Loop

You can use the same loop structures to iterate for the other parts of the assignment. I would strongly recommend using the same variable for an index everywhere the index is used. For example, at one place in your code, you use “i” for the index into the “records” array. In another place, you use “r”. By consistently using the save variable for the index, you decrease the chance that you will use the wrong index for an array. This can easily happen when you use multidimensional arrays, or arrays of arrays.

No, I realize that much. I can't organize it in my head. I need this spelled out for me, more than that. Thanks though. Couldn't have guessed, right? I need details.

That's what pen and paper are for. To organize it so you don't need to keep it in your head. It's called design and desk checking. Your head is a terrible designer/organizer.

fixed code tags.

WaltP - I know. Still wasn't making any headway (no pun intended...)

Fortran IV - TREMENDOUS THANKS I'm going to try that out now, and thanks for the iteration variable tip also.

Okay. Still not clear on this. Sorry for being retarded.

Questions regarding the code given by FortranIV:

Why do I declare ProfitForProd to be an array of 0? Doesn't it have to be 1? It'll contain one value. (Like the sales array in the products class; it's declared as sales[4], which contains sales[0] through sales[3] -- i.e., 4 values.)

Then also, for GreatestProfitProd, how do I make that return the item name? As I'm typing this I think I'm getting a very vague hint of an idea... but still, help me out. Because the thing is I need this whole step 4 to be called in the main function from an external one. Therefore, what would really help me would be if I knew what the parameters for the function should be, and what type of function... for example, I was thinking to use another void function... which would also print the results within it? Or what would be the cleanest/most efficient way of doing this? (So to recap, I need to know specifically (1) what type of function to use, (2) what parameters to use, and (3) how to bring it all together....... I guess...)

Also I'm using Eclipse, and it doesn't seem to be letting me use "ProdIdx" as the iterating variable in the for loop... so I was just using x... ayayay I don't know, thanks so much for all your help though guys I'm sorry I'm just not grasping it...

CRAP ALRIGHT I got it -- EXCEPT for some reason the string value for the last product name doesn't show. Just gotta fix this. Any ideas?

Code and output:

#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);

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);

    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];
	int ProfitForProd[4];

	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;
}

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

In addition to that last question, I'm on the last part now (Step 6), and I'm confused all over again... here's the mess of code that I have drafted so far for the function:

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;
				}
			}
		}
	}

If anyone still has any patience with me, would you please be so kind as to give me some guidance on this last step? (AND MY LITTLE PROBLEM SHOWN ABOVE)


Thank you very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.