Dynamic Memory in Classes?

Hi. I need help with trying to figure out using this array across different functions for a class. Here is my task:
"Implement the class illustrated below, and a main function that demonstrates it
working. You may need to look up random number generation if you can't remember it
from coursework assignment 1. Implement each of the functions listed.
Tips:
•The constructor should set the size to 0 and initialise the list pointer to NULL
• The destructor should deallocate any memory allocated for the list of numbers
•The generate function should create a new array of integers of size n, and fill it
with random numbers between 0 and 100"

I have two private class members of int pointer list and int size. With what I have so far, I have managed to get the generate function to fill the array randomly but when I have the average, it is giving a very unexpected result. I would really appreciate some help with this. Thank you!

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
  #include "stdafx.h"
#include "RandomArray.h"
#include <stdlib.h>
#include <iostream>

RandomArray::RandomArray()
{
	size = 0;
	list = nullptr;

}




void RandomArray::generate(int n)
{
	
	int* Array = new int[n];

	for (int i = 0; i < n; i++)
	{
		Array[n] = rand() % 101;
		std::cout << Array[n] << std::endl;
	}

	size = n;
	list = Array;

}


void RandomArray::print()
{
	
}


void RandomArray::getAverage()
{
	int sum = 0;
	for (int i = 0; i < size; i++)
	{
		sum += list[i];
	}
	sum / size;
	std::cout << sum << std::endl;
}


void RandomArray::getMin()
{
	int current;
	int min;

	min = list[1];

	for (int i = 0; i < size; i++)
	{
		current = list[i];

		if (current < min)
		{
			min = current;
		}
	}
}


void RandomArray::getMax()
{

}


RandomArray::~RandomArray()
{

}
For reference, this is what the class definition looks like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once
class RandomArray
{
private:
	int* list;
	int size;

public:
	RandomArray();
	~RandomArray();
	void generate(int n);
	void print();
	void getAverage();
	void getMin();
	void getMax();
};

You should check in your generate to make sure you haven't already allocated it

1
2
3
4
5
6
7
8
9
10
11
void RandomArray::generate(int n)
{
  if (!list && n > 0) 
  {
    list = new int[n]{0}; //fill with zeros
    for (int i = 0; i < n; i++)
    {
      list[i] = rand() % 101;  
    }
  }
}


In your destructor, call delete[] on your list, but only if it isn't null (you don't want to delete a nullpointer).
Thanks for the help, managed to figure it out now. :)
In your destructor, call delete[] on your list, but only if it isn't null (you don't want to delete a nullpointer).
operator delete actually handles the nullptr itself, so an explicit check is unnecessary.
Last edited on
Hi there. I thought I had it all working and it does work with what I have. But I need to use my print function to print out the values of everything. How can I access what I need from the print function?

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
#include "stdafx.h"
#include "RandomArray.h"
#include <stdlib.h>
#include <iostream>
#include <ctime>

RandomArray::RandomArray()
{
	size = 0;
	list = nullptr;

}




void RandomArray::generate(int n)
{
	if (!list && n > 0)
	{
		list = new int [n] {0};
		srand(time(NULL));
		for (int i = 0; i < n; i++)
		{
			list[i] = rand() % 101;
			std::cout << list[i] << std::endl;
		}
	}
	size = n;
	
}


void RandomArray::print()
{
	std::cout << "Average of array: ";
	std::cout << "Minimum Array value: ";
	std::cout << "maximum Array value: ";
}


void RandomArray::getAverage()
{
	int sum = 0;
	for (int i = 0; i < size; i++)
	{
		sum += list[i];
	}
	sum = sum / size;
	std::cout << sum << std::endl;
}


void RandomArray::getMin()
{
	int min;

	min = list[1];	//Sets the minimum as the first current as nothing will be smaller than 0

	for (int i = 0; i < size; i++)	//Cycles through the array 
	{
		if (list[i] < min)	//Compares if the the current array value is smaller than the current minimum
		{
			min = list[i];	//Make the current array value min
		}
	}
	std::cout << "Minimum is: " << min << std::endl;
}


void RandomArray::getMax()
{
	int max;
	max = list[1];	//Sets the minimum as the first current as nothing will be smaller than 0

	for (int i = 0; i < size; i++)	//Cycles through the array 
	{
		if (list[i] > max)	//Compares if the the current array value is smaller than the current minimum
		{
			max = list[i];	//Make the current array value min
		}
	}
	std::cout << "Minimum is: " << max << std::endl;
}


RandomArray::~RandomArray()
{
	if (list != nullptr)
	{
		delete list;
		std::cout << "List successfully deleted " << std::endl;
	}
}
You could have getAverage() return double (not int since average of int's could well be double, so change the variable sum to double and cast the numerator to double before dividing by size), and getMax(), getMin() to return int's instead of void as all these function do currently
Unfortunately I have to keep the functions all as void the way they have been set up. It might mean changing quite a bit of the code, but I need to figure out how to get it done the way it is.
Can you use reference?

1
2
3
4
5
6
7
8
9
10
11
void RandomArray::getAverage(double &average)
{
	int sum = 0;
	for (int i = 0; i < size; i++)
	{
		sum += list[i];
	}
        average = (double)sum / size;

	std::cout << average << std::endl;
}
Last edited on
Topic archived. No new replies allowed.