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
|
#include <iostream>
#include <iomanip>
using namespace std;
// cin.get() <-------------- used to let the user read the screen
// Function prototypes
void calcSales(const int [], const double [], double [], int);
void showOrder(const double [], const int [], int);
void dualSort(int [], double [], int);
void showTotals(const double [], const int [], int);
// NUM_PRODS is the number of products produced.
const int NUM_PRODS = 12; // This is were i would like to change values which go 1,2,3... in output to say product id 10002,10003,etc.
int units[NUM_PRODS];
int main ()
{
// Array with product ID numbers
int id[NUM_PRODS] = {1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12};
// Array with number of units sold for each product
for (int count = 0; count < NUM_PRODS; count++)
{
cout << "Enter the number of units sold by product id "
<< (count + 1) << ": ";
cin >> units[count];
cin.get();
}
// Array with the product's prices
double prices[NUM_PRODS] = {11.95, 14.97, 9.99, 8.72, 54.99, 82.92,
2.99, 11.99, .99, 8.49, 7.62, 4.99};
// Array to hold the computed sales amount
double sales[NUM_PRODS];
// Calculate each product's sales
calcSales( units, prices, sales, NUM_PRODS);
// Sort the elements in the sales array in decending order
// and shuffle the id numbers to keep the parallel.
dualSort( id, sales, NUM_PRODS);
// Set the numeric output formatting.
cout << setprecision (2) << fixed << showpoint;
// Display the products and sales amounts
showOrder(sales, id, NUM_PRODS);
// Display total units sold and total sales
showTotals(sales, units, NUM_PRODS);
return 0;
}
//***************************************************************
// Definition of CalcSales. Accepts units, prices, and sales *
// arrays as arguments. The size of these arrays is passed *
// into the num parameter, This function calculates each *
// product's sales by multiplying its units sold by each unit's *
// price. The result is stored in the sales array. *
//***************************************************************
void calcSales(const int units[], const double prices[], double sales[], int num)
{
for (int index = 0; index < num; index++)
sales[index] = units[index] * prices[index];
}
//****************************************************************
// Definition of function dualSort. Accepts ids and sales arrays *
// as arguments. The size of these arrays is passed into size *
// This function performs a descending order selection sort on *
// the sales array. The elements of the id array are exchanged *
// identically as those of the sales array. Size is the number *
// of elements in each array. *
//****************************************************************
void dualSort(int id[], double sales[], int size)
{
int startScan, maxIndex, tempid;
double maxValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
maxIndex = startScan;
maxValue = sales[startScan];
tempid = id[startScan];
for(int index = startScan + 1; index < size; index++)
{
if (sales[index] > maxValue)
{
maxValue = sales[index];
tempid = id[index];
maxIndex = index;
}
}
sales[maxIndex] = sales[startScan];
id[maxIndex] = id[startScan];
sales[startScan] = maxValue;
id[startScan] = tempid;
}
}
void showOrder(const double sales[], const int id[], int num)
{
cout << "Product Number\t\tSales\n";
cout << "-------------------------------------\n";
for (int index = 0; index < num; index++)
{
cout << id[index] << "\t\t\t$";
cout << setw(8) << sales[index] << endl;
}
cout << endl;
}
//*************************************************************
// Show totals function
void showTotals(const double sales[], const int units[], int num)
{
int totalUnits = 0;
double totalSales = 0.0;
for (int index = 0; index < num; index++)
{
totalUnits += units[index];
totalSales += sales[index];
}
cout << "-------------------------------------\n";
cout << " Total Units sold: " << totalUnits << endl;
cout << " Total Sales: $" << totalSales << endl;
cin.get();
}
|