12 Replies - 1841 Views - Last Post: 07 February 2009 - 07:52 PM Rate Topic: -----

#1 Vanessa6   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 30-January 09

coding for output

Post icon  Posted 06 February 2009 - 07:27 PM

I am working on a homework assignment where we are learning Operator overloading. I am supposed to use the overloaded + operator to find the average score and time for each player and print out which racer had the lowest total time and which performer had the highest total score.

I think I have the functions right but I can't figure out how to get the average or lowest time and highest total score. I'd appreciate any hints.


#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;



class RacingData
{
public:
	RacingData();
	RacingData(string rn, int sc, int min, int sec);
	RacingData operator+(const RacingData& a);
	int score, minutes, seconds;
	string racerName;
	void printRacingData();
	friend ostream& operator << (ostream& theStream, RacingData& raceInfo);
};

RacingData::RacingData(string rn, int sc, int min, int sec)
{
	racerName = rn;
	score = sc;
	minutes = min;
	seconds = sec;
}

RacingData::RacingData()
{
	racerName = " ";
	score = 0;
	minutes = 0;
	seconds = 0;
}
void RacingData::printRacingData()
{
	cout << racerName << "	 " << score << "	" << minutes << ":" << seconds << "\n";
}

RacingData RacingData::operator +(const RacingData &a)
{
	RacingData obj = RacingData();

	if (racerName == a.racerName)
	{
		obj.racerName = racerName;
	}

	obj.score = score + a.score;
	obj.minutes = minutes + a.minutes;
	obj.seconds = seconds + a.seconds;

	if (obj.seconds >= 60)
	{
		obj.minutes += obj.seconds / 60;
		obj.seconds = obj.seconds % 60;
	}

	return obj;

	

	

}

int main()
{
	RacingData dPRace1("Danica Patrick", 185, 11, 20);
	RacingData dPRace2("Danica Patrick", 103, 11, 30);
	RacingData dPRace3("Danica Patrick", 73, 12, 40);
	RacingData dPRace4;

	

	RacingData jGRace1("Jeff Gordon", 155, 10, 10);
	RacingData jGRace2("Jeff Gordon", 127, 11, 15);
	RacingData jGRace3("Jeff Gordon", 34, 12, 35);
	RacingData jGRace4;

	  

	cout << "Average Scores:\n";
	
	dPRace4 = dPRace1 + dPRace2 + dPRace3;
	jGRace4 = jGRace1 + jGRace2 + jGRace3;

	dPRace4.printRacingData();
	jGRace4.printRacingData();
	
	int i;
	cin>>i;
	return 0;
}


The way I have it set now I am able to print the totals for each racer, but thats not what I need.

Is This A Good Question/Topic? 0
  • +

Replies To: coding for output

#2 thepeoplescoder   User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 97
  • Joined: 02-February 09

Re: coding for output

Posted 06 February 2009 - 08:30 PM

Okay, looking at your code, this is what I propose.

We all know that the average is calculated like so:

average = (x1 + x2 + x3 + ... + xN) / number_of_terms_in_the_sum



First, add an integer variable to your class. Make it private, and call it number_of_terms. In your constructors, you want to ensure that this variable is set to one.

Now, let me propose this to you. Suppose we have these:

Three samples: sum1 = x1 + x2 + x3

Four samples: sum2 = x4 + x5 + x6 + x7

Would you agree that the average of sum1 is equal to sum1 / 3?
Would you agree that the average of sum2 is equal to sum2 / 4?

The key numbers here are 3 and 4. sum1 contains 3 terms, sum2 contains 4 terms.

Clearly, the following sum contains 7 terms.

x1 + x2 + x3 + x4 + x5 + x6 + x7
For kicks, I'll call this sumXs.

Which could also be written as sum1 + sum2.

Therefore, to get the average of sumXs, we would divide it by 7, since there are 7 terms. Alternatively, the average is equivalent (sum1 + sum2) / 7.

Alright, so what does this have to do with your project? Remember that number_of_terms variable I was talking about? In your constructors, that variable is going to be initialized to one. In the addition operator, you're going to add together each number_of_terms variable, so you can keep track of the number of terms to be averaged.

Oh wait, now that you have the number of terms, you can now calculate the average!

I know there weren't any code samples, but hopefully, I explained it well enough for you to fill in the blanks, since we're not allowed to do anyone's homework for them.
Was This Post Helpful? 1
  • +
  • -

#3 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: coding for output

Posted 06 February 2009 - 08:36 PM

Add another member variable eventCount to your RacingData class and increment it each time results are added. Then you can calculate average time and average score at any given time regardless of how many events were run just by dividing the racer's score or total time by the eventCount.

Why do you bother with member variable minutes? It's easier to keep the total time in seconds and calculate minutes only when printing.

Are you sure you're supposed to use the + operator to determine which player had the best scores? That would be a pretty bizarre use of +! Why not simply compare the scores and times of the 2 players after their individual results have been combined?
Was This Post Helpful? 1
  • +
  • -

#4 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: coding for output

Posted 06 February 2009 - 08:52 PM

PS: in
RacingData RacingData::operator +(const RacingData &a)
{
	RacingData obj = RacingData();


why not just:
	RacingData obj;
?
Was This Post Helpful? 1
  • +
  • -

#5 thepeoplescoder   User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 97
  • Joined: 02-February 09

Re: coding for output

Posted 06 February 2009 - 09:04 PM

Good point, you're absolutely right about that one. I try not to be too critical on other people's code, unless it's severely screwed. However, yours isn't.

But he is right.

However, how long have you been programming? Or is this strictly a major requirement you're trying to fulfill?

Either way, happy coding!
Was This Post Helpful? 0
  • +
  • -

#6 Plus   User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

Re: coding for output

Posted 07 February 2009 - 12:23 AM

your operator+ function is 360 degree in the wrong way ...
i've corrected it,

RacingData& RacingData::operator +(const RacingData &a){
	score += a.score;
	minute += a.minutes;
	seconds += a.seconds;
	return *this; // can be used multiple time
}

// can be used like this: A +B +C +D; -> (A = A+B+C+D)


This post has been edited by Plus: 07 February 2009 - 12:27 AM

Was This Post Helpful? 0
  • +
  • -

#7 Vanessa6   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 30-January 09

Re: coding for output

Posted 07 February 2009 - 08:36 AM

View Postthepeoplescoder, on 6 Feb, 2009 - 08:04 PM, said:

Good point, you're absolutely right about that one. I try not to be too critical on other people's code, unless it's severely screwed. However, yours isn't.

But he is right.

However, how long have you been programming? Or is this strictly a major requirement you're trying to fulfill?

Either way, happy coding!



This class is my first attempt at programming and it's only the 5th week so I wouldn't be surprised to find out that my coding was totally screwed. This class is a requirement for my major and it leads to further classes in programming, so I'm not just trying to get through the class, I am determined to learn and understand this. I really appreciate the help. For now I'm going to go and try to apply the changes you suggested and see what I can figure out, I'm sure I'll have more questions.
Was This Post Helpful? 0
  • +
  • -

#8 Vanessa6   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 30-January 09

Re: coding for output

Posted 07 February 2009 - 08:41 AM

View Postr.stiltskin, on 6 Feb, 2009 - 07:52 PM, said:

PS: in
RacingData RacingData::operator +(const RacingData &a)
{
	RacingData obj = RacingData();


why not just:
	RacingData obj;
?


The only reason I wrote the code that way is because my professor wrote it that way in an example video. I'd be happy to try it your way. Right now I'm just doing my best to understand how to write the code and why I'm writting it the way I am.
Was This Post Helpful? 0
  • +
  • -

#9 thepeoplescoder   User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 97
  • Joined: 02-February 09

Re: coding for output

Posted 07 February 2009 - 11:08 AM

View PostVanessa6, on 7 Feb, 2009 - 07:36 AM, said:

This class is my first attempt at programming and it's only the 5th week so I wouldn't be surprised to find out that my coding was totally screwed. This class is a requirement for my major and it leads to further classes in programming, so I'm not just trying to get through the class, I am determined to learn and understand this. I really appreciate the help. For now I'm going to go and try to apply the changes you suggested and see what I can figure out, I'm sure I'll have more questions.


Wow, you guys are already getting into object oriented programming and operator overloading?? Your class must go pretty fast. Well, if you need any help, don't hesitate to ask or shoot me a private message. If you're confused on how or why something works, just let me know, maybe I can try and explain it for you. The good thing about programming is when you understand it, you totally understand it. xD
Was This Post Helpful? 0
  • +
  • -

#10 Vanessa6   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 30-January 09

Re: coding for output

Posted 07 February 2009 - 11:21 AM

View Postthepeoplescoder, on 7 Feb, 2009 - 10:08 AM, said:

View PostVanessa6, on 7 Feb, 2009 - 07:36 AM, said:

This class is my first attempt at programming and it's only the 5th week so I wouldn't be surprised to find out that my coding was totally screwed. This class is a requirement for my major and it leads to further classes in programming, so I'm not just trying to get through the class, I am determined to learn and understand this. I really appreciate the help. For now I'm going to go and try to apply the changes you suggested and see what I can figure out, I'm sure I'll have more questions.


Wow, you guys are already getting into object oriented programming and operator overloading?? Your class must go pretty fast. Well, if you need any help, don't hesitate to ask or shoot me a private message. If you're confused on how or why something works, just let me know, maybe I can try and explain it for you. The good thing about programming is when you understand it, you totally understand it. xD



The class Im in is Object Oriented Programming using C++ and it's an excellerated class so it only lasts 8 weeks. I'm starting to think that there should have been a pre-requisite of programming knowledge. Thanks to all the help here (DIC) I know I'll be able to learn this. Now I'm off to learn how to do the if statement.
Was This Post Helpful? 0
  • +
  • -

#11 thepeoplescoder   User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 97
  • Joined: 02-February 09

Re: coding for output

Posted 07 February 2009 - 03:37 PM

View PostVanessa6, on 7 Feb, 2009 - 10:21 AM, said:

The class Im in is Object Oriented Programming using C++ and it's an excellerated class so it only lasts 8 weeks. I'm starting to think that there should have been a pre-requisite of programming knowledge. Thanks to all the help here (DIC) I know I'll be able to learn this. Now I'm off to learn how to do the if statement.


My advice in regards to that, in order to make them easier to understand, read them in plain English.

That basically applies to all conditionals. Don't worry about how things evaluate in the boolean world just yet; if you learn how to read them in plain English, you'll do just fine, and everything else will follow.
Was This Post Helpful? 0
  • +
  • -

#12 thepeoplescoder   User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 97
  • Joined: 02-February 09

Re: coding for output

Posted 07 February 2009 - 03:43 PM

View PostPlus, on 6 Feb, 2009 - 11:23 PM, said:

your operator+ function is 360 degree in the wrong way ...
i've corrected it,

RacingData& RacingData::operator +(const RacingData &a){
	score += a.score;
	minute += a.minutes;
	seconds += a.seconds;
	return *this; // can be used multiple time
}

// can be used like this: A +B +C +D; -> (A = A+B+C+D)



The great thing about programming is that there are no right or wrong answers, if the code does what you have intended, however, Plus, if that's what you want, then that's fine.

No offense, but my only problem is by doing it that way, you're updating the variable on the left side of the + operator, which may not be what someone wants. I mean, consider the following code.

#include <iostream>

using namespace std;

int main(void)
{
   int a, b, result;

   a = 3;
   b = 4;

   // As a result of this computation, do we really want the variable a to contain 7?
   // That's what you're implying in your redefinition of the + operator.
   //
   // Also, that's the reason behind the convention of a temporary variable when
   // overloading the +, -, *, or / operators.
   result = a + b;

   cout << "a = " << a << endl;
   cout << "b = " << b << endl;
   cout << "result = " << result << endl;

   return 0;
}


This post has been edited by thepeoplescoder: 07 February 2009 - 03:43 PM

Was This Post Helpful? 1
  • +
  • -

#13 Vanessa6   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 30-January 09

Re: coding for output

Posted 07 February 2009 - 07:52 PM

View Postthepeoplescoder, on 7 Feb, 2009 - 02:43 PM, said:

View PostPlus, on 6 Feb, 2009 - 11:23 PM, said:

your operator+ function is 360 degree in the wrong way ...
i've corrected it,

RacingData& RacingData::operator +(const RacingData &a){
	score += a.score;
	minute += a.minutes;
	seconds += a.seconds;
	return *this; // can be used multiple time
}

// can be used like this: A +B +C +D; -> (A = A+B+C+D)



The great thing about programming is that there are no right or wrong answers, if the code does what you have intended, however, Plus, if that's what you want, then that's fine.

No offense, but my only problem is by doing it that way, you're updating the variable on the left side of the + operator, which may not be what someone wants. I mean, consider the following code.

#include <iostream>

using namespace std;

int main(void)
{
   int a, b, result;

   a = 3;
   b = 4;

   // As a result of this computation, do we really want the variable a to contain 7?
   // That's what you're implying in your redefinition of the + operator.
   //
   // Also, that's the reason behind the convention of a temporary variable when
   // overloading the +, -, *, or / operators.
   result = a + b;

   cout << "a = " << a << endl;
   cout << "b = " << b << endl;
   cout << "result = " << result << endl;

   return 0;
}




I think I can see what you are saying. I already finished my project and uploaded it to my schools dropbox but I'm going to go back in the program and do the changes that you propose, just so that I can learn all that you have tried to teach me. I wish I could express to you how much I truely appreciate the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1