1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
// Chapter 8 - Chips and Salsa
// This program produces a sales report for a salsa maker who
// markets 5 types of salsa. It includes total sales for all
// products and identifies the highest and lowest selling product.
// Kara Pardue
// 12/12/2011
// The program should use 2 parallel arrays, one for string names, and one for integer sales.
// It should call on three functions, one to get the total sales, one for highest selling salsa, and one for lowest selling.
// These three functions should use the arrays in order to return their data.
// The program should then print all of the data, including salsa type, sales of each, total sales, and highest and lowest of each.
#include<iostream>
#include <iomanip>
#include <string>
using namespace std;
// Function prototypes
int getTotal(int [], int);
int posOfSmallest(int [], int);
int posOfLargest(int [], int);
int main()
{
const int NUM_TYPES = 5;
int sales[NUM_TYPES];
string name[NUM_TYPES] = {"Mild", "Medium", "Sweet", "Hot", "Zesty"};
// Create the arrays for the names and sales amounts
int totalJarsSold,
hiSalesProduct,
loSalesProduct;
// Input the number of jars sold
for (int type = 0; type < NUM_TYPES; type++)
{
cout << "Jars sold last month of " << name[type] << ": ";
cin >> sales[type];
while (sales[type] < 0)
{ cout << "Jars sold must be 0 or more. Please re-enter: ";
cin >> sales[type];
}
}
// Call functions to get total sales and high and low selling products
totalJarsSold = getTotal(sales, NUM_TYPES);
loSalesProduct = posOfSmallest(sales, NUM_TYPES);
hiSalesProduct = posOfLargest(sales, NUM_TYPES);
// Produce the sales report
cout << endl << endl;
cout << " Salsa Sales Report \n\n";
cout << "Name Jars Sold \n";
cout << "____________________________\n";
cout << name[0] << " " << sales[0] << "\n";
cout << name[1] << " " << sales[1] << "\n";
cout << name[2] << " " <<sales[2] << "\n";
cout << name[3] << " " << sales[3] << "\n";
cout << name[4] << " " << sales[4] << "\n";
// insert the code that prints the salsa names and number jars sold
cout << "\nTotal Sales:" << setw(15) << totalJarsSold << endl;
cout << "High Seller: " << name[hiSalesProduct] << endl;
cout << "Low Seller : " << name[loSalesProduct] << endl;
system("pause");
return 0;
}
/************************************************************
* getTotal *
* Calculates and returns the total of the values stored in *
* the array passed to the function. *
************************************************************/
int getTotal (int array[], int numElts)
{
int total = 0;
for (int type = 0; type < numElts; type++)
total += array[type];
return total;
}
/************************************************************
* posOfLargest *
* Finds and returns the subscript of the array position *
* holding the largest value in the array passed to the *
* function. *
************************************************************/
int posOfLargest(int array[], int numElts)
{
int indexOfLargest = 0;
for (int pos = 1; pos < numElts; pos++)
{
if (array[pos] > array[indexOfLargest])
indexOfLargest = pos;
}
return indexOfLargest;
}
/************************************************************
* posOfSmallest *
* Finds and returns the subscript of the array position *
* holding the smallest value in the array passed to the *
* function. *
************************************************************/
int posOfSmallest(int array[], int numElts)
{
int indexOfSmallest = 0;
for (int pos = 1; pos < numElts; pos++)
{
if (array[pos] < array[indexOfSmallest])
indexOfSmallest = pos;
}
return indexOfSmallest;
}
/*
Jars sold last month of Mild: 34
Jars sold last month of Medium: 89
Jars sold last month of Sweet: 63
Jars sold last month of Hot: 109
Jars sold last month of Zesty: 47
Salsa Sales Report
Name Jars Sold
____________________________
Mild 34
Medium 89
Sweet 63
Hot 109
Zesty 47
Total Sales: 342
High Seller: Hot
Low Seller : Mild
*/
|