You are here:

# C++/Learning C++ for ME

Question
Mr. McArdell,
My name is Jeanette Akerson, and I decided to learn C++ on my own, just because I have always wanted to be able to make computers do what I want them to do.  I bought C++ How To Program by Dietel and Dietel because I had read good things about the book, and up until now, I have had no problem understanding and writing the programs in the exercises.  However, I have come across one on which I am stumped, have no idea where to begin or what to do.  I want to reiterate that I am NOT doing this as an assignment, this is for me to learn something new.  I would appreciate any assistance you can give me on how to get going on this project so that I can move on to the next one with confidence.  Here is the exercise:

Use a two-dimensional array to solve the following problem.  A company has four salespeople (1 to 4) who sell five different products (1 to 5).  Once a day, each salesperson passes in a slip for each different type of product sold.  Each slip contains the following:
a. Salesperson number
b. Product number
c. Total dollar value of that product sold that day
Thus, each salesperson passes in between 0 and 5 sales slips per day. Assume that the information from all of the slips for last month is available. Write a program that will read all this information for last month's sales and summarize the total sales by salesperson by product. All totals should be sorted in the two-dimensional array sales. AFter processing all the information for last month, print the results in tabular format with each of the columns representing a particular salesperson and each of the rows representing a particular product. Cross total each row to get the total sales of each product for last month; cross total each column to get the total sales by salesperson for last month. Your tabular printout should include these cross totals to the right of the totaled rows and to the bottom of the totaled columns.

All I can think of to start this is that I need to have a two-dimensional array like the problem states.  However, I'm not sure what to put in the array or how to set it up so that it accepts user input.  I think once I get that, I should be able to do the rest of the program as I have already done several array problems, just not two dimensional.
Thanks for any help you can provide, and explanations would be great!
Jeanette

Thank you for pointing out your circumstances. However as you state what specific help you require to just set you on your way I would probably have provided assistance even if I thought it were coursework or homework. Most such requests just re-post their assignment question expecting (presumably) that I provide them with a complete solution they can hand in as their own!

Now I do not have the book you refer to and I am not sure the text of the question you post is 100% correct. Specifically, is the following (as copied from your question text) correct?

"All totals should be sorted in the two-dimensional array sales."

It is the word "sorted" that has me worried. Should it be "stored" perhaps?

The reason is that having to both store the data and also sorting it requires more effort and complexity.

Assuming to start with that you are just required to store the sales totals in the 2 dimensional array then you just have to observe the following:

1/ for each sales person you need to save totals for each of their sales of the 5 products.

So if you only had one salesperson you would of course use a single dimensional array:

double salesForSingleSalesperson[NumberOfProducts];

As we are talking money, which has fractional parts (the cents), I have chosen to store the values in floating point double type array elements.

NumberOfProducts is an unsigned integer constant having a value of 5:

const unsigned int NumberOfProducts(5);

2/ there is not one salesperson but 4. You could use an array for each:

double salesForSalesperson0[NumberOfProducts];
double salesForSalesperson1[NumberOfProducts];
double salesForSalesperson2[NumberOfProducts];
double salesForSalesperson3[NumberOfProducts];

I suspect you have been through similar code when learning why single dimensional arrays are useful, but with collections of scalar (single valued) variables rather than single dimensional arrays (also known as vectors by the way). So you might then guess my next point: It is more useful to use an array of 4 arrays of salespersons' product sales totals:

double sales[NumberOfSalespersons][NumberOfProducts];

NumberOfSalespersons, like NumberOfProducts, is a constant unsigned integer:

const unsigned int NumberOfSalespersons(4);

This array can be thought of like a grid, table or maybe a spreadsheet. It has 4 * 5 elements (i.e. 20 elements) arranged so there is one group of 5 product totals for each of the 4 salespersons.

3/ the question states that the input data comes in groups of three values: salesperson number (1 to 4), product number (1 to 5) and dollar value of the sales. Now we have an array arranged as rows of salespersons and columns of product numbers. This is also the data input to us as the first two items for each slip's record. They can therefore be used to specify which element of the sales array should be updated. A first try might then be to specify the following element needs updating:

sales[salespersonNumber][productNumber]

Where salespersonNumber and productNumber are the first two items of the information for a slip entered. However our array starts with element [0][0] - i.e. the element for the value of sales of product 1 by salesperson 1 is at [0][0] not [1][1], so we convert from the input range for these values to the C/C++ array-indexes range by subtracting 1 from these values, for example:

sales[salespersonNumber-1][productNumber-1]

4/ What you need to do with each salesperson/product dollar value is add them up. This means you will need to ensure that each element of the sales array is initialised to zero. Then just add each dollar value entered to the relevant sales array element, e.g.:

sales[salespersonNumber-1][productNumber-1] += dollarTotal;

Where dollarTotal is the third value read for each set of slip records.

Now if you also have to sort the sales figures then we loose the mapping of array index to (say) salesperson number (the first array index is the salesperson number minus 1). The problem is similar if the values are sorted by product.

This brings up a further problem with the question if indeed sorting is required: Which way round? By salesperson or by product, or both? And should the values be sorted in ascending (lowest first) or descending (highest first) order?

To expand on this issue. If we sort the data so that the first row contains the lowest values for each product then the index value for the row no longer has any correlation to the salespeople which obtained them, for example if we have (view with a single spaced font such as Courier):

Product # 1  2  3  4  5
------------------------
10 34 76 89 99    Sales person 1's product totals
01 23 54 76 12    Sales person 2's product totals
87 22 65 34 96    Sales person 3's product totals
65 06 62 67 48    Sales person 4's product totals

Sorting each column so that the numbers are in ascending order gives:

Product # 1  2  3  4  5
------------------------
01 06 54 34 12
10 22 62 67 48
65 23 65 76 96
87 34 76 89 99

This scrambles the order so we no longer know which salesperson the figures belong to.

Likewise if we sort them by product number, so the rows run in ascending order we get:

10 34 76 89 99    Sales person 1's product totals
01 12 23 54 76    Sales person 2's product totals
22 34 65 87 96    Sales person 3's product totals
06 48 62 65 67    Sales person 4's product totals

This scrambles the order so we no longer know which product numbers the values refer to.

Finally we can sort the value so both the columns and row are in ascending order:

01 06 12 34 54
10 22 48 62 67
23 65 65 76 96
34 76 87 89 99

The above is sorted so that the columns are first sorted into ascending order and then the result sorted so the rows are also in ascending order. The following is the result of sorting the rows into ascending order first and then the columns:

01 12 23 54 67
06 34 62 65 76
10 34 65 87 96
22 48 76 89 99

The results are different but in both cases we no longer know which salesperson or which product number the sales figures refer to.

So you see why I am not really sure if sorting the sales table array is required or even sensible. If sorting is required then maybe you could give some thought to possible solutions and post a further question if you cannot see any solutions (hint: I would use additional 1 or 2 dimensional arrays).

C++

Volunteer

#### Ralph McArdell

##### Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

##### Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials