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

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 6041
9,065 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;
  6. if (n1+n2+n3 == total)
  7. {
  8.     cout << "n1+n2+n3 == total is true\n";
  9. }
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;
  6.     if (fn1+fn2+fn3 == ftotal)
  7.     {
  8.         cout << "fn1+fn2+fn3 == ftotal is true\n";
  9.     }
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

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

Similar topics

by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
by: memocan | last post by:
#include <iostream> using namespace std; int x; //global variable matrix int main() { x= new float ; //initialize the size now }
by: John | last post by:
When using the C# for the Matrix Transformation for a 100x200 panel: 1. To Reverse Y axis, I can: Matrix m = new Matrix(new Rectangle(0,0,100,200), new Point { new Point(0,100), new...
by: Duncan M Gunn | last post by:
Hi, I need to store the following matrix of values: T U V A 2 1 1 B 2 - - C - - 2 Where (-) is an empty cell.
by: Zoran Stipanicev | last post by:
Hi! The question is product of laziness. I want to write generic operators for matrix computation which would adapt to different types of matrices i.e. when adding square matrix and lower...
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
by: Hypnotik | last post by:
Hello everyone. I'm writing a program which uses a class called matrix. I have written all of the different functions, constructor, etc. When I run the program I receive "Constructor", which I...
by: Thomas Bauer | last post by:
Hello, I search a example like that. (1,0,0,1,4,5 ) -- moving (1,0,0,1,0,0 ) -- normal matrix (-1,0,0,1,0,0 ) -- Mirror Y-Axis (0,-1,0,1,0,0 ) -- Mirror X-Axis I caluculate all in mm.
by: krishnai888 | last post by:
I had already asked this question long back but no one has replied to me..I hope someone replies to me because its very important for me as I am doing my internship. I am currently writing a code...
by: almurph | last post by:
Hi everyone, Concerning the Needleman-Wunsch algorithm (cf. http://en.wikipedia.org/wiki/Needleman-Wunsch_algorithm) I have noticed a possible loop. Inside the algorithm there is an...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.