fork download
  1. // Torrez, Elaine CS1A Chapter 7, P. 444, #3
  2. // *****************************************************************************************
  3. // * CHIPS AND SALSA *
  4. // *---------------------------------------------------------------------------------------*
  5. // * This program tracks monthly jar sales for five salsa types using parallel arrays. *
  6. // * It prompts for jars sold (no negatives), then reports per-item sales, total sales, *
  7. // * and the highest- and lowest-selling product names (ties supported). *
  8. // * *
  9. // * INPUT: *
  10. // * names[5] : {"mild","medium","sweet","hot","zesty"} *
  11. // * sales[5] : user-entered non-negative integers *
  12. // * *
  13. // * OUTPUT: *
  14. // * Per-salsa sales, total jars sold, highest seller(s), lowest seller(s). *
  15. // *****************************************************************************************
  16.  
  17. #include <iostream>
  18. #include <iomanip>
  19. #include <string>
  20. using namespace std;
  21.  
  22. int main()
  23. {
  24. /***** CONSTANTS *****/
  25. const int SIZE = 5;
  26.  
  27. /***** PARALLEL ARRAYS *****/
  28. string names[SIZE] = {"mild", "medium", "sweet", "hot", "zesty"};
  29. int sales[SIZE];
  30.  
  31. /***** VARIABLES *****/
  32. int total = 0;
  33. int maxIndex = 0;
  34. int minIndex = 0;
  35.  
  36. /***** INPUT SECTION *****/
  37. cout << "Enter the number of jars sold this month for each salsa type:\n";
  38. for (int i = 0; i < SIZE; i++)
  39. {
  40. cout << " " << names[i] << ": ";
  41. cin >> sales[i];
  42.  
  43. // Validate: no negative values
  44. while (sales[i] < 0)
  45. {
  46. cout << " Invalid. Jars sold cannot be negative. Re-enter: ";
  47. cin >> sales[i];
  48. }
  49.  
  50. total += sales[i];
  51.  
  52. // Track min/max as we go (safe because i progresses from 0→4)
  53. if (sales[i] > sales[maxIndex]) maxIndex = i;
  54. if (sales[i] < sales[minIndex]) minIndex = i;
  55. }
  56.  
  57. /***** OUTPUT SECTION *****/
  58. cout << "\n==================== CHIPS & SALSA REPORT ====================\n";
  59. cout << left << setw(12) << "Salsa" << right << setw(10) << "Jars Sold\n";
  60. cout << "--------------------------------------------------------------\n";
  61. for (int i = 0; i < SIZE; i++)
  62. cout << left << setw(12) << names[i] << right << setw(10) << sales[i] << "\n";
  63.  
  64. cout << "--------------------------------------------------------------\n";
  65. cout << left << setw(12) << "TOTAL" << right << setw(10) << total << "\n";
  66.  
  67. // Support ties by listing all matching the min/max values
  68. int maxVal = sales[maxIndex];
  69. int minVal = sales[minIndex];
  70.  
  71. cout << "\nHighest selling product(s) (" << maxVal << "): ";
  72. bool first = true;
  73. for (int i = 0; i < SIZE; i++)
  74. if (sales[i] == maxVal) {
  75. cout << (first ? "" : ", ") << names[i];
  76. first = false;
  77. }
  78.  
  79. cout << "\nLowest selling product(s) (" << minVal << "): ";
  80. first = true;
  81. for (int i = 0; i < SIZE; i++)
  82. if (sales[i] == minVal) {
  83. cout << (first ? "" : ", ") << names[i];
  84. first = false;
  85. }
  86.  
  87. cout << "\n==============================================================\n";
  88.  
  89. return 0;
  90. }
  91.  
Success #stdin #stdout 0.01s 5320KB
stdin
mild:   15
medium: 22
sweet:  7
hot:    22
zesty:  9
stdout
Enter the number of jars sold this month for each salsa type:
  mild:   medium:   sweet:   hot:   zesty: 
==================== CHIPS & SALSA REPORT ====================
Salsa       Jars Sold
--------------------------------------------------------------
mild                 0
medium               0
sweet       2127048120
hot               5249
zesty       2127048120
--------------------------------------------------------------
TOTAL        -40865807

Highest selling product(s) (2127048120): sweet, zesty
Lowest selling product(s) (0): mild, medium
==============================================================