Minimum number of coins with 3 currencies algorithm
In the following u is the amount, (p, z1, z2) is the combination of currencies being consid-
ered which involves n coins and which has a loose of l pence. The combination (ˆp, ˆz1, ˆz2)
involving ˆn coins, with a loose of ˆl pence is the best found at any given stage.
(i) Set the first candidate for the optimum division as
ˆp = int(u) with ˆz1 and ˆz2 as follows:
If r2 > r1 then take ˆz1 = 0 and ˆz2 = int(r2(u − ˆp)).
Otherwise take ˆz1 = int(r1(u − ˆp)) and ˆz2 = 0.
Set ˆl = u − (p + z1/r1 + z2/r2) and ˆn = g(p, GBP) + g(z1, USD) + g(z2, JPY).
This uses the maximum amount of the currency GBP.
(ii) Loop through all the possibilities:
For p = int(u), · · · , 1, 0
If g(p, GBP) > ˆn then jump to the next p
For z1 = 0, 1, 2, · · · , int(r1(u − p))
If g(p, GBP) + g(z1, USD) > ˆn then jump to the next z1
z2 = int(r2(u − p − r1/z1))
n = g(p, GBP) + g(z1, USD) + g(z2, JPY)
If n > ˆn then jump to the next z1
l = u − (p + z1/r1 + z2/r2)
If n = ˆn and l > ˆl then jump to the next z1
Update the best so far to ˆn = n, ˆp = p, ˆz1 = z1, ˆz2 = z2, ˆl = l.
i am trying to convert this to c++ lang. this what i have done so far:
void fo08ssg1_get_mixed(double u, double r1, double r2,
int m_gb[], int m_c1[], int m_c2[],
int& ph, int& z1h, int& z2h, double& lh, int& nh)
{
// ..fo08ssg1_part[J]: determine ph, z1h, z2h, lh and nh
int n=0;
double l=0;
z2h =int(r2*(u-p-r1/z1);
nh = g(ph,m_gb) + g (z1h, m_c2) + g (z2h, m_c1);
if (n > nh) continue;
{
l = u-( p+ z1h / r1+ z2 / r2 );
if ((n == nh && l > lh)) continue);
{
nh=n;
ph=p;
z1h=z1;
z2h=z2;
lh=l;
}
}
}
[b][b][b][b][b]but it won't compile any help would be helpfull
error recieved are :
xfstru103.academic.windsor\fo08\fo08ssg1\My Documents\fo08ssg1_a1.cpp: In function `void fo08ssg1_get_mixed(double, double, double, int*, int*, int*, int&, int&, int&, double&, int&)':
\\uxfstru103.academic.windsor\fo08\fo08ssg1\My Documents\fo08ssg1_a1.cpp:107: error: `g' was not declared in this scope
\\uxfstru103.academic.windsor\fo08\fo08ssg1\My Documents\fo08ssg1_a1.cpp:107: warning: left-hand operand of comma has no effect
\\uxfstru103.academic.windsor\fo08\fo08ssg1\My Documents\fo08ssg1_a1.cpp:107: warning: left-hand operand of comma has no effect
\\uxfstru103.academic.windsor\fo08\fo08ssg1\My Documents\fo08ssg1_a1.cpp:107: warning: left-hand operand of comma has no effect
\\uxfstru103.academic.windsor\fo08\fo08ssg1\My Documents\fo08ssg1_a1.cpp:109: error: `p' was not declared in this scope
\\uxfstru103.academic.windsor\fo08\fo08ssg1\My Documents\fo08ssg1_a1.cpp:109: error: `Ph' was not declared in this scope
\\uxfstru103.academic.windsor\fo08\fo08ssg1\My Documents\fo08ssg1_a1.cpp:111: error: expected primary-expression before "if"
\\uxfstru103.academic.windsor\fo08\fo08ssg1\My Documents\fo08ssg1_a1.cpp:111: error: expected `;' before "if"
\\uxfstru103.academic.windsor\fo08\fo08ssg1\My Documents\fo08ssg1_a1.cpp:111: error: expected primary-expression before "if"
\\uxfstru103.academic.windsor\fo08\fo08ssg1\My Documents\fo08ssg1_a1.cpp:111: error: expected `)' before "if"
\\uxfstru103.academic.windsor\fo08\fo08ssg1\My Documents\fo08ssg1_a1.cpp:113: error: `z1' was not declared in this scope
\\uxfstru103.academic.windsor\fo08\fo08ssg1\My Documents\fo08ssg1_a1.cpp:116: error: continue statement not within a loop
\\uxfstru103.academic.windsor\fo08\fo08ssg1\My Documents\fo08ssg1_a1.cpp:118: error: `z2' was not declared in this scope
\\uxfstru103.academic.windsor\fo08\fo08ssg1\My Documents\fo08ssg1_a1.cpp:119: error: continue statement not within a loop
\\uxfstru103.academic.windsor\fo08\fo08ssg1\My Documents\fo08ssg1_a1.cpp:119: error: expected `;' before ')' token
\\uxfstru103.academic.windsor\fo08\fo08ssg1\My Documents\fo08ssg1_a1.cpp:119: error: expected primary-expression before ')' token
\\uxfstru103.academic.windsor\fo08\fo08ssg1\My Documents\fo08ssg1_a1.cpp:119: error: expected `;' before ')' token
void fo08ssg1_get_mixed(double u, double r1, double r2,
int m_gb[], int m_c1[], int m_c2[],
int& ph, int& z1h, int& z2h, double& lh, int& nh)
All those variables are declared in the function defenition of fo08ssg1_get_mixed, wich means they belong to that function and dont exist outside it (They are only accessible inside the scoop of this function).
Declaring them as global variables or inside main will probably solve a lot of your errors.
(Edited:)I must be tired or someting... z1h is someting else as z1, ph someting else as Ph etc. That causes the scoop-errors
Besides that, you got some syntax-errors (expected ';' before ')' etc.). Simply look at the errors and walk through your program, and you should be able to solve them.
Hope this helps, good luck
Ps. Always choose clear names for variables and functions. Avoid names like fo08ssg1_get_mixed (long, complicated), g, p, ph (doesnt say anyting about there function)