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
|
// 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.
#include<iostream>
#include <iomanip>
using namespace std;
// Function prototypes
int getTotal(int [], int);
int posOfHighest(int[], int);
int posOfSmallest(int [], int);
int main()
{
const int NUM_TYPES = 5;
// Create the arrays for the names and sales amounts
string name[NUM_TYPES] = {"mild", "sweet", "medium", "hot", "zesty"};
int sales[NUM_TYPES];
int totalJarsSold,
hiSalesProduct,
loSalesProduct,
totalSales;
// Input the number of jars sold
for (int type = 0; type < NUM_TYPES; type++)
{
cout << "Jar 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);
hiSalesProduct = posOfHighest(sales, NUM_TYPES);
loSalesProduct = posOfSmallest(sales, NUM_TYPES);
// Produce the sales report
cout << endl << endl;
cout << " Salsa Sales Report \n\n";
cout << "Name Jars Sold \n";
cout << "____________________________\n";
for (int salsaType = 0; salsaType < NUM_TYPES; salsaType++)
// insert the code that prints the salsa names and number jars sold
totalSales = getTotal(sales, NUM_TYPES);
cout << "\nTotal Sales:" << setw(15) << totalJarsSold << endl;
cout << "High Seller: " << name[hiSalesProduct] << endl;
cout << "Low Seller : " << name[loSalesProduct] << endl;
return 0;
}
/************************************************************
* getTotal *
* Calculates and returns the total of the values stored in *
* the array passed to the function. *
************************************************************/
int getTotal(int array[] , int count)//$you didn't name the second argument
{
int total = 0;//$meant to be general, right.
for (int i = 0; i < count; i++)//$this for loop was really screwed up.
total += array[i];
return total;
}
/************************************************************
* posOfLargest *
* Finds and returns the subscript of the array position *
* holding the largest value in the array passed to the *
* function. *
************************************************************/
int posOfHighest(int array[], int numElts)
{
int indexOfHighest = 0;
for (int pos = 1; pos > numElts; pos++)
{
if(array[pos] > array[indexOfHighest])
indexOfHighest = pos;
}
return indexOfHighest;
}
/************************************************************
* 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;
}
|