Modifying and changing libraries

How can I rewrite the following Program so that works using Smart Pointers instead of Dynamic Memory Allocation.??
First program
program 1.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
48
49
50
51
52
53
 // 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>
using namespace 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 = 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; // 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.

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
      // This program demonstrates a unique_ptr pointing
    // to a dynamically allocated array of integers.
    #include <iostream>
    #include <memory>
    using namespace 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( new int[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.

GradeList.txt
67
78
78
95
78
45
52
89
83
69
72
36
67
56
86
58
78
56
58
67
58
44
83
84
78
91
44
67
61
70
75
81
77
86
89
71
69
52
83
50
58
73
76
97
79
88
94
89
85
80
Is there any good reason why you don't simply use vectors?
this is a lab that's why I need help
For 1)

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
#include <iostream>
#include <iomanip>
#include <memory>
using namespace 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;
}


For 2)

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
#include <iostream>
using namespace 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 {new int[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;
}


PS What's with gradelist.txt??????
Last edited on
Topic archived. No new replies allowed.