-1

I am stuck trying to figure out a way using C++ to solve a situation where the user inputs a value using cin and then have the computer solve for a way to get the value of cin, given that the format is given. a super fast example is written below.. yes i know there is a lot of code missing... but the concept is there..

int x;
int y;
int w;

int x = 30 < w < 50;
int y = 60 < w < 90;

cin >> input;

x + y = input;

cout << x;
cout << y;

Naturally though x + y cant be on the lvalue on the right. so i cant just write x + y = input.. so how would i have it solve x + y = input? Additionally I want x and y to be between the numbers listed, which limits the numbers between those inputs.. however in my actual coding i did this with a function.

has school even started yet? no its not homework. im teaching myself C++.. – Sean Holt 1 min ago edit

No im just trying to figure a way of having the computer solve for x/y of an input value if x and y are between specified values in a function

6
  • What's the use of int x = 30 < w < 50; int y = 60 < w < 90;? Aug 25, 2012 at 8:25
  • We cannot understand your example. Sorry Aug 25, 2012 at 8:26
  • Is this int x = 30 < w < 50; pseudo-code for some conditions that x must satisfy? If so, it might be easier to express it in words. Aug 25, 2012 at 8:29
  • is school even started yet? no its not homework. im teaching myself C++..
    – Sean Holt
    Aug 25, 2012 at 8:38
  • 1
    Two variables, one equation...
    – user1434698
    Aug 25, 2012 at 8:42

4 Answers 4

2

It looks like you think that C++ is going to solve equations for you. It won't. C++ is an imperative style language that is based around the concept of you telling it exactly what to do.

You will have to figure out how to solve for x and y so that you can make an algorithm. This algorithm is then what you make your program from.

There exists other languages in which you can in a sense describe what you want and have the compiler or runtime figure out how to get it for you. C++ is not one of them.

Different ways to solve your particular problem would be to set up an equation system and solving that. Or do brute force approach and iterate through the values of x and y in order to find out which values match.

1

It looks like you have a 'mathematical' problem here: a couple of values constrained by equations, and you want 'the computer' to find all possible values that fit into the constraints (equations). Am I right?

While some computer programs can certainly do that, the C++ language is not designed for this purpose. The role of the C++ is to give you a way of giving instructions to the processor, like "store this value in memory" or "add these two numbers". But there is no way of saying "solve this mathematical problem".

What you need is some equation solver. But I am not familiar with any. There are tools like Matlab or Mathematica. But I do not think they are free.

0

If you want to solve the math problem algorithmically, here is a brute force idea in pseudocode:

Input a number.
for each value x between 30 and 50
  for each value y between 60 and 90
    if x+y equals the number
      print x and y

Now you can take a good book or tutorial and code in C++. Look for the for and if keywords (algorithmic concepts of iteration and selection) in your teaching material. Have fun!

0

This case can be solved trivially by interval arithmetic. C++ code that solves your "sum of two interval-constrained variables problem" is given below.

int min_x = 30, max_x = 50;
int min_y = 60, max_y = 90;

// solutions exist in this interval
int min_z = min_x + min_y, max_z = max_x + max_y;

cin >> input;

// solutions possible?
if (input >= min_z && input <= max_z)
{
    // determine solution interval for x (y is dependent)
    cout
    << "Solution:\n"
    << "x in [" << min(max(  input - max_y  , min_x),max_x)
    << ";"      << min(max(  input - min_y  , min_x),max_x) << "], "
    << "y = "   << input << " - x" << endl;
}
else
{
    cout << "No solution." << endl;
}

Computers are "basically stupid" and if they do smart things it is the software. Using a general purpose programming language like C++ requires you (or at least the libraries you eventually use) to be very specific on how exactly to solve a problem based on the simple arithmetic means of the bare computer.

Although the programming language won't magically and somehow do things for you, algorithms exist to solve many mathematical standard problems such as e.g. systems of equations. Numerical Recipes in C++ covers a variety of algorithms and their C++ implementations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.