/* 1 **************************************************
predefined code (header files, using directive)
****************************************************/
#include <iostream> // cout, cin, and endl
#include <string> // string objects
using namespace std; // allows scope in std::<object>
/* 2 **************************************************
structure definitions and global constants:
****************************************************/
const unsigned int YEARS = 3; // years of data to collect
const unsigned int MONTHS = 12; // months per year
const string monthNames[12] = // array strings for month names
{
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December" };
/* 3 **************************************************
function prototypes:
****************************************************/
int collectSalesData (void); // expects no arguments. returns int.
int** createBookSalesArray (const unsigned int, const unsigned int); // expects 2 const unsigned int. returns pointer-to-array-of-int.
void setBookSales (int**, const unsigned int, const unsigned int); // expects pointer-to-array-of-int. 2 const unsigned int. returns nothin.
unsigned int getTotalSales (int**, const unsigned int, const unsigned int); // expects pointer-to-array-of-int. 2 const unsigned int. returns unsigned int.
/* 4 *************************************************
main() code
****************************************************/
int main() // required function returns int to OS.
{
/* Create 2D Array */
int** monthlySalesPtr = createBookSalesArray(YEARS, MONTHS);
/* Get Input From User. Pass Along Size of Arrays for overflow protection; YEARS, MONTHS */
setBookSales(monthlySalesPtr, YEARS, MONTHS);
/* Stores Total # of Books Sold. Stores Returned Value From getTotalSales() */
unsigned int allBookSales = 0;
allBookSales = getTotalSales(monthlySalesPtr, YEARS, MONTHS);
/* Reports Total Book Sales for # of YEARS */
cout << "Total annual sales: " << allBookSales << " books." << endl << endl;
system("pause"); // halts execution. keeps console in view.
return 0; // indicates successful execution.
}
/* 5 **************************************************
new user-defined functions
****************************************************/
/*
* Name: createBookSalesArray()
* Arguments: pointer to array of data, number of years, number of months
* Description: Asks user for monthly book sales and stores them in 2D Array [which was created on the heap, from main()]
*/
int** createBookSalesArray(const unsigned int YRS, const unsigned int MNTHS)
{
int** bookSalesArrayPtr = new int*[YRS]; // bookSalesArrayPtr is a Pointer to array of pointers, created on the heap (size of bookSalesArrayPtr is YEAR; 3)
for (unsigned int i = 0; i < YRS; i++) // Intends to: Loop through each element of the array
bookSalesArrayPtr[i] = new int[MNTHS]; // and dynamically (heap) create an array of integers (size of int array is MONTHS; 12)
return bookSalesArrayPtr; // Return to main(): a pointer-to-array-of-pointers [that will store the sales data]; multidimensional array.
}
/*
* Name: setBookSales()
* Arguments: pointer to array of data, number of years, number of months
* Description: Asks user for monthly book sales and stores them in 2D Array [which was created on the heap, from main()]
*/
void setBookSales(int** salesDataPtr, const unsigned int YRS, const unsigned int MNTHS)
{
/* Ask User for Monthly Values */
for (unsigned int row = 0; row < YRS; row++) // Intends to: Loop through each 1st-Dimension element of the array [YRS]
for (unsigned int col = 0; col < MNTHS; col++) // and each 2nd-Dimension element of the array [MNTHS]
{
cout << "Enter " << monthNames[col] << "'s sales numbers (for year " << (row + 1) << "): "; // Ask For Sales, Each Month, By Year
cin >> salesDataPtr[row][col]; // Save the sales data by [YRS][MNTHS]
}
return; // Formal Return.
}
/* Name: getTotalSales()
* Arguments: pointer to array of data, const number of years, const number of months
* Description: Sums book sales of all months, of all years. Returns total annual sales to main() for report to console.
*/
unsigned int getTotalSales(int** salesDataPtr, const unsigned int YRS, const unsigned int MNTHS)
{
unsigned int totalAnnualSales = 0; // Holds sum of each month, for each year
for (unsigned int row = 0; row < YRS; row++) // Intends to: Loop through each 1st-Dimension element of the array [YRS]
for (unsigned int col = 0; col < MNTHS; col++) // and each 2nd-Dimension element of the array [MNTHS]
{
totalAnnualSales += salesDataPtr[row][col]; // Adds each months sales number to totalAnnualSales, thus summing all months, for each year.
}
return totalAnnualSales; // Return total sales for all months, for all years.
}