473,385 Members | 1,630 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,385 software developers and data experts.

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

"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

"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

"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
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

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Alan Bashy | last post by:
Please, guys, In need help with this. It is due in the next week. Please, help me to implement the functions in this programm especially the first three constructor. I need them guys. Please, help...
2
by: Jackson Yap | last post by:
can someone kind enough to help me look at the attached html and js file? Why is it that the javascript menu could not work at www.apchosting.net but could work at...
5
by: Craig Keightley | last post by:
Please help, i have attached my page which worksin IE but i cannnot get the drop down menu to fucntion in firefox. Any one have any ideas why? Many Thanks Craig ...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
1
by: Michael D. Reed | last post by:
I am using the help class to display a simple help file. I generated the help file using Word and saving it as a single page Web page (.mht extension). I show the help file with the following...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
4
by: Stef Mientki | last post by:
I'm making special versions of existing functions, and now I want the help-text of the newly created function to exists of 1. an extra line from my new function 2. all the help text from the old...
22
by: Amali | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of...
1
by: Bob | last post by:
Hi, Hope you can help me with this one. I'm at my wits end. I'm trying to create an intelligent edit-box like the excellent "Customer" one at the URL: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.