fork download
  1. /* 1 **************************************************
  2.  predefined code (header files, using directive)
  3.  ****************************************************/
  4. #include <iostream> // cout, cin, and endl
  5. #include <string> // string objects
  6. using namespace std; // allows scope in std::<object>
  7.  
  8.  
  9. /* 2 **************************************************
  10.  structure definitions and global constants:
  11.  ****************************************************/
  12. const unsigned int YEARS = 3; // years of data to collect
  13. const unsigned int MONTHS = 12; // months per year
  14. const string monthNames[12] = // array strings for month names
  15. {
  16. "January",
  17. "February",
  18. "March",
  19. "April",
  20. "May",
  21. "June",
  22. "July",
  23. "August",
  24. "September",
  25. "October",
  26. "November",
  27. "December" };
  28.  
  29. /* 3 **************************************************
  30.  function prototypes:
  31.  ****************************************************/
  32. int collectSalesData (void); // expects no arguments. returns int.
  33. int** createBookSalesArray (const unsigned int, const unsigned int); // expects 2 const unsigned int. returns pointer-to-array-of-int.
  34. void setBookSales (int**, const unsigned int, const unsigned int); // expects pointer-to-array-of-int. 2 const unsigned int. returns nothin.
  35. unsigned int getTotalSales (int**, const unsigned int, const unsigned int); // expects pointer-to-array-of-int. 2 const unsigned int. returns unsigned int.
  36.  
  37. /* 4 *************************************************
  38.  main() code
  39.  ****************************************************/
  40. int main() // required function returns int to OS.
  41. {
  42.  
  43. /* Create 2D Array */
  44. int** monthlySalesPtr = createBookSalesArray(YEARS, MONTHS);
  45.  
  46.  
  47. /* Get Input From User. Pass Along Size of Arrays for overflow protection; YEARS, MONTHS */
  48. setBookSales(monthlySalesPtr, YEARS, MONTHS);
  49.  
  50.  
  51. /* Stores Total # of Books Sold. Stores Returned Value From getTotalSales() */
  52. unsigned int allBookSales = 0;
  53. allBookSales = getTotalSales(monthlySalesPtr, YEARS, MONTHS);
  54.  
  55.  
  56. /* Reports Total Book Sales for # of YEARS */
  57. cout << "Total annual sales: " << allBookSales << " books." << endl << endl;
  58.  
  59.  
  60. system("pause"); // halts execution. keeps console in view.
  61. return 0; // indicates successful execution.
  62. }
  63.  
  64.  
  65. /* 5 **************************************************
  66.  new user-defined functions
  67.  ****************************************************/
  68. /*
  69.  * Name: createBookSalesArray()
  70.  * Arguments: pointer to array of data, number of years, number of months
  71.  * Description: Asks user for monthly book sales and stores them in 2D Array [which was created on the heap, from main()]
  72.  */
  73. int** createBookSalesArray(const unsigned int YRS, const unsigned int MNTHS)
  74. {
  75. int** bookSalesArrayPtr = new int*[YRS]; // bookSalesArrayPtr is a Pointer to array of pointers, created on the heap (size of bookSalesArrayPtr is YEAR; 3)
  76. for (unsigned int i = 0; i < YRS; i++) // Intends to: Loop through each element of the array
  77. bookSalesArrayPtr[i] = new int[MNTHS]; // and dynamically (heap) create an array of integers (size of int array is MONTHS; 12)
  78. return bookSalesArrayPtr; // Return to main(): a pointer-to-array-of-pointers [that will store the sales data]; multidimensional array.
  79. }
  80.  
  81.  
  82.  
  83. /*
  84.  * Name: setBookSales()
  85.  * Arguments: pointer to array of data, number of years, number of months
  86.  * Description: Asks user for monthly book sales and stores them in 2D Array [which was created on the heap, from main()]
  87.  */
  88. void setBookSales(int** salesDataPtr, const unsigned int YRS, const unsigned int MNTHS)
  89. {
  90. /* Ask User for Monthly Values */
  91. for (unsigned int row = 0; row < YRS; row++) // Intends to: Loop through each 1st-Dimension element of the array [YRS]
  92. for (unsigned int col = 0; col < MNTHS; col++) // and each 2nd-Dimension element of the array [MNTHS]
  93. {
  94. cout << "Enter " << monthNames[col] << "'s sales numbers (for year " << (row + 1) << "): "; // Ask For Sales, Each Month, By Year
  95. cin >> salesDataPtr[row][col]; // Save the sales data by [YRS][MNTHS]
  96. }
  97.  
  98. return; // Formal Return.
  99. }
  100.  
  101.  
  102.  
  103. /* Name: getTotalSales()
  104.  * Arguments: pointer to array of data, const number of years, const number of months
  105.  * Description: Sums book sales of all months, of all years. Returns total annual sales to main() for report to console.
  106.  */
  107. unsigned int getTotalSales(int** salesDataPtr, const unsigned int YRS, const unsigned int MNTHS)
  108. {
  109. unsigned int totalAnnualSales = 0; // Holds sum of each month, for each year
  110. for (unsigned int row = 0; row < YRS; row++) // Intends to: Loop through each 1st-Dimension element of the array [YRS]
  111. for (unsigned int col = 0; col < MNTHS; col++) // and each 2nd-Dimension element of the array [MNTHS]
  112. {
  113. totalAnnualSales += salesDataPtr[row][col]; // Adds each months sales number to totalAnnualSales, thus summing all months, for each year.
  114. }
  115. return totalAnnualSales; // Return total sales for all months, for all years.
  116.  
  117. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty