Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   C++ help (http://www.velocityreviews.com/forums/t460877-c-help.html)

 davy.zou@brentwood.bc.ca 03-03-2007 10:42 PM

C++ help

I have started learning c++ and I need help. I need to write a
program, the question is as follows.

At a post office, there are a certain number of 2, 7, and 9cents
stamps, now, given a total number of cents required, find the correct
and most effective combination of stamps.

meaning that if you were to need 14cents, the correct output should be
2 seven cents not 7 two cents.

the program is to use functions, and at first I thought I could use
bisection searching, but that didn't go very well. I think we are
suppose to use call-by-references as well, except I haven't figured
out how to do that yet.

Any help would be greatly appreciated.

Thanks
Davy

 Ian Collins 03-03-2007 10:55 PM

Re: C++ help

davy.zou@brentwood.bc.ca wrote:
> I have started learning c++ and I need help. I need to write a
> program, the question is as follows.
>
> At a post office, there are a certain number of 2, 7, and 9cents
> stamps, now, given a total number of cents required, find the correct
> and most effective combination of stamps.
>
> meaning that if you were to need 14cents, the correct output should be
> 2 seven cents not 7 two cents.
>
> the program is to use functions, and at first I thought I could use
> bisection searching, but that didn't go very well. I think we are
> suppose to use call-by-references as well, except I haven't figured
> out how to do that yet.
>
> Any help would be greatly appreciated.
>

Post an attempt and you will get some help. We don't do homework here,
but we are happy to help with problems in your homework code.

--
Ian Collins.

 davy.zou@brentwood.bc.ca 03-03-2007 11:02 PM

Re: C++ help

On Mar 3, 5:55 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> davy....@brentwood.bc.ca wrote:
> > I have started learning c++ and I need help. I need to write a
> > program, the question is as follows.

>
> > At a post office, there are a certain number of 2, 7, and 9cents
> > stamps, now, given a total number of cents required, find the correct
> > and most effective combination of stamps.

>
> > meaning that if you were to need 14cents, the correct output should be
> > 2 seven cents not 7 two cents.

>
> > the program is to use functions, and at first I thought I could use
> > bisection searching, but that didn't go very well. I think we are
> > suppose to use call-by-references as well, except I haven't figured
> > out how to do that yet.

>
> > Any help would be greatly appreciated.

>
> Post an attempt and you will get some help. We don't do homework here,
> but we are happy to help with problems in your homework code.
>
> --
> Ian Collins.

I wasn't asking anyone to do my homework. I just need someone to point
me in the right direction.

 davy.zou@brentwood.bc.ca 03-03-2007 11:12 PM

Re: C++ help

here is the code I first came up with, obviously it doesn't work,

#include <iostream.h>
#include <math.h>

int centscalculation (int x, int y, int z);
int computecent (int x, int y, int z);

int centscalculation (int x, int y, int z) {
double low=0, high=1, guessedcent, calculatedcent;
const double epsilon=0.0001;

for (;;) {
guessedcent=(low+high)/2;
if ((high-low)<(2*epsilon)) {
return guessedcent;
}

centscalculation=computecent (x, y, z);

if (calculatedcent==guessedcent) {
return guessedcent;
}

if (calculatedcent>guessedcent) {
low=guessedcent;
} else {
high=guessedcent;
}
}

}

int computecent (int x, int y, int z) {

int a=2, b=7, c=9;

return x*a+y*b+z*c;

}

void main () {

int cents, a=2, b=7, c=9, x, y, z, ans; //a has value of 2, b has 7,
c has 9

while (true) {

cout<<"Enter cents, 0 to terminate: "<<endl;
cin>>cents;

if (cents<0) {
cout<<"Error."<<endl;
}

if (cents=0) {
break;
}

ans=centscalculation (x, y, z);

}

}

 Gavin Deane 03-03-2007 11:23 PM

Re: C++ help

On 3 Mar, 23:02, davy....@brentwood.bc.ca wrote:
> On Mar 3, 5:55 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> > davy....@brentwood.bc.ca wrote:
> > > I have started learning c++ and I need help. I need to write a
> > > program, the question is as follows.

>
> > > At a post office, there are a certain number of 2, 7, and 9cents
> > > stamps, now, given a total number of cents required, find the correct
> > > and most effective combination of stamps.

>
> > > meaning that if you were to need 14cents, the correct output should be
> > > 2 seven cents not 7 two cents.

>
> > > the program is to use functions, and at first I thought I could use
> > > bisection searching, but that didn't go very well. I think we are
> > > suppose to use call-by-references as well, except I haven't figured
> > > out how to do that yet.

>
> > > Any help would be greatly appreciated.

>
> > Post an attempt and you will get some help. We don't do homework here,
> > but we are happy to help with problems in your homework code.

> I wasn't asking anyone to do my homework. I just need someone to point
> me in the right direction.

The FAQ is at http://www.parashift.com/c++-faq-lite/ but it seems to
be down at the moment. When it comes back to life, the FAQ you want is
5.8. As Ian says, there are plenty of people here willing and able to
without knowing what your problem is. FAQ 5.8 explains how to get that
help. In summary:

Post the actual code you are having problems with, not a description
of the code.
Post *minimal* code - i.e. the *smallest possible* program that
Post *compileable* code - i.e. copied and pasted *directly* from your
For code that does not compile and you don't understand why, copy and
past the error messages directly from your compiler and make it clear
which statement(s) the error refers to.
For a program that compiles and runs but doesn't behave as you expect,
post the inputs to the program, the actual outputs and your expected
outputs.

Without all this, nobody can be sure they are recreating exactly the
situation you have and be sure they are answering the right question.

HTH
Gavin Deane

 davy.zou@brentwood.bc.ca 03-03-2007 11:31 PM

Re: C++ help

On Mar 3, 6:23 pm, "Gavin Deane" <deane_ga...@hotmail.com> wrote:
> On 3 Mar, 23:02, davy....@brentwood.bc.ca wrote:
>
>
>
> > On Mar 3, 5:55 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> > > davy....@brentwood.bc.ca wrote:
> > > > I have started learning c++ and I need help. I need to write a
> > > > program, the question is as follows.

>
> > > > At a post office, there are a certain number of 2, 7, and 9cents
> > > > stamps, now, given a total number of cents required, find the correct
> > > > and most effective combination of stamps.

>
> > > > meaning that if you were to need 14cents, the correct output should be
> > > > 2 seven cents not 7 two cents.

>
> > > > the program is to use functions, and at first I thought I could use
> > > > bisection searching, but that didn't go very well. I think we are
> > > > suppose to use call-by-references as well, except I haven't figured
> > > > out how to do that yet.

>
> > > > Any help would be greatly appreciated.

>
> > > Post an attempt and you will get some help. We don't do homework here,
> > > but we are happy to help with problems in your homework code.

> > I wasn't asking anyone to do my homework. I just need someone to point
> > me in the right direction.

>
> The FAQ is athttp://www.parashift.com/c++-faq-lite/but it seems to
> be down at the moment. When it comes back to life, the FAQ you want is
> 5.8. As Ian says, there are plenty of people here willing and able to
> without knowing what your problem is. FAQ 5.8 explains how to get that
> help. In summary:
>
> Post the actual code you are having problems with, not a description
> of the code.
> Post *minimal* code - i.e. the *smallest possible* program that
> Post *compileable* code - i.e. copied and pasted *directly* from your
> For code that does not compile and you don't understand why, copy and
> past the error messages directly from your compiler and make it clear
> which statement(s) the error refers to.
> For a program that compiles and runs but doesn't behave as you expect,
> post the inputs to the program, the actual outputs and your expected
> outputs.
>
> Without all this, nobody can be sure they are recreating exactly the
> situation you have and be sure they are answering the right question.
>
> HTH
> Gavin Deane

Thanks alot.

 osmium 03-03-2007 11:43 PM

Re: C++ help

<davy.zou@brentwood.bc.ca> wrote:

> here is the code I first came up with, obviously it doesn't work,

<snip>

Think about dividing the available amount by 9, 7 and 2 in that order. I am
not sure that is the right answer in all cases, but you can expand on it if
needed. Look up the modulo operator (%). See the Wikipedia entry for
modulo..

 Kai-Uwe Bux 03-04-2007 12:06 AM

Re: C++ help

osmium wrote:

> <davy.zou@brentwood.bc.ca> wrote:
>
>> here is the code I first came up with, obviously it doesn't work,

>
> <snip>
>
> Think about dividing the available amount by 9, 7 and 2 in that order. I
> am not sure that is the right answer in all cases, but you can expand on
> it if needed. [snip]

I am not so sure that this looks like a promissing line of attack. There
seems to be a little more to the problem. What is the answer for 10 and how
do you find it through the division sequence? What about 35?

Best

Kai-Uwe Bux

 davy.zou@brentwood.bc.ca 03-04-2007 12:07 AM

Re: C++ help

On Mar 3, 6:43 pm, "osmium" <r124c4u...@comcast.net> wrote:
> <davy....@brentwood.bc.ca> wrote:
> > here is the code I first came up with, obviously it doesn't work,

>
> <snip>
>
> Think about dividing the available amount by 9, 7 and 2 in that order. I am
> not sure that is the right answer in all cases, but you can expand on it if
> needed. Look up the modulo operator (%). See the Wikipedia entry for
> modulo..

THANK YOU! dividing it by 9, 7, and 2! that is ingenious!

 bnonaj 03-04-2007 12:35 AM

Re: C++ help

davy.zou@brentwood.bc.ca wrote:
> I have started learning c++ and I need help. I need to write a
> program, the question is as follows.
>
> At a post office, there are a certain number of 2, 7, and 9cents
> stamps, now, given a total number of cents required, find the correct
> and most effective combination of stamps.
>
> meaning that if you were to need 14cents, the correct output should be
> 2 seven cents not 7 two cents.
>
> the program is to use functions, and at first I thought I could use
> bisection searching, but that didn't go very well. I think we are
> suppose to use call-by-references as well, except I haven't figured
> out how to do that yet.
>
> Any help would be greatly appreciated.
>
> Thanks
> Davy
>

This is a variation of the bin packing problem. I've developed
C++ code which can solve this problem for all solutions. It is
also capable of handling target ranges as well, and multiples
thereof. It's known to be a hard problem to create an efficient
algorithm for.

JB

All times are GMT. The time now is 10:56 AM.