I have this code:

void ShapeArr::publishStats()
{
	double ap = 0.0;

	for (int i=0; i < currCount; i++)
	{
		Shape *s = arr[i];
		ap += s->getAPRatio();
	}
	cout << "Max APRatio = " << [B]???[/B] << "	Average APRatio = " << ap/currCount << endl;
}

What do I need to do to actually calculate the MAX APRatio? "getAPRatio" is being called 3-4 times and returning a single integer each time. Of the 3 or 4 times it's called, I need to somehow compare them and take the biggest one as my max. I think the average is set correctly, but I'm not sure how to separate them and compare them to get the max. Any help??

Dani AI

Generated

A few points that tie the existing replies together and fill the gaps.

You do not need to store every APRatio just to find the maximum. Walk the array once, call getAPRatio() once per shape (cache the result into a local double), accumulate the sum for the average, and update a running maximum. Handle the empty-array case explicitly. Avoid fixed-size buffers like tempArr[5] — that will break when currCount is larger than the buffer.

Example (compute max+average in one pass):

double maxAP = std::numeric_limits<double>::lowest();
double sum = 0.0;

for (int i = 0; i < currCount; ++i) {
    Shape* s = arr[i];
    double v = s->getAPRatio();   // call once, reuse
    sum += v;
    if (v > maxAP) maxAP = v;
}

if (currCount > 0)
    cout << "Max APRatio = " << maxAP << "  Average APRatio = " << sum / currCount << endl;
else
    cout << "No shapes to report\n";

On deleting the newed shapes: if arr is an array of Shape* (and you delete[] arr in the destructor), you must first delete each object pointed to, then delete the pointer array itself. Do not just delete[] arr — that frees the pointer block but leaks the objects.

ShapeArr::~ShapeArr()
{
    for (int i = 0; i < currCount; ++i)
        delete arr[i];    // free each concrete shape
    delete[] arr;         // free the array of pointers
}

Modern advice: prefer RAII. Use std::vector<std::unique_ptr<Shape>> (C++11+) or std::vector<std::shared_ptr<Shape>> if shared ownership is needed. That removes manual delete and makes code safer. That also avoids the need for temporary arrays as suggested; 's point about using a loop is correct; and , avoid fixed-size temp arrays and call getAPRatio() once per element.

Recommended Answers

All 6 Replies

put them in an array then find the max after the loop stops.

Then I can't ;get the average though because "ap/currCount" won't work if ap is an array...??

Whenever you have an array, you need to be thinking about using a for loop as well.

Ok, I think I got it...:

double Max(const double *Numbers, const int Count)
{
	double Maximum = Numbers[0];

	for(int i = 0; i < Count; i++)
		if( Maximum < Numbers[i] )
			Maximum = Numbers[i];

	return Maximum;
}

void ShapeArr::publishStats()
{
	double ap = 0.0;
	double tempArr[5];
	double x = 0.0;
	for (int i=0; i < currCount; i++)
	{
		Shape *s = arr[i];
		ap = s->getAPRatio();
		x += s->getAPRatio();
		tempArr[i] = ap;
	}

	double Maximum = Max(tempArr, currCount);
	cout << "Max APRatio = " << Maximum << "	";
	cout << "	Average APRatio = " << x/currCount << endl;
}

Now in my add funcs, I declare new shapes from classes...How do i destruct them/free up the memory of those "new's"??

Post deleted, didn't check my internal delay to follow up on posts posted while I was writing mine.

Sounds good, but I still need help with how to delete the memory I allocated for all the shapes I called "NEW". I delete "arr" in the shapearr destructor, but how do I properly delete the memory I allocated by the new Rectangle, new Circle, new Triangle, and new RegularPoly in their respective add functions? HELP!?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.