By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 967 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Help with function

P: n/a
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.
Jul 19 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Nothing like asking for help and getting shot in the face. Disregard then.
"Victor Bazarov" <v.********@attAbi.com> wrote in message
news:vj************@corp.supernews.com...
"Paul" <PB@nospam.networktel.net> wrote...
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.


What have you already done (aside from typing your assignment)?
If you have a program that doesn't compile, post it, we could
help you fix it. If you haven't done nothing, well, go do
something. We can help, but we're not going to do your homework
for you.

If you don't know where to begin, here is a simple program that
allows you to enter a number and then it prints it back:

#include <iostream>
using namespace std;

int main()
{
cout << "enter a number " << flush;
int number;
cin >> number;
cout << "you entered " << number << endl;
return 0;
}

Victor

Jul 19 '05 #2

P: n/a
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.

"Paul" <PB@nospam.networktel.net> wrote in message
news:vj************@corp.supernews.com...
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.

Jul 19 '05 #3

P: n/a

"Paul" <ti**@nospamaol.com> wrote in message
news:vj************@corp.supernews.com...
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
Jul 19 '05 #4

P: n/a

"Paul" <ti**@nospamaol.com> wrote in message
news:vj************@corp.supernews.com...
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
Jul 19 '05 #5

P: n/a

"Paul" <ti**@nospamaol.com> wrote in message
news:vj************@corp.supernews.com...
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
Jul 19 '05 #6

P: n/a
Thanks John.
"John Harrison" <jo*************@hotmail.com> wrote in message
news:bh*************@ID-196037.news.uni-berlin.de...

"Paul" <ti**@nospamaol.com> wrote in message
news:vj************@corp.supernews.com...
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

Jul 19 '05 #7

P: n/a

"John Harrison" <jo*************@hotmail.com> wrote in message
news:bh*************@ID-196037.news.uni-berlin.de...

"Paul" <ti**@nospamaol.com> wrote in message
news:vj************@corp.supernews.com...
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.


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
Jul 19 '05 #8

P: n/a
Ah makes perfect sense now!

Thank you
"Howard" <al*****@hotmail.com> wrote in message
news:bh********@dispatch.concentric.net...

"Paul" <ti**@nospamaol.com> wrote in message
news:vj************@corp.supernews.com...
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

Jul 19 '05 #9

P: n/a
Paul wrote:

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
Jul 19 '05 #10

P: n/a
Paul wrote:
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

Jul 19 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.