473,472 Members | 2,128 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C++ matrix question

1 New Member
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 6043
Banfa
9,065 Recognized Expert Moderator Expert
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

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

Similar topics

6
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...
6
by: memocan | last post by:
#include <iostream> using namespace std; int x; //global variable matrix int main() { x= new float ; //initialize the size now }
1
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...
10
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.
0
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...
2
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...
18
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...
14
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.
4
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...
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
jinu1996
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...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.