Help with function

P

Paul

Trying to write a function that does the following:

Joe gets 100 points for every 30 widgets he sells.

So if he sells less than thirty he gets nothing. If he gets between 30 and
59 he gets 100, 60-89 he gets 200, 90-119 he gets 300 etc.....

How can I display the points joe gets depending on then number of widgets
entered.
 
P

Paul

In anycase I know that if I divide the number of widgets by 30 and then
taking the whole
number of the result and multiply by 100 I will get it. Thought there might
be a cleaner way.
 
J

John Harrison

Paul said:
Nothing like asking for help and getting shot in the face. Disregard then.

No-one's shot you in the face (or even slapped you in the face). There are
three good reasons why you should post the code you've already written,

1) It helps everyone assess what skill you already have and so give advice
appropriate to your level of skill
2) It helps describe your problem and what exactly you are stuck on much
better than a prose description can ever do, therefore posters won't waste
time answering the wrong question.
3) It proves that you are prepared to make some effort yourself, and aren't
just expecting to be handed the answer.

These rules apply to everyone, no-one is picking on you. So, post the code
you've already written and you'll get good answers very quickly, and
everyone will be happy.

john
 
J

John Harrison

Paul said:
In anycase I know that if I divide the number of widgets by 30 and then
taking the whole
number of the result and multiply by 100 I will get it. Thought there might
be a cleaner way.

Sounds good to me. That's how I would do it.

john
 
H

Howard

Paul said:
In anycase I know that if I divide the number of widgets by 30 and then
taking the whole
number of the result and multiply by 100 I will get it. Thought there might
be a cleaner way.

That already sounds like the cleanest way to me!

int points( int widgets )
{
return 100 * ( widgets / 30 );
}

If widgets is an integer and the function returns an integer, then that
division already gives you the "whole number" part, so the above code should
be sufficient.

-Howard
 
J

John Harrison

John Harrison said:
Sounds good to me. That's how I would do it.

But note also, you don't need to take the whole number of the result. In C++
when you divide two integers the answer is always another integer.

int points_per_widget(int widgets)
{
return (widgets/30)*100;
}

john
 
P

Paul

Ah makes perfect sense now!

Thank you
Howard said:
That already sounds like the cleanest way to me!

int points( int widgets )
{
return 100 * ( widgets / 30 );
}

If widgets is an integer and the function returns an integer, then that
division already gives you the "whole number" part, so the above code should
be sufficient.

-Howard
 
D

Default User

Paul said:
Nothing like asking for help and getting shot in the face. Disregard then.


Are you looking to the third jackass to hit the killfile today? We don't
do homework, which if you'd taken a bare amount of time to find the
newsgroup FAQ and read you'd already know.

You have to show some sort of intiative, and posting your homework
assignment doesn't cut it.




Brian Rodenborn
 
T

Thomas Matthews

Paul said:
Trying to write a function that does the following:

Joe gets 100 points for every 30 widgets he sells.

So if he sells less than thirty he gets nothing. If he gets between 30 and
59 he gets 100, 60-89 he gets 200, 90-119 he gets 300 etc.....

How can I display the points joe gets depending on then number of widgets
entered.

Many people solving this problem or similar problems use a cascade
set of "if" statements:
unsigned int widgets_sold;
unsigned int points_earned;

//...
if (widgets_sold < 30)
{
points_earned = 0;
}
if ((widgets_sold >= 30) && (widgets_sold < 60))
{
points_earned = 100;
}
// Und so weite.

A switch statement is not an efficient approach since the case
statement requires an integral constant and doesn't handle
ranges. All the values within the ranges would have to be
listed with cases:
switch (widgets_sold)
{
case 0:
case 1:
// ...
case 29:
points_earned = 0;
break;
// Und so weite.
}


Another, maybe more proper method, is to use a range, value
table. The table contains records of
<range start, range end, value>

struct Range_Record
{
unsigned int start;
unsigned int end;
unsigned int points;
};

Then create the table:
const Range_Record Price_Table[] =
{
// start end points
{ 0, 29, 0},
{ 30, 59, 100},
{ 60, 89, 200},
// Und so weite.
};
const unsigned int NUM_PRICE_RECORDS =
sizeof (Price_Table) / sizeof (Price_Table[0]);


The above methods are usually applied when there is no
mathematical expression to the data and the range
of values is finite.

The big advantage to using the table, is that you can
add entries without having to retest the code that
searches the table. In many situations, modifying
executable code causes many problems and much regression
testing.

Just showing alternatives.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 

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

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top