-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntroBookDynamicAllocation.cpp
47 lines (38 loc) · 1.14 KB
/
IntroBookDynamicAllocation.cpp
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
#include <iostream>
#include <iomanip>
using namespace std;
int main(int argc, char **argv){
double *sales, //dynamically allocate an array
total=0.0, //"accumulator"
average; //To hold average sales
int numDays, //To hold num days of sales
count; //Counter variable
//Get the number of days of sales
cout << "How many days of sales ";
cout << "figures do you wish to process? ";
cin >> numDays;
//Dynamically allocate an array large
//enough to handle that many days of
//sales amounts
sales = new double[numDays];
//Get the sales figures for each day
cout << "Enter the sales figures below\n";
for(count=0; count<numDays; count++){
cout << "Day " << (count + 1) << ": ";
cin >> sales[count];
}
//Calculate the total sales
for(count=0; count<numDays; count++){
total += sales[count];
}
//Calculate the average sales per day
average = total / numDays;
//Display the results
cout << fixed << showpoint << setprecision(2);
cout << "\n\nTotal Sales: $" << total << endl;
cout << "Average Sales: $" << average << endl;
//Free dynamically allocated memory
delete[] sales;
sales = 0; //makes sales point to null
return 0;
}