fork download
  1. // Saliha Babar CS1A Chapter 7, Page 444, #3
  2. //
  3. /******************************************************************************
  4.  * DISPLAY SALES FOR EACH FLAOURS
  5.  * ____________________________________________________________________________
  6.  * This program accepts number of jars sold for each 5 flavours of salsa. This
  7.  * program then calculates the total jars sold as well as highest selling flavour
  8.  * and lowest selling flavour.
  9.  * ____________________________________________________________________________
  10.  * INPUT
  11.  * NumOfJars : number of jars sold for each flavour
  12.  *
  13.  * OUTPUT
  14.  * total : total jars sold
  15.  * highestFLavour : flavour wih most jars sold
  16.  * lowestFlavour : flavour with least jars sold
  17.  * ***************************************************************************/
  18. #include <iostream>
  19. using namespace std;
  20.  
  21. int main() {
  22. const int TOTAL_FLAVOURS = 5;
  23. string flavours[TOTAL_FLAVOURS] ={ "Mild" , "Medium" , "Sweet" , "Hot" , "Zesty"};
  24. int NumOfJars[TOTAL_FLAVOURS];
  25. int total;
  26. int highest;
  27. int lowest;
  28. string highestFlavour;
  29. string lowestFlavour;
  30.  
  31. total=0;
  32.  
  33. for (int i = 0; i < TOTAL_FLAVOURS ; i++)
  34. {
  35. cout << "Enter the number of jars sold for the type : " << flavours[i] ;
  36. cin >> NumOfJars[i];
  37.  
  38. while (NumOfJars[i] < 0)
  39. {
  40. cout << "Enter positive number only\n";
  41. cin >> NumOfJars[i];
  42. }
  43.  
  44. cout << endl;
  45.  
  46. total += NumOfJars[i];
  47. }
  48.  
  49.  
  50. // Find the highest and the lowest value
  51. highest = NumOfJars[0];
  52. lowest = NumOfJars[0];
  53. highestFlavour = "Mild";
  54. lowestFlavour = "Mild";
  55.  
  56. for ( int i = 1 ; i < TOTAL_FLAVOURS ; i++)
  57. {
  58. if (NumOfJars[i] < lowest)
  59. {
  60. lowest = NumOfJars[i];
  61. lowestFlavour = flavours[i];
  62. }
  63.  
  64. if (NumOfJars[i] > highest)
  65. {
  66. highest = NumOfJars[i];
  67. highestFlavour = flavours[i];
  68. }
  69.  
  70. }
  71.  
  72. // Display the output
  73. for (int i = 0 ; i < TOTAL_FLAVOURS ; i++)
  74. {
  75. cout << flavours[i] << " salsa. Number of jars sold : " << NumOfJars[i] << endl;
  76. }
  77.  
  78. cout << "Total jars sold is " << total << endl;
  79. cout << "The highest selling flavour is " << highestFlavour << endl;
  80. cout << "The lowest selling flavour is " << lowestFlavour << endl;
  81.  
  82. return 0;
  83. }
Success #stdin #stdout 0.01s 5276KB
stdin
45
30
1
101
24
stdout
Enter the number of jars sold for the type : Mild
Enter the number of jars sold for the type : Medium
Enter the number of jars sold for the type : Sweet
Enter the number of jars sold for the type : Hot
Enter the number of jars sold for the type : Zesty
Mild salsa. Number of jars sold : 45
Medium salsa. Number of jars sold : 30
Sweet salsa. Number of jars sold : 1
Hot salsa. Number of jars sold : 101
Zesty salsa. Number of jars sold : 24
Total jars sold is 201
The highest selling flavour is Hot
The lowest selling flavour is Sweet