C++ Sum multiple group to find the max

shyy

Well-known Member
Joined
Nov 6, 2008
Messages
1,484
Hey everyone,

Does anyone know how to sum groups in C++? I need to find the group with the highest amount. I can find the max when it comes to finding a set of individual numbers but now when a group is required. For Ex:

[TABLE="width: 128"]
[TR]
[TD="width: 64"]Group A[/TD]
[TD="width: 64, align: right"]64[/TD]
[/TR]
[TR]
[TD]Group A[/TD]
[TD="align: right"]48[/TD]
[/TR]
[TR]
[TD]Group A[/TD]
[TD="align: right"]18[/TD]
[/TR]
[TR]
[TD]Group B[/TD]
[TD="align: right"]49[/TD]
[/TR]
[TR]
[TD]Group B[/TD]
[TD="align: right"]36[/TD]
[/TR]
[TR]
[TD]Group C[/TD]
[TD="align: right"]64[/TD]
[/TR]
[TR]
[TD]Group C[/TD]
[TD="align: right"]75[/TD]
[/TR]
[TR]
[TD]Group C[/TD]
[TD="align: right"]87[/TD]
[/TR]
[TR]
[TD]Group C[/TD]
[TD="align: right"]72[/TD]
[/TR]
[/TABLE]

The group with the highest amount is GROUP C

Thanks
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hi,
the question cannot be answered without knowing how the data is stored. If this is an array or data structure you would need to write a an algorithm to sum the groups, then sort by amount and take the highest one. Or something along those lines.
ξ
 
Upvote 0
Hi Xenou,

I used a class structure. This is the code I used to find the max value within a list of one set of numbers. Problem is how do I get this to group by the television shows to see which show grossed the most.

Rich (BB code):
string findMax(Agency Talents[], int limit, string show)
{
	double maxPrice = Talents[0].pay;
	string maxTalent;


	int maxIndex = 0;


	for (int i = 0; i < limit; i++)
		if(Talents.job == show)
		{
			double currentPrice = Talents.pay;


			if( currentPrice > maxPrice)
			{
				maxPrice = currentPrice;
				maxIndex = i;
				maxTalent = Talents.talent;;
			}


		}
		return maxTalent;
}
 
Upvote 0
That's beyond my expertise. If I was doing this in VBA i'd create a dictionary with the groups, then iterate the array to sum up the totals, accumulating them in the dictionary. I'm sure C++ has similar data structures but offhand I don't know how to create such a thing in C++. To write a very simplistic algorithm without the use of key-value pairs, you could instead do the following:

Create a new array of unique groups (this will be ugly if all you do is use loops and your original array but it can be done). Now iterate your new array, and within that have an inner loop where you iterate the original array to accumulate the totals. But this seems like an ugly solution that would be best left undone. Better to learn how to use more efficient data structures in C.
 
Upvote 0
Thanks for the suggestion, I will post the answer when I figure the solution.
 
Upvote 0
The answer :)

Rich (BB code):
void findMaxJob(Agency Talents[], int limit)
{
	system("CLS");


	string curJob = Talents[0].job;
	string maxJob;
	double maxTotoal = 0;
	double curTotal = 0;


	for (int i = 0; i < limit; i++)
	{
		if(Talents.job == curJob)
		{
			curTotal += Talents.pay;
		}	
		else 
		{
			if (curTotal > maxTotoal) 
			{
				maxTotoal = curTotal;
				maxJob = curJob;
			}


			curTotal = Talents.pay;
			curJob = Talents.job;
		}


		if (curTotal > maxTotoal)
			maxJob = curJob;
	}
	cout << "Television Show with the highest grossed amount is: " << maxJob << endl;


	system("PAUSE");
	system("CLS");
}
 
Upvote 0
Very pretty. I'm glad you did this instead of me - I was thinking of something a bit clumsier, to tell the truth. :)
 
Upvote 0

Forum statistics

Threads
1,225,662
Messages
6,186,290
Members
453,348
Latest member
newbieBA

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top