0

I know this problem has been there before but I still need help. I have found an old task and have been at it for 5 hours but can't find the solution. Does someone have some advice for me? The task is to create an math formula which allows to decide between 3 different formula without any kind of "switch, if-else or other operators" Sooo goal is it to just cin a number; decide with cin 1, 2 or 3 between Celsius to Fahrenheit, meters in foot or € in $ and gettin a result.

The formulas are:

  1. Celsius degrees to Fahrenheit = x * 1.8 + 32
  2. meters to feet = x * 3.2808
  3. euro to US dollar = x * 1.2957

I am not allowed to use if, else, bool, == etc. Only +, -, , *, =, cout, cin and the other basic things like data type and variables.

#include <iostream>

using namespace std;

int main()
{
double eingabe;
int auswahl = 0;
double ergebnis;


//zahleneingabe
cout << "Ihre Eingabe: ? " << endl;
cin >> eingabe;
cout << "Ihre Eingabe: " << eingabe << endl;

system("PAUSE");


//abfragenausgabe der umrechnungsart
cout << "Ihre Auswahl der Umwandlung: " << endl;
cout << "1 - Celsius in Fahrenheit" << endl;
cout << "2 - Meter in Fuss" << endl;
cout << "3 - Euro in US Dollar " << endl;

cin >> auswahl;

cout << "Ihr Ergebnis ist: " << ergebnis << endl;

system("PAUSE");

}

  • 1
    What does "basic things" mean? Are you allowed to use an array of function pointers? Or is that not basic anymore? – UnholySheep Oct 08 '22 at 22:58
  • Why so many restrictions? – Stephen Newell Oct 08 '22 at 23:02
  • 1
    Without an `if` or a `switch`, what you're asking cannot be done with the *basic things* you've listed. I'm guessing you misunderstood your assignment. – Ken White Oct 08 '22 at 23:02
  • @StephenNewell: Because it's homework? – Ken White Oct 08 '22 at 23:02
  • @user20194239: What part of *Without `if` or a `switch`, what you're asking cannot be done* is unclear? And again, I'm guessing you misunderstood your assignment. – Ken White Oct 08 '22 at 23:03
  • @KenWhite - I assumed that, but also assumed the instructor has a reason for these restrictions. That reason may be the key to provide an answer (or a reason to find a new instructor). – Stephen Newell Oct 08 '22 at 23:03
  • @UnholySheep that‘s also not allowed – user20194239 Oct 08 '22 at 23:04
  • @StephenNewell: My suspicion is that the OP misunderstood the assignment, and thus is presenting a problem that can't be solved. – Ken White Oct 08 '22 at 23:05
  • @KenWhite no i dont misunderstood the assignment. I was also confused but the teacher said it‘s possible and he will upload the solution next week but I cant wait. I also saw the same Assignment on other websites. – user20194239 Oct 08 '22 at 23:08
  • @user20194239: Then get the answer from that *other website*. You can't decide on which one of three options you execute without an `if` or `switch`. – Ken White Oct 08 '22 at 23:09
  • @KenWhite it‘s the same assignment like here https://stackoverflow.com/questions/58553291/combined-conversion – user20194239 Oct 08 '22 at 23:09
  • @user20194239: Then use the answer to that question to figure it out. – Ken White Oct 08 '22 at 23:13
  • @KenWhite sry but I tried to understand how the others came up with their solutions but I didn't get it. – user20194239 Oct 08 '22 at 23:19
  • @user20194239: The accepted answer to that post (the one with the green checkmark) has a pretty clear explanation of the math involved to solve the problem, which applies to C as well as C++. If you can't understand that explanation, how is one you receive here any different? I think your post should be closed as a duplicate of the post you linked, because the accepted answer is applicable to almost any coding language. – Ken White Oct 08 '22 at 23:22
  • 1
    I always enjoy the setup that OP has been presented this problem with *absolutely zero* principles taught beforehand. I know that there are professors that lousy out there, but I'd consider them a small minority. Also, can't use `if`, which **is** basic, but *can* use `system("PAUSE");`. That tracks. – sweenish Oct 08 '22 at 23:23
  • @KenWhite but he used other formulas. – user20194239 Oct 08 '22 at 23:27
  • @user20194239: No, there is nothing there but basic math operations (+, -, *, /, %). Read it again. The accepted answer has a *very, very clear* explanation of what exactly is being done, and the sample code is very clear and easy to read. There are also two other answers there that are very clear if you read the code. Again, this question should be closed as a duplicate of that other post, because it's the same exact question with three perfectly good answers - even the variable names are the same as the one in your question. You need to use that information and apply it to your post. – Ken White Oct 08 '22 at 23:30
  • @sweenish i cant figure out how they get the values at the end. – user20194239 Oct 08 '22 at 23:41
  • I don't know what you hoped for by pinging me. The duplicate is as clear of an explanation as you're going to get. – sweenish Oct 09 '22 at 02:39

1 Answers1

0

The expression (choice - 1) * (choice - 2) * 0.5 is 0 if the user's choice is 1 or 2, and it is 1 if the user's choice is 3.

Multply this by the formula for the Euro-to-dollar conversion.

Then add that product to the similar products for the other two conversions.

David Grayson
  • 84,103
  • 24
  • 152
  • 189