so the assignment is to take four binary input numbers and to get them inside four logic gate and to take the output of the four logic gates and convert it to decimal .
the binary number which have been input is { 1 0 1 1) and it suppose to been added up by the logic gates to { 10+11 } which is equal to { 2+3 } in decimal which the output of it in binary is { 1 0 1 } which is equal to { 5 } in decimal.
and this is as far is i have gone in it , if anyone can help me please do.
When I think of a gate, I think of something simple like an AND or OR gate. Your gate functions are more complex than that. Is that okay for the assignment? What do each of the gate functions do?
Your binary_decimal function shouldn't need to use doubles. And why on earth does it take the inputs in 3,1,4,2 order?? It would seem a lot easier to me to make this:
int binary_decimal(int input4, int input3, int input2, int input1) {
return input4*8 + input3*4 + input2*2 + input1;
}
Now the function is very clear.
Finally, you are ignoring the return values of your gateN() functions. Is that okay?
Personally, if I were writing this with gate functions that are more complex than simple AND and OR functions, I'd write a function to do binary addition of 2 numbers:
// Add bit1 and bit2. The return value is the sum and "carry" is modified to return the carry out value
it's only AND , OR and NOT logic gates function and the input 3,1,4,2 is because the second and the fourth input numbers are going to the first gate (gate1 function) and the first inputs is going to the (second gate2 function, and the third gate4 function) and the third input is going in to (gate4 function)
this one is working with the main question which ( 10 + 11) in decimal ( 2 + 3 )
equal to the answer which is ( 0101) which equal to ( 5 ), but when i enter ( 1 1 1 1 ) which suppose to be ( 11 + 11 ) in decimal ( 3 + 3 ) equal to ( 0110) in binary equal to ( 6 ) in binary.
And i want it in a loop to ask the user if they wanna enter any other number or not .
Please use code tags when posting. Highlight your code and click the <> button to the right of the edit window.
gate1() is wrong, or you're calling it wrong. Your current code modifies the 3rd argument, which is bin3. So anything you enter for bin3 will be ignored. Studying that function a little more closely, it appears that it adds arguments input1 and input2, returning the sum and storing the carry result in innergate1output. Hey, that's the same functionality as my add() function. :)
Can you explain what each of the gate<n> functions is supposed to do? It's impossible to fix the code without knowing how it's supposed to work. Choosing more descriptive names for your functions and variables would go a long way.
Is this code supposed to model a specific circuit? Can you post a picture of the circuit?
I don't think you need 4 separate functions. In fact, you can solve this with 4 calls to your one-bit adder (gate1). Grab a pencil and paper, assume you have a circuit that will add 2 numbers. Now draw a circuit that will use the adders to add two 2-bit numbers. Once you have that circuit, label the wires. Each of these is a variable name. Finally, write the code to simulate the circuit.
Thank you for providing that diagram. It really helps.
The biggest problem is that you aren't actually using the return values from any of your gate() functions.
I suggest that you take the diagram and start labeling the connections. For example, the main inputs can be x1,x2 and y1,y2 for the numbers 10 and 11. The main outputs can be out1, out2 and out3.
Now label the distinct inputs and outputs for the 3 gates. Gate1 has 4 inputs and 2 outputs. Note that I'm saying 4 inputs, even though for this circuit, pairs of inputs are tied together.
Gate2 has 4 inputs and 1 output. Gate 3 has 6 inputs and 1 output. Gate 4 has 4 inputs and 1 output.
Write the functions for the gates.
Now write the code. You'll need two temp variables to handle the side output of gate1, and the output of gate2. Everything else comes from or goes to your main inputs/outputs.