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 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 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.

Recommended Answers

All 3 Replies

I'm pretty much all lost/braindead on step 6.

It's a variation of the algorithm for checking if an array is sorted. If it's not sorted in ascending or descending order, the profits fluctuated, which is pretty easy to check:

#include <iostream>
#include <string>

using namespace std;

string ProfitTrend(int a[], int size)
{
    // Check for an increasing trend
    for (int i = 0; i < size - 1; i++)
    {
        if (a[i] > a[i + 1])
        {
            break;
        }
        else if (i + 1 == size - 1)
        {
            return "Steadily INCREASED";
        }
    }
    
    // Check for a decreasing trend
    for (int i = 0; i < size - 1; i++)
    {
        if (a[i] < a[i + 1])
        {
            break;
        }
        else if (i + 1 == size - 1)
        {
            return "Steadily DECREASED";
        }
    }
    
    return "FLUCTUATED";
}

int main()
{
    int a1[] = {1, 2, 3, 4, 5}; // Steadily increasing
    int a2[] = {5, 4, 3, 2, 1}; // Steadily decreasing
    int a3[] = {1, 2, 5, 4, 2}; // Fluctuating unpredictably
    
    cout << "a1 trend: " << ProfitTrend(a1, sizeof a1 / sizeof *a1) << endl;
    cout << "a2 trend: " << ProfitTrend(a2, sizeof a2 / sizeof *a2) << endl;
    cout << "a3 trend: " << ProfitTrend(a3, sizeof a3 / sizeof *a3) << endl;
}

That's quick and dirty code, by the way. If you spend some time on it you can make it shorter and faster. But the basic idea is to check for a sorted array. You might also add a check for no change in profits by checking for a variance between two profits. For example:

// Check for a steady trend
for (int i = 0; i < size - 1; i++)
{
    // 5 is an arbitrary variance, but two profits within
    // the range of 5 will be compared as equal
    if (abs(a[i] - a[i + 1]) > 5)
    {
        break;
    }
    else if (i + 1 == size - 1)
    {
        return "Remained Steady";
    }
}

Then for step 4, for some unknown reason the item name doesn't print...

I'd guess from a quick glance that the first product has the greatest profits. Note that you initialize ProfitForProd[q] to the first item's sales, but GreatestProfitProd[q] is set to an empty string. It should be set to the first item's product name to account for the edge case where array[r].sales[q] > ProfitForProd[q] is never true:

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] = array[0].item;
        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;
}

YES THAT MAKES ALL THE SENSE IN THE WORLD THANK YOU GODMAN I've been getting all too frustrated lately so this means a lot to me I'll update you on whether I get it to work in a few. Thanks again.

Got it THANK YOU

#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);
string ProfitTrend (products instance);

int main()
{
	// Initialize integer values:
	int size=0;
	int abctotal=0;

    ifstream datafile; // Declare input file
    datafile.open("AnnualReport.txt"); // Open input file

    // Check for error opening input file
    if (!datafile.is_open())
    {
    	cout << "datafile was not opened correctly" << endl;
    	cout << "program halted";
    	return 0;
    }

    // Count number of records in external file:

    string line;
    line.clear();

	  getline(datafile,line);

	while (datafile)
	{
		size++;
		getline(datafile,line);
	} // Determines number of records

    if(datafile.eof()) // upon reaching end of file
    {
       datafile.clear(); // clear internal flags of the stream
       datafile.seekg(0,ios::beg); // go back to the start of the file
    }

    // Now declare records array in class 'products' with size determined above
	products records[size];

	for (int i=0; i<size; 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;
	}

	// Print table headers:
	cout << 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<size; i++)
		printData (records[i]); // Print records

	for (int i=0; i<size; i++)
		abctotal = abctotal + records[i].total; // Calculate company's total annual profits

	cout << endl;

	cout << "ABC Corp. total annual profits: " << abctotal << endl;

	cout << endl;
	cout << endl;

	cout << "GREATEST QUARTERLY PROFITS:" << endl;
	cout << "---------------------------" << endl;
	GreatestProfits (records, size); // Prints greatest profit and corresponding product for each quarter

	cout << endl;
	cout << endl;

	ProfitLoss (records, size); // Prints table of how many items profited and lost profit for each quarter

	cout << endl;
	cout << endl;

	cout << "2011 PROFIT TRENDS:" << endl;
	cout << "--------------------------------" << endl;
	for (int i=0; i<size; i++)
	{
		cout << records[i].item << "\t";
		cout << ProfitTrend(records[i]);
		cout << endl;
	} // Determines and prints profit trend for each product over the year

    datafile.close(); // Close input file
	return 0; // Close main program function
}

void printData (products instance)
{
	cout << left
	<< 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 a[], int n)
{
	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] = a[0].item;
		ProfitForProd[q] = a[0].sales[q];

		// Iterate through product sales for each quarter:
		for (int r=0; r<n; r++)
			if (a[r].sales[q] > ProfitForProd[q])
			{
				ProfitForProd[q] = a[r].sales[q];
				GreatestProfitProd[q] = a[r].item;
			}
		// Print results:
		cout << " " << "Q" << q+1 << "\t"
				<< GreatestProfitProd[q] << "\t" << ProfitForProd[q] << endl;
	}

	return;
}

void ProfitLoss (products a[], int n)
{
	int Profits[4]; int Losses[4]; // Declare arrays to store profit/loss item counts for each quarter

	cout << setw(8) << "" << "QUARTERLY" << endl;
	cout << setw(5) << "" << setw(9) << "Profits" << setw(7) << "Losses" << endl;
	cout << "     ---------------" << endl;

	for (int q=0; q<4; q++) // loop through quarters
	{
		Profits[q]=0; Losses[q]=0; // Initialize values

		for (int r=0; r<n; r++) // loop through products
		{
			if (a[r].sales[q] > 0)
				Profits[q]++;
			else if (a[r].sales[q] < 0)
				Losses[q]++;
		}
		// Print results:
		cout << "Q" << q+1 << ":\t"
				<< Profits[q] << "\t" << Losses[q] << endl;
	}
	return;
}

string ProfitTrend (products instance)
{
	// Check for an increasing trend
	for (int q=0; q<4; q++)
	{
		if (instance.sales[q] > instance.sales[q+1])
			break;
		else if (q == 2) // i.e., has reached the end of the loop (i.e., unbroken)
			return "steady INCREASE";
	}

	// Check for a decreasing trend
	for (int q=0; q<3; q++)
	{
		if (instance.sales[q] < instance.sales[q+1])
			break;
		else if (q == 2)
			return "steady DECREASE";
	}

	//Otherwise
	return "FLUCTUATED";
}
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.