C++ Sum multiple group to find the max
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: C++ Sum multiple group to find the max

  1. #1
    Join Date
    Nov 2013
    Posts
    21

    C++ Sum multiple group to find the max

    Hey everyone,

    Does anyone know how to sum groups in C++? I have a class with 2 attributes (Group and Price). 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 not when a group is required. For Ex:

    Group A 64
    Group A 48
    Group A 18
    Group B 49
    Group B 36
    Group C 64
    Group C 75
    Group C 87
    Group C 72

    The group with the highest amount is GROUP C

    Thanks

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,262

    Re: C++ Sum multiple group to find the max

    What's the problem? What can't you figure out? Are the groups always in sorted order? If yes, you need a total for the highest group so far and its name and if the current group is higher, this becomes the highest group. So at the end you know the highest total and its name. If the groups are not yet in sorted order, then you can either sort first and then proceed as above, or maintain a map of group names and runing total.

    If you post the code you have, we'll be able to advise further.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Nov 2013
    Posts
    21

    Re: C++ Sum multiple group to find the max

    Thanks, the data is actually sorted. The code I used is to find the max within a set of individual numbers but I am not sure how to do this if groups are involved. This code works if I have the user input the television show. By group I mean to find the max between the television shows that grossed the most.

    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[i].job == show)
    		{
    			double currentPrice = Talents[i].pay;
    
    
    			if( currentPrice > maxPrice)
    			{
    				maxPrice = currentPrice;
    				maxIndex = i;
    				maxTalent = Talents[i].talent;;
    			}
    
    
    		}
    		return maxTalent;
    }
    The data looks like
    Code:
    Ali Frasier 1500.00
    Alice Frasier 800.00
    Rob Frasier 2300.00
    John Frasier 1800.00
    David Frasier 500.00
    Ali Cheers 1000.00
    Alice Cheers 1800.00
    Rob Cheers 2000.00
    John Cheers 800.00
    David Cheers 500.00
    Ali Seinfeld 800.00
    Alice Seinfeld 3800.00
    Rob Seinfeld 1000.00
    John Seinfeld 850.00
    David Seinfeld 1000.00
    Last edited by shyy; December 17th, 2013 at 10:50 AM.

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,262

    Re: C++ Sum multiple group to find the max

    Ok. Assuming that you want the show with the highest total, then you keep a variable with the current show name and a running total for that show. When you iterate through talents, when the show name changes then you have the total for the show. If greater than current max, save data. Then reset show name and current total and resume talent iteration.

    Is this a homework assignment?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Nov 2013
    Posts
    21

    Re: C++ Sum multiple group to find the max

    I have a final on Saturday, but this example was from a previous final. Trying to study for this. We never had this type of question where we dealt with groups. This function doesn't ask for any user input. Just displays the highest grossed job (job is televison show)

    This is the code I currently have which probably doesn;t make sense

    Code:
    void findMaxJob(Agency Talents[], int limit)
    {
    	system("CLS");
    	double sum1 = 0;
    	double sum2 = 0;
    	int Index = 0;
    
    	for (int i = 0; i < limit; i++)
    	{
    		if(Talents[i].job == Talents[i+1].job)
    		{
    			sum1 += Talents[i].pay;
    		}		
    
    		else if(Talents[i].job != Talents[i+1].job)
    		{
    			sum2 += Talents[i].pay;
    		}
    		
    		if(sum1 > sum2)
    		{
    				sum1 = sum2;
    				Index = i;
    		}
    	}
    		cout << sum1;
    
    		system("PAUSE");
    }

  6. #6
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,262

    Re: C++ Sum multiple group to find the max

    if(Talents[i].job == Talents[i+1].job)
    No. As i+1 is beyond the end of the array.

    Assuming job is a string, then you have a local variable called something like curjob and set it to array 0. When you iterate through the array, keep totalling while job is same as curjob. When it's not then check for greatest total etc and reset curjob to the new job name.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  7. #7
    Join Date
    Nov 2013
    Posts
    21

    Re: C++ Sum multiple group to find the max

    Not sure what you mean, I did this

    Code:
    void findMaxJob(Agency Talents[], int limit)
    {
    	system("CLS");
    
    	string curJob = Talents[0].job;
    	double sum1 = 0;
    	double sum2 = 0;
    	int Index = 0;
    
    
    	for (int i = 0; i < limit; i++)
    	{
    		if(Talents[i].job == curJob)
    		{
    			sum1 += Talents[i].pay;
    		}	
    		else if (Talents[i].job != curJob)
    		{
    			sum2 += Talents[i].pay;
    		}
    
    		if(sum1 > sum2)
    		{
    			sum1 = sum2;
    			Index = i;
    		}
    	}
    
    	cout << sum1;
    
    	system("PAUSE");
    }

  8. #8
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,262

    Re: C++ Sum multiple group to find the max

    As you're revising for your final and this is just an example and its the season of good will, have a look at this

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    struct Agency {
    	string name;
    	string job;
    	double pay;
    };
    
    const string iname = "talent.txt";
    
    const int MAXSIZE = 100;
    
    string findMaxJob(const Agency Talents[], const int limit);
    
    int main()
    {
    Agency talents[MAXSIZE];
    
    int size;
    
    	ifstream inf(iname.c_str());
    
    	if (!inf.is_open()) {
    		cout << "Cannot open input file: " << iname << endl;
    		return 1;
    	}
    
    	for (size = 0; size < MAXSIZE && (inf >> talents[size].name >> talents[size].job >> talents[size].pay); size++);
    
    	cout << "Best show is " << findMaxJob(talents, size);
    	return 0;
    }
    
    string findMaxJob(const Agency Talents[], const int limit)
    {
    string curjob = Talents[0].job;
    string maxjob;
    
    double maxtot = 0.0;
    double curtot = 0.0;
    
    	for (int i = 0; i < limit; i++) {
    		if (Talents[i].job == curjob) {
    			curtot += Talents[i].pay;
    		} else {
    			if (curtot > maxtot) {
    				maxtot = curtot;
    				maxjob = curjob;
    			}
    			curtot = Talents[i].pay;
    			curjob = Talents[i].job;
    		}
    	}
    
    	if (curtot > maxtot)
    		maxjob = curjob;
    
    	return maxjob;
    }
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  9. #9
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,010

    Re: C++ Sum multiple group to find the max

    Quote Originally Posted by shyy View Post
    Does anyone know how to sum groups in C++? I have a class with 2 attributes (Group and Price). 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 not when a group is required.
    The easiest way to solve this is to keep a std::unordered_map<std::string, int> in which you keep track of the running total (or maximum, isn't clear to me which one you need) of each group while you iterator through all items. After that you run through your map and find the highest value.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  10. #10
    Join Date
    Nov 2013
    Posts
    21

    Re: C++ Sum multiple group to find the max

    Thanks so much! it worked!!

    D-Drmmr, we havn't learned unordered_map<std::string, int> this in class.

  11. #11
    Join Date
    Nov 2013
    Posts
    21

    Re: C++ Sum multiple group to find the max

    I have another question, more to deal with the syntax and structure of a function, should I open a new thread?

    I have the function and it works just need to clean it up for better presentation.

  12. #12
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,262

    Re: C++ Sum multiple group to find the max

    Quote Originally Posted by shyy View Post
    I have another question, more to deal with the syntax and structure of a function, should I open a new thread?

    I have the function and it works just need to clean it up for better presentation.
    If its a different question about a different function then it's probably better to start a new thread.

    By the way, if you are sitting a final exam on Saturday I would have expected the coding of a simple function like this to be fairly easy for you. The syntax of the statements used is not complex and you should have been able to work out the simple logic required. Programming is a lot more than knowing how to write some statements. The algorithms and design needed are as important, if not more so, than being able to produce some code. Given a situation like the one you presented here where the issue was logic and not code syntax, the first thing I would suggest is to produce a program design - ie write out (in English or other natural language - not a computer language) how you would do the problem yourself without a computer using pen & paper. From this design then code the program. As part of your course, have you not covered proram design with your tutor?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  13. #13
    Join Date
    Nov 2013
    Posts
    21

    Re: C++ Sum multiple group to find the max

    We always dealt with situations dealing with a set of numbers but never by group. We havn't really talk about designing of the program more life like situations. All those situations didn't deal with any grouping.

  14. #14
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,262

    Re: C++ Sum multiple group to find the max

    We havn't really talk about designing of the program
    What are these tutors teaching? Program design is fundamental to programming. Even us wizened old gurus who dream in code still produce written program designs before we start coding!

    Design->code->test->debug. Repeat sections as appropriate until program is 'correct'.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  15. #15
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,010

    Re: C++ Sum multiple group to find the max

    Quote Originally Posted by shyy View Post
    D-Drmmr, we havn't learned unordered_map<std::string, int> this in class.
    But you have learned about std::map, I hope? unordered_map was added to the standard library in 2003 (then in namespace std::tr1, since 2011 in namespace std) and should be available on any modern C++ compiler.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center