Hi guys, I'd really appreciate a tip in what's going wrong for me in this code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
usingnamespace std;
int main()
{
float a;
float b;
float d = a + b;
if (d == 2)
{
cout << a << '\n' << b;
}
return 0;
}
I first wanted to get factors of numbers using this method or maybe an infinite while method to find all the factors of a number or something but it doesn't even give me the write answers for addition, http://puu.sh/1jw2t ..
I'm probably making some obvious mistake here but I can't seem to find it..
You are forgetting to declare the values of float variables a and b. If they are not declared, then the compiler will have trouble finding the sum of a problem whose factors have no value. Your code should be:
#include "iostream"
using namespace std;
int main()
{
float a=1;
float b=1;
float d=a+b;
if (d=2)
{
cout<< a << '\n' <<b;
}
return(0)
I actually wanted the computer to find 'a' and 'b' for the user and the user would input 'd', so it would be kinda like a calculator. so I'm trying to ask the compiler if 'd' is 2, what are 'a' and 'b'?
I wanted to use this method for multiplication and division too!
Line 12 is an assignment, not a comparison. So whatever a + b is, after line 12 d is always equal to 2. Try if (d == 2) instead.
Finding the factors of a number requires some modulo/remainder arithmetic, which you really can't do with floating point values (it works on integers).
Could you know whether a+b=10 if I don't tell you the values of a and b?
Well, the computer can, sort of. Unassigned variables are left with the value of whatever happened to be in memory when the variable was allocated. This will almost always be garbage. To put it succinctly: garbage+garbage=garbage, and garbageā 10.
So that means creating a simple programme that finds missing numbers of an equation is impossible? or is there a way to clean up the garbage of where unassigned variables are allocated?
I'll try maybe assigning 'a' and 'b' values of 0 and then working from there or adding some for loop or something :D .
So that means creating a simple programme that finds missing numbers of an equation is impossible?
It's impossible this way. There are symbolic computation methods to solve certain classes of equations, and there's also numerical algorithms to approximate said solutions.
But in any case, the C++ language is imperative, not declarative like mathematics. You can't just do
1 2
int a=20, b;
a=10+b;
and expect b to hold 10 at the end.
Despite their name, statements don't actually state anything about the variables involved. a=10+b is a transformation of the state (http://en.wikipedia.org/wiki/State_%28computer_science%29 ) of the computer; more specifically of the value of a.
or is there a way to clean up the garbage of where unassigned variables are allocated?
if(a = b) should be if(a == b). if(a = b) Sets a = b, so it always returns true. a == b is a conditional statement.
so:
Double symbol (&&, ||, ==) is conditional, while a single (&, |, =) is the binary representation.
Also, you failed to initialize 'a' and 'b', so those two integers could be any number.
That is why your code will not work.
On another note: Createing a program to "find 'x'" is not impossible. You simply have to reverse the equation your self, and give the program that equation to use, and the variables to use it with.
I did this all the time in my trigonometry class, lol. We would get to another subject and I would program my calculator with all the equations to find every variable in the equation. That way no matter what variable I needed to find, I would be able to pick the variable from my calculator, enter the numbers, and
it would give me the right answer every time.
On the other hand, if you want a program that will reverse an equation (x + 1 = 2 + 4 -> 2 + 4 - 1 = x), you will have to write a parser algorithm that will read the equation, and take the necessary steps to reverse it. That is extremely complex though (imo).