// This program totals and averages the sales figures for any
// number of days. The figures are stored in a dynamically
// allocated array.
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
double *sales, // To dynamically allocate an array
total = 0.0, // Accumulator
average; // To hold average sales
int numDays, // To hold the number of days of sales
count; // Counter variable
// Get the number of days of sales.
cout << "How many days of sales figures do you wish ";
cout << "to process? ";
cin >> numDays;
// Dynamically allocate an array large enough to hold
// that many days of sales amounts.
sales = newdouble[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; // Make sales point to null.
return 0;
}
Second program
program 2.cpp
How can I modify the following program so that it no longer needs the <memory> library and works with Dynamically Allocated Memory, rather than Smart Pointers.
// This program demonstrates a unique_ptr pointing
// to a dynamically allocated array of integers.
#include <iostream>
#include <memory>
usingnamespace std;
int main()
{
int max; // Max size of the array
// Get the number of values to store.
cout << "How many numbers do you want to enter? ";
cin >> max;
// Define a unique_ptr smart pointer, pointing
// to a dynamically allocated array of ints.
unique_ptr<int[]> ptr( newint[max]);
// Get values for the array.
for (int index = 0; index < max; index++)
{
cout << "Enter an integer number: ";
cin >> ptr[index];
}
// Display the values in the array.
cout << "Here are the values you entered:\n";
for (int index = 0; index < max; index++)
cout << ptr[index] << endl;
return 0;
}
After modifying the Second program I want to make another copy of it ( program 3.cpp and add these two enhancements to that program:
•The program have to check to make certain that newwas able to find memory to allocate to create the Dynamic Array.
•If it is successful in finding memory to allocate, the program additionally computes and displays the mean of the values input from the user.
#include <iostream>
#include <iomanip>
#include <memory>
usingnamespace std;
int main()
{
double total {};
int numDays {};
// Get the number of days of sales.
cout << "How many days of sales figures do you wish ";
cout << "to process? ";
cin >> numDays;
auto sales {make_unique<double[]>(numDays)};
// Get the sales figures for each day.
cout << "Enter the sales figures below.\n";
for (int count = 0; count < numDays; ++count)
{
cout << "Day " << (count + 1) << ": ";
cin >> sales[count];
total += sales[count];
}
// Display the results
cout << fixed << showpoint << setprecision(2);
cout << "\n\nTotal Sales: $" << total << endl;
cout << "Average Sales: $" << total / numDays << endl;
}
#include <iostream>
usingnamespace std;
int main()
{
int max {};
// Get the number of values to store.
cout << "How many numbers do you want to enter? ";
cin >> max;
int* ptr {newint[max] {}};
// Get values for the array.
for (int index = 0; index < max; index++)
{
cout << "Enter an integer number: ";
cin >> ptr[index];
}
// Display the values in the array.
cout << "Here are the values you entered:\n";
for (int index = 0; index < max; index++)
cout << ptr[index] << endl;
delete[] ptr;
}