Connecting Tech Pros Worldwide Help | Site Map

Help with function

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 19th, 2005, 04:20 PM
Paul
Guest
 
Posts: n/a
Default Help with function

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.



  #2  
Old July 19th, 2005, 04:20 PM
Paul
Guest
 
Posts: n/a
Default Re: Help with function

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


"Victor Bazarov" <v.Abazarov@attAbi.com> wrote in message
news:vjik3nk9tij893@corp.supernews.com...[color=blue]
> "Paul" <PB@nospam.networktel.net> wrote...[color=green]
> > 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[/color]
> and[color=green]
> > 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[/color][/color]
widgets[color=blue][color=green]
> > entered.[/color]
>
> 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
>
>[/color]


  #3  
Old July 19th, 2005, 04:20 PM
Paul
Guest
 
Posts: n/a
Default Re: Help with function

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:vjijbsecb3s179@corp.supernews.com...[color=blue]
> 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[/color]
and[color=blue]
> 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.
>
>[/color]


  #4  
Old July 19th, 2005, 04:20 PM
John Harrison
Guest
 
Posts: n/a
Default Re: Help with function


"Paul" <timK@nospamaol.com> wrote in message
news:vjikl7kn50c23f@corp.supernews.com...[color=blue]
> Nothing like asking for help and getting shot in the face. Disregard[/color]
then.[color=blue]
>[/color]

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


  #5  
Old July 19th, 2005, 04:20 PM
John Harrison
Guest
 
Posts: n/a
Default Re: Help with function


"Paul" <timK@nospamaol.com> wrote in message
news:vjil4091hgo0aa@corp.supernews.com...[color=blue]
> 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[/color]
might[color=blue]
> be a cleaner way.
>[/color]

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

john


  #6  
Old July 19th, 2005, 04:20 PM
Howard
Guest
 
Posts: n/a
Default Re: Help with function


"Paul" <timK@nospamaol.com> wrote in message
news:vjil4091hgo0aa@corp.supernews.com...[color=blue]
> 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[/color]
might[color=blue]
> be a cleaner way.
>[/color]

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


  #7  
Old July 19th, 2005, 04:20 PM
Paul
Guest
 
Posts: n/a
Default Re: Help with function

Thanks John.


"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:bhbkkq$10cq44$1@ID-196037.news.uni-berlin.de...[color=blue]
>
> "Paul" <timK@nospamaol.com> wrote in message
> news:vjil4091hgo0aa@corp.supernews.com...[color=green]
> > 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[/color]
> might[color=green]
> > be a cleaner way.
> >[/color]
>
> Sounds good to me. That's how I would do it.
>
> john
>
>[/color]


  #8  
Old July 19th, 2005, 04:20 PM
John Harrison
Guest
 
Posts: n/a
Default Re: Help with function


"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:bhbkkq$10cq44$1@ID-196037.news.uni-berlin.de...[color=blue]
>
> "Paul" <timK@nospamaol.com> wrote in message
> news:vjil4091hgo0aa@corp.supernews.com...[color=green]
> > 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[/color]
> might[color=green]
> > be a cleaner way.
> >[/color]
>
> Sounds good to me. That's how I would do it.
>[/color]

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


  #9  
Old July 19th, 2005, 04:20 PM
Paul
Guest
 
Posts: n/a
Default Re: Help with function

Ah makes perfect sense now!

Thank you
"Howard" <alicebt@hotmail.com> wrote in message
news:bhbkrn$bj2@dispatch.concentric.net...[color=blue]
>
> "Paul" <timK@nospamaol.com> wrote in message
> news:vjil4091hgo0aa@corp.supernews.com...[color=green]
> > 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[/color]
> might[color=green]
> > be a cleaner way.
> >[/color]
>
> 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[/color]
should[color=blue]
> be sufficient.
>
> -Howard
>
>[/color]


  #10  
Old July 19th, 2005, 04:20 PM
Default User
Guest
 
Posts: n/a
Default Re: Help with function

Paul wrote:[color=blue]
>
> Nothing like asking for help and getting shot in the face. Disregard then.[/color]


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
  #11  
Old July 19th, 2005, 04:21 PM
Thomas Matthews
Guest
 
Posts: n/a
Default Re: Help with function

Paul wrote:[color=blue]
> 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.
>
>[/color]

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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.