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
|
#include "SalesNmSpce.h"
#include <iostream>
#include <climits>
///sizeof(array) returns size in bytes, sizeof(double) returns size of the double in bytes. By dividing
///the two together you receive the array length
namespace SALES
{
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-
struct sales //structure declaration
{
double _Sales[QUARTERS];
double average;
double max;
double min;
};
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-
void _Set(sales & s, const double Ar[])
{
double * ar; //temp array
for(int i = 0; i < sizeof(Ar)/sizeof(double); i++)
ar[i] = Ar[i]; //copy contents of constant array into temp
SortArr(ar); //sort the array
for(int i = 0; i < 4; i++) s._Sales[i] = ar[i]; //grab the lowest values
_Compute(s); //find average, min and max
}
void _InterSet(sales & s)
{
double ar[4]; //stores choices by user
std::cout << "Welcome to the interactive sales gathering program...\n";
std::cout << "Please input the four values of your quarter sales in order First to Fourth. Ex) 12.3 4 3.2 9\n-- ";
for(int i = 0; i < 4; i++)
{
std::cin >> ar[i]; //read in choice
s._Sales[i] = ar[i]; //set array to the choice
}
_Compute(s);//find average, min and max
}
void SortArr(double * Arr)
{
int i, j, first, temp;
int numLength = (sizeof(Arr)/sizeof(double));
for (i= numLength - 1; i > 0; i--) //start at last point in the array
{
first = 0; // initialize to subscript of first element
for (j=1; j<=i; j++) // locate smallest between positions 1 and i.
{
if (Arr[j] < Arr[first])
first = j; //first is the new lowest int found
}
temp = Arr[first]; // Swap smallest found with element in position i.
Arr[first] = Arr[i];
Arr[i] = temp;
}
}
void _Compute(sales & s)
{
double average,max,min; //temporary variables to store data
max = 0; min = INT_MAX; //INT_MAX is the limit, around 2,000,000,000
int Length = (sizeof(s._Sales)/sizeof(double)); //calculate length
for(int i = 0; i < Length;i++)
{
if(s._Sales[i] > max) max = s._Sales[i]; //set max and min
if(s._Sales[i] < min) min = s._Sales[i];
average += s._Sales[i]; //add onto the average
}
///set values
s.average = average / Length;
s.max = max;
s.min = min;
}
void ShowSales(const sales & s) //display results
{
std::cout << "--------DISPLAYING SALES REPORT--------\n";
std::cout << "QUARTER 1: " << s._Sales[0];
std::cout << "\tQUARTER 2: " << s._Sales[1];
std::cout << "\nQUARTER 3: " << s._Sales[2];
std::cout << "\tQUARTER 4: " << s._Sales[3];
std::cout << "\n MEAN: " << s.average << " Minimum: " << s.min << " Maximum: " << s.max;
std::cout << std::endl;
}
}
main.cpp
[code]
#include <iostream>
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-
#include "SalesNMSpce.h" //linking issue maybe?
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-
int main()
{
double __Sales[4] = {12.3,14.97,223.9,1.4}; //create array for setting sales using an array
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-
SALES::sales Obj; //create sales object(DOES NOT WORK)
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-
SALES::_Set(Obj,__Sales);
SALES::ShowSales(Obj);
std::cout << "Not with interactive: \n";
SALES::_InterSet(Obj);
SALES::ShowSales(Obj);
}
|