468,513 Members | 975 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

C++ matrix question

I'm trying to write a program that can do the following:

it takes 3 inputs: ex. 2000, 1000, 1000
it takes one more input called the total: ex. 6000
it should output that (2)*(2000)+(1)*(1000)+(1)*(1000)=6000 (so x=2, y=1, z=1)
the program should be able to find what combination of the 3 inputs will produce the total (it should be able to find all combinations). in this case it's easy because (2)*(2000)+1000+1000=6000 but I'm trying to do more complicated numbers that would be declared as "double" and not "int."

this is what I've got, I'm at work and we don't have C++ so I can't try it yet but can someone please tell me if this would work? and if not, can someone help me please?! thanks!

double n1, n2, n3;
cout << "enter 3 values" << endl;
cin >> n1 >> n2 >> n3;
double total;
cout << "enter a value for the total: " << endl;
cin >> total;
if (n1+n2+n3=total) {
cout << n1 << "+" << n2 << "+" << n3 << " = " << total;
return 0;
}
if (n1+n2=total) {
cout << n1 << "+" << n2 << " = " << total;
return 0;
}
if (n1+n3=total) {
cout << n1 << "+" << n3 << " = " << total;
return 0;
}
if (n2+n3=total) {
cout << n2 << "+" << n3 << " = " total;
return 0;
}
else {
int x, y, z;
(n1)*(x)+(n2)*(y)+(n3)*(z) = total;
cout << "x = " << x << ", y = " << y << ", z = " << endl;
}
return 0;
}


also, I don't know how to get it to output all possible combinations because like with the example before, if you have 2000, 1000, and 1000 and your total is 6000, you could have (3)*(2000) or (6)*(1000) or (2)(2000)+(1)(1000)+(1)(1000), etc.
Sep 8 '06 #1
1 5812
Banfa
9,056 Expert Mod 8TB
OK I see 2 problems with your code/problem.

Problem 1is a coding one in this code
Expand|Select|Wrap|Line Numbers
  1. int n1 = 2000;
  2. int n2 = 1000;
  3. int n3 = 1000;
  4. int total = 4000;
  5.  
  6. if (n1+n2+n3 == total)
  7. {
  8.     cout << "n1+n2+n3 == total is true\n";
  9. }
  10.  
Then the cout statement is executed because with integers there is no uncertainty in their value if you set an integer to a value of 4000 it has a value of 4000.

However in this example
Expand|Select|Wrap|Line Numbers
  1.     float fn1 = 2000.04f;
  2.     float fn2 = 1000.21f;
  3.     float fn3 = 1000.35f;
  4.     float ftotal = 4000.60f;
  5.  
  6.     if (fn1+fn2+fn3 == ftotal)
  7.     {
  8.         cout << "fn1+fn2+fn3 == ftotal is true\n";
  9.     }
  10.  
The cout statement is not executed. This may be surprising since the numbers look like they should add up to the total, your brain will tell you they should but this is demonstrating a problem with floating point arithmatic on a computer. The computer holds and approximation to the value, if you use a symbolic debugger you will find (or at least I did, the numbers may be different on your system) that

fn1+fn2+fn3 = 4000.6000366210937 - type double

ftotal = 4000.6001 - type float
ftotal = 4000.6000976562500 - type cast to double

and 4000.6000366210937 != 4000.6000976562500

Now this code produces this problem because I have used floats (which I did because it is easier to produce this type of problem with floats) and the inaccuracies havee crept in as they were cast to double. However with an equation of the form

x*n1 + y*n2 + z*n3 = total

there will be times when these errors creap into the calculation even if you are using floating point numbers.

So you can not test for equality the best you can get is something like

fabs( n1+n2+n3-total) < A preset tolerance for equality

where fabs is the absolute value of a floating point number.

Problem 2 is more esoteric less to do with programming and more to do with the nature of the universe. Let's consider a simpler example

a * b = 16

If I say find all positive integers a and b which meet this criteria then the solution is fairly simple, we have

a = 1, b =16
a = 2, b = 8
a = 4, b = 4
a = 8, b = 2
a = 16, b = 1

Note that after a=4,b=4 the results are the reverse of the initial results and that 4 = sqrt(16).

If you change the problem to find all the positive reals (floating point) a dn b that meet this criteria how have created yourself a problem because for every real a meeting the criteria

0 < a <= 4

there will be a corresponding value of b, once a has reach the value of 4 then by the argument above you have started repeating yourself.

So how many reals lie between 0 and 4, well there are a infinite number of real values between and 2 numbers so you have an infinite number of results.

Now on a computer things are so bad because a double can not represent any value but there are still a vast number of answers available and none of them will be acurate because of the rounding issue given in problem 1.
Sep 8 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by memocan | last post: by
1 post views Thread by John | last post: by
10 posts views Thread by Duncan M Gunn | last post: by
reply views Thread by Zoran Stipanicev | last post: by
2 posts views Thread by Allan Ebdrup | last post: by
18 posts views Thread by Hypnotik | last post: by
14 posts views Thread by Thomas Bauer | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.