How member function

E

erictsn

Anyone can tell me the how to do the function member "Normalize"?

#include "Money.h"
//default constructor with no arg
MoneyType::MoneyType(){
dollars = 0;
cents = 0;
}

//function member MoneyType
MoneyType::MoneyType(long newDollars, long newCents){
// dollars is set to newDollars; cents is set to newCents.
dollars = newDollars;
cents = newCents;
}

long MoneyType::DollarsAre(){
// class member dollars is returned.
return dollars;
}

long MoneyType::CentsAre(){
// class member Cents is returned.
return cents;
}

MoneyType MoneyType::Add(const MoneyType &value){
// both operands have been initialized.
// value + self is returned.
MoneyType result;
result.cents = cents+value.cents;
result.dollars = dollars+value.dollars;
return result;
}

void MoneyType::Normalize(const myCents &value){
// To implement this member function
// To normalize the cents value the cents to between 0 and 99 and

}
 
K

Karl Heinz Buchegger

Anyone can tell me the how to do the function member "Normalize"?

How would you do it with paper and pencil?

Say I have 3 dollars and 358 cents. What is that in a normalized form?
What steps did you take? (Hint the answer is: 6 dollars and 58 cents)

The thing is: I don't give you a programming solution because you
need to learn one thing: It is absolutely impossible to write
a program to do something when you are unable to do the very same
just with paper and pencil. So first of all you need to know what
it is that you want to do. Then you need to figure out a way how
to do it. Only if you know all of this you are ready to write
a program.
 
J

Jim Langston

Anyone can tell me the how to do the function member "Normalize"?

#include "Money.h"
//default constructor with no arg
MoneyType::MoneyType(){
dollars = 0;
cents = 0;
}

//function member MoneyType
MoneyType::MoneyType(long newDollars, long newCents){
// dollars is set to newDollars; cents is set to newCents.
dollars = newDollars;
cents = newCents;
}

long MoneyType::DollarsAre(){
// class member dollars is returned.
return dollars;
}

long MoneyType::CentsAre(){
// class member Cents is returned.
return cents;
}

MoneyType MoneyType::Add(const MoneyType &value){
// both operands have been initialized.
// value + self is returned.
MoneyType result;
result.cents = cents+value.cents;
result.dollars = dollars+value.dollars;
return result;
}

void MoneyType::Normalize(const myCents &value){
// To implement this member function
// To normalize the cents value the cents to between 0 and 99 and

}

Well, it really depends on what you want to do in Normalize. It's just a
math problem really. Do you want to take 235 cents and return 2 dollars and
35 cents? Since you aren't returning a value it's gonna be hard to do this.

First you gotta figure out how you're gonna use Normalize, what's it
supposed to return? What is the function actually need to do? Once you
figure that out, I'm sure the solution will be simple to you.
 
G

Greg

Anyone can tell me the how to do the function member "Normalize"?

#include "Money.h"
//default constructor with no arg
MoneyType::MoneyType(){
dollars = 0;
cents = 0;
}

//function member MoneyType
MoneyType::MoneyType(long newDollars, long newCents){
// dollars is set to newDollars; cents is set to newCents.
dollars = newDollars;
cents = newCents;
}

long MoneyType::DollarsAre(){
// class member dollars is returned.
return dollars;
}

long MoneyType::CentsAre(){
// class member Cents is returned.
return cents;
}

MoneyType MoneyType::Add(const MoneyType &value){
// both operands have been initialized.
// value + self is returned.
MoneyType result;
result.cents = cents+value.cents;
result.dollars = dollars+value.dollars;
return result;
}

void MoneyType::Normalize(const myCents &value){
// To implement this member function
// To normalize the cents value the cents to between 0 and 99 and

}

When a format can express a single value in more than one way, it's
standard practice to "normalize" or "canonicalize" the representation
of value (that is, provide some means to know which representation to
use, so that all instances use the same format).

For values expressed in different-sized units, here is the usual
normalization procedure:

Start with the largest unit and progressing toward the smallest, assign
each unit the largest possible value that it could contain while still
being able to represent the normailized value. (hint divide the
normalized value by the largest unit, take the remainder and divide it
by the next largest unit, take that remainder and...)

Greg
 
A

Amazing

void MoneyType::Normalize(){
// To implement this member function
// To normalize the cents value the cents to between 0 and 99 and

}
This is an original code; I need to do that is how to round off the
cents. For example $15.157 will be $15.16 or $16.1235 will be $16.12.
This is the member function by main.cpp. In main.cpp it just call void
MoneyType::Normalize();.
I can't find any info from web it talk the Type long about round off
decimal.

Thx
 
K

Karl Heinz Buchegger

Amazing said:
void MoneyType::Normalize(){
// To implement this member function
// To normalize the cents value the cents to between 0 and 99 and

}
This is an original code; I need to do that is how to round off the
cents. For example $15.157 will be $15.16 or $16.1235 will be $16.12.
This is the member function by main.cpp. In main.cpp it just call void
MoneyType::Normalize();.
I can't find any info from web it talk the Type long about round off
decimal.

I think you misunderstodd what the assignment is asking you.
Since Normalize() takes no arguments, it is a safe bet, that it should
work on the object it is called for.
eg.

int main()
{
MoneyType Test( 3, 465 );

cout << "$" << Test.DollarsAre() << "." Test.CentsAre() << "\n";
//
// Output is: $3.465
// But this is misleading, since there is a difference between
// 3 dollars and 465 cents, and 3.465 dollars

// Now normalize is called
// which normalizes the cents into a range of 0 to 99 just
// as explained in the comment
Test.Normalize();

//
// and now the output becomes ...
//
cout << "$" << Test.DollarsAre() << "." Test.CentsAre() << "\n";

// ... $7.65 which is correct, since 3 dollars and 465 cents sum
// up to a total of 7.65
}

That is what Normalize is asking you for, it has nothing to do with
rounding (especially since no floating point data types are involved
in the whole program, so with the exception of division, rouding is
a non issue).
 
J

Jim Langston

Amazing said:
void MoneyType::Normalize(){
// To implement this member function
// To normalize the cents value the cents to between 0 and 99 and

}
This is an original code; I need to do that is how to round off the
cents. For example $15.157 will be $15.16 or $16.1235 will be $16.12.
This is the member function by main.cpp. In main.cpp it just call void
MoneyType::Normalize();.
I can't find any info from web it talk the Type long about round off
decimal.

Thx

Ahh, you dont' want to round. You don't really have $15.157, you have 15
dollars and 157 cents, which is 16 dollars and 57 cents or $16.57

Divide cents by 100 (integer math). I.E. 157 / 100 = 1 (.57 is lost in
integer math). Add that 1 to your dollars. Subtract 100 cents for every
dollar you added. I usually use a temporary variable for this. I store in
the temporary variable the result of the division, then multiply it by 100
and subtract that from the cents. 3 lines of code. I'd give you the code
but this is homework.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,252
Latest member
MeredithPl

Latest Threads

Top