Hi,

I've been working on this one problem in C++ for hours, starting last night. I haven't been able to figure it out completely but have been able to complete 3 of the 6 functions required for the program. I'm in the process of completing the fourth and have run into some trouble. The problem is as follows:

Ask the user to enter four quarterly sales figures for six divisions of a company. The figures should be stored in a two dimensional array. Once the figures are entered, the program should display the following data:

  • A list of the sales figures by division (completed)
  • Each division's increase or decrease from the previous quarter (This will not be displayed for the first quarter.)
  • The total sales for the quarter (completed)
  • The company's increase or decrease from the previous quarter (This will not be displayed for the first quarter.)
  • The average sales for all divisions that quarter (completed)
  • The division with the highest sales for that quarter

I've made modifications fo the function that displays Division with the highest sales for the quarter. But no matter what I do, it changes. The way my code is currently, it displays 6 quarters on the output, because it's going through the loop 6 times due to the 6 Divisions. When I tried to drop it to four it seemed like it wouldn't even look past the first Division and the output of the first quarter highest changed for some reason.

As for the other two functions that I haven't completed, I have no idea what to do to get those results. I've tried writing it down on paper and then tried moving it onto the computer but they've only frustrated me until I got a headache. I couldn't figure out what I was doing wrong. I thought that if I summed the columns that would give me the total for the quarters like in the other function. But from there is where I'm lost. I don't know how to take that total, compare it to the previous total and display the difference as an increase or decrease.

I know you all won't do it for me, but can someone at least explain to me what I'm doing wrong on those remaining functions? On the increase/deacrease, could someone explain to me how to get what I'm trying to get? My current code is pasted below. Any help you could give would be greatly appreciated.

//This program will ask the user to enter four quarterly sales figures for six divisions of a company and have those values 
//stored in a two dimensional array. It will then take that data and use functions to output the quarterly sales statistics 
//for the six divisions of that company. 

#include <iostream>
#include <iomanip>

using namespace std; 

const int COLS = 4; //Global Constant initialized to be used as size declarator for the array argument in the functions.

//Function Prototypes
void divQtrSales(double [][COLS], int); 
void quarterlyTotals(double [][COLS], int); 
void average(double [][COLS], int);
void highestQrtlyDiv(double [][COLS], int);

int main()
{
    //Constant int's for the number of divisions and quarters to be used in the two dimensional array.
    const int num_Divisions = 6,
              num_Quarters = 4;
    //Two Dimensional array definition.
    double salesFigures[num_Divisions][num_Quarters];
    //Int's to be used in the data aquisition from the user. 
    int div, qtr, x, y, rows; 
    
    //Tell the user what information the program is looking for. 
    cout<<"Please enter the quarterly sales data for Divisions 1 through 6.\n";
    
    //For loop asks user for input, validates input and saves input into the array if valid. 
    for (div = 0; div < num_Divisions; div++)
        {
            for (qtr = 0; qtr < num_Quarters; qtr++)
            {
                cout<<"\nDivision "<<div+1<<"\n";
                cout<<"Quarter "<<qtr+1<<": ";
                cin>>salesFigures[div][qtr];
            
                //Nested while loop for data validation.
                while (salesFigures[div][qtr] < 0) 
                {
                      cout<<"Please enter a positive number.\n";
                      cout<<"\nDivision "<<div+1<<"\n";
                      cout<<"Quarter "<<qtr+1<<": ";
                      cin>>salesFigures[div][qtr];
                }
            }
        }
        
        //Output of the sales figures by division, called from the function divQtrSales.
        cout<<"\nThe sales figures listed by Division are:\n";
        divQtrSales (salesFigures, num_Divisions);
        
        //Function call for function to display quarterly totals.
        quarterlyTotals (salesFigures, num_Divisions);
        
        cout<< endl;
        
        //Function call for function to display the quarterly average per Division.
        average (salesFigures, num_Divisions);
        
        cout<< endl;
        
        //Function call for the function to display the Division with the highest sales figures for the quarter. 
        highestQrtlyDiv (salesFigures, num_Divisions);
        
        //Pause system to allow user to read and interpret data prior to the program completing and closing itself.
        system("pause"); 

        return 0;
}
     
void divQtrSales (double array[][COLS], int rows)
{
     for (int x = 0; x < rows; x++)
     {
         cout<<setw(4)<<"Div"<<x+1<<"\n";
         cout<<"--------------------\n";
         
         for (int y = 0; y < COLS; y++)
         {
             cout<<setw(4)<< "Qtr"<<y+1;
             cout<<setw(4)<<"$ "<<array[x][y]<<"\n";
         }
         cout<< endl;
     }
}


void quarterlyTotals (double array[][COLS], int columns)
{
     for (int col = 0; col < COLS; col++) 
     {
         double total = 0;
         
         for (int row = 0; row < columns; row++)
         {
             total += array[row][col];
         }
         
         cout<<fixed<<showpoint<<setprecision(2);
         cout<<"Total sales for Quarter "<<(col+1)<<": $ "<<total<< "\n";
     }
}

void average (double array[][COLS], int columns)
{
       double average;
     for (int col = 0; col < COLS; col++) 
     {
         double total = 0;
         
         for (int row = 0; row < columns; row++)
         {
             total += array[row][col];
         }
         
         average = total / 4;
         cout<<fixed<<showpoint<<setprecision(2);
         cout<<"The average sales for Division #"<<(col+1)<<": $ "<<average<<endl;
     }
}

void highestQrtlyDiv (double array[][COLS], int rows)
{

        double highest = array[0][0]; //Highest variable will store the highest value in the array.
        int highDiv;
        
        //For loop with nested if expression to compare and determine highest value entered. Once highest value is determined
        //it is stored in the variable for highest.
        for (int x = 0; x < rows; x++)
        {
            for (int y = 0; y < COLS; y++)
            {
                if (array[x][y] > highest)
                {
                   highest = array[x][y];
                   highDiv = (x+1);
                }
            }
        cout<<"Highest Division for Quarter #"<<(x+1)<<": Div"<<highDiv<< endl;
        }
}

In many of the functions you want for each division check each quarter, but for some functions you will want to flip that to for each quarter check each division. That's certainly the case for highestQrtlyDiv(). Also, if you are going to check all four quarters in the same function, then you will need to reset the baseline value of highest each time through the outer loop.

This article has been dead for over six months. Start a new discussion instead.