473,408 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

How member function

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

}

Oct 19 '05 #1
6 1901
er*****@gmail.com wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Oct 19 '05 #2
<er*****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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.
Oct 19 '05 #3

er*****@gmail.com wrote:
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

Oct 19 '05 #4
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

Oct 19 '05 #5
Amazing wrote:

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).

--
Karl Heinz Buchegger
kb******@gascad.at
Oct 20 '05 #6

"Amazing" <er*****@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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.
Oct 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Wenjie | last post by:
Hello, I read someone posted assertions that even the (public) member function is not static, there are probably only one copy of the code in the executable. Then except the...
5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
3
by: Roy Yao | last post by:
Hello, I need to pass a pointer to a callback function to the lower level modules. But the function is thought to be a virtual member one. How can I get the real address of the virtual...
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
7
by: jon wayne | last post by:
Hi I'm a little confused here about the lifetime of a static pointer to member function, Say, I declare,define & initialize a static ptr to mem function in the header file of a class(the class...
6
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the...
13
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member...
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.