Help with Pointers

Hi all, so i am having trouble with pointers. Our assignment is to fill in the places where the comments are to make it work and i did some areas but the rest i still can't figure out how to make it work. Any help and all help will be greatly appreciated.

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
#include <iostream>

#include <iomanip>

using namespace std;

 

float * GetSales(int numOfSales);

 

int main()

{

       float *monthSales; // a pointer used to point to an array holding monthly sales float total = 0;

       float average; // average of monthly sales

       int numOfSales; // number of sales to be processed

       float total = 0; // total of all sales


 

       cout << fixed << showpoint << setprecision(2);

 
	  float GetSales(numOfSales);
       //***** write code to call function to read sales data from stdin into the dynamically allocated array

 

       for (int count = 0; count < numOfSales; count++)

       {

              total = total + monthSales[count];

       }

       average = total / numOfSales;//**** Fill in code to find the average;

       cout << "Average Monthly sale is $" << average << endl;

 
	   delete [] monthSales;
		monthSales = 0;
       //****** Fill in the code to deallocate memory assigned to the array.

 

       return 0;

}

 

float * GetSales(int numOfSales)

{

 

       float *dataPtr;

       cout << "How many monthly sales will be processed? ";

       cin >> numOfSales;

 
	    dataPtr = new float [numOfSales];
       //******** Fill in the code to allocate memory for the array pointed to by monthSales.

 

       if ( dataPtr == NULL )

       {

              cout << "Error allocating memory!\n";

              return NULL;

       }

       cout << "Enter the sales below\n";

       for (int count = 0; count < numOfSales; count++)

       {

              

              cout << "Sales for Month number "  << (count + 1)//******* Fill in code to show the number of the month

              << ":";
			  cin >> dataPtr[count];
              

              //****** Fill in code to read and store sales into an element of the array

       }

 

       return dataPtr;

}
Line 31 isn't quite right. If you take a closer look at GetSales(), you'll see that it takes a number and returns dynamically allocated array of floats. But you aren't saving that pointer to floats thing anywhere.
I'm not sure I follow the logic here, but a couple of things stand out:

You haven't actually initialised numOfSales, because it gets passed by value to GetSales(int). The numOfSales in main() remains unaffected by the changes you make to the numOfSales in GetSales(int). Do your cin >> numOfSales line in main() instead. Also, your line 31 has done nothing for monthSales, you need to assign the return value - maybe try "monthSales = GetSales(numOfSales)" instead.

You might also want to try some input validation, and write some tests, to impress your lecturer.

To summarize:
Move the numOfSales initialisation to main()
Properly initialise monthSales

See new code below:

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
#include <iostream>

#include <iomanip>

using namespace std;

 

float * GetSales(int numOfSales);

 

int main()

{

       float *monthSales; // a pointer used to point to an array holding monthly sales float total = 0;

       float average; // average of monthly sales

       int numOfSales; // number of sales to be processed

       float total = 0; // total of all sales


 

       cout << fixed << showpoint << setprecision(2);

       cout << "How many monthly sales will be processed? ";

       cin >> numOfSales;

 
	  monthSales = GetSales(numOfSales);
       //***** write code to call function to read sales data from stdin into the dynamically allocated array

 

       for (int count = 0; count < numOfSales; count++)

       {

              total = total + monthSales[count];

       }

       average = total / numOfSales;//**** Fill in code to find the average;

       cout << "Average Monthly sale is $" << average << endl;

 
	   delete [] monthSales;
		monthSales = 0;
       //****** Fill in the code to deallocate memory assigned to the array.

 

       return 0;

}

 

float * GetSales(int numOfSales)

{

 

       float *dataPtr;

 
	    dataPtr = new float [numOfSales];
       //******** Fill in the code to allocate memory for the array pointed to by monthSales.

 

       if ( dataPtr == NULL )

       {

              cout << "Error allocating memory!\n";

              return NULL;

       }

       cout << "Enter the sales below\n";

       for (int count = 0; count < numOfSales; count++)

       {

              

              cout << "Sales for Month number "  << (count + 1)//******* Fill in code to show the number of the month

              << ":";
			  cin >> dataPtr[count];
              

              //****** Fill in code to read and store sales into an element of the array

       }

 

       return dataPtr;

}
@tipaye

You're not supposed to actually do someone's homework. You should try to help without actually doing it for them, well not without working thru it with the op.
Apologies everyone, point taken.
Topic archived. No new replies allowed.