this is a logic gate assignment in college

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.




#include<iostream>
#include<cmath>
#include<string>


using namespace std;

double binary_decimal(int input1, int input2, int input3, int input4);
bool gate1(bool input2, bool input4, bool &innergate1output);
bool gate2(bool input1, bool &innergate1output, bool &outputgate2to4);
bool gate3(bool input1, bool input3, bool innergate1output);
bool gate4(bool input3, bool outputgate2to4);


int main()
{
int x = 0;

bool input1 = 0, input2 = 0, input3 = 0, input4 = 0, outputgate2to4 = 0, innergate1output = 0;


cout << "Enter the binary number with space between the numbers: " << endl;
cin >> input3;
cin >> input1;
cin >> input4;
cin >> input2;

cout << "You enter these numbers: " << input3 << input1 << input4 << input2 << " in binary = " <<endl;

gate1(input2, input4, innergate1output);
gate2(input1, innergate1output, outputgate2to4);

gate3(input1, input3, innergate1output);
gate4(input3, outputgate2to4);


binary_decimal(input3, input1, input4, input2);

cout << binary_decimal(input3, input1, input4, input2) << " in decimal" << endl;


system("pause");
return 0;
}



bool gate1(bool input2, bool input4, bool &innergate1output)
/*logic gate 1 function*/
{
bool temp1 = input2 || input4;
bool temp2 = input2 && input4;
innergate1output = temp2;
bool temp3 = !temp2;
bool outputgate1 = temp1&&temp3;


return outputgate1;
}

bool gate2(bool input1, bool &innergate1output, bool &outputgate2to4)
/*logic gate 2 function*/
{
bool temp1 = innergate1output && input1;
bool temp2 = !temp1;
bool temp3 = temp2 && innergate1output;
bool temp4 = input1 && temp2;
bool outputgate2 = temp3 || temp4;
outputgate2to4 = outputgate2;

return outputgate2;
}

bool gate3(bool input1, bool input3, bool innergate1output)
/*logic gate 3 function*/
{
bool temp1 = innergate1output && input1;
bool temp2 = input3 && innergate1output;
bool temp3 = input3 && input1;
bool temp4 = temp2 || temp3;
bool outputgate3 = temp1 || temp4;


return outputgate3;
}

bool gate4(bool input3, bool outputgate2to4)
/*logic gate 4 function*/
{
bool temp1 = input3 && outputgate2to4;
bool temp2 = !temp1;
bool temp3 = temp2 && outputgate2to4;
bool temp4 = temp2 && input3;
bool outputgate4 = temp3 || temp4;

return outputgate4;
}

double binary_decimal(int input3, int input1, int input4, int input2)
/*Function to convert binary to decimal.*/
{
double temp4 = input2*pow(2, 0);
double temp3 = input4*pow(2, 1);
double temp2 = input1*pow(2, 2);
double temp1 = input3*pow(2, 3);

double result = (temp1 + temp2) + (temp3 + temp4);
return result;
}
[/code]

Last edited on
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
1
2
3
4
5
bool add(bool bit1, bool bit2, bool &carry)
{
    carry = bit1&bit2;
    return bit1 ^ bit2;
}

Now you can use this to add the numbers.

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 .




[#include<iostream>
#include<cmath>
#include<string>


using namespace std;

double binary_decimal(int bin1, int bin2, int bin3, int bin4);
bool gate1(bool input1, bool input2, bool &innergate1output);
bool gate2(bool input3, bool input4, bool &innergate1output);
bool gate3(bool input3, bool input4, bool innergate1output);
bool gate4(bool input4, bool outputgate2);

int main()
{

int x = 0;
bool bin1 = 0, bin2 = 0, bin3 = 0, bin4 = 0;
cout << "Enter the first binary number: " << endl;
cin >> bin3 >> bin1 >> bin4 >> bin2;
int decimal = 0;


if (bin1 == 0 || bin1 == 1 && bin2 == 0 || bin2 == 1 && bin3 == 0 || bin3 == 1 && bin4 == 0 || bin4 == 1)
{
gate1(bin1, bin2, bin3);
gate2(bin1, bin2, bin3);
gate3(bin1, bin2, bin3);
gate4(bin1, bin2);
binary_decimal(bin1, bin2, bin3, bin4);


}

else
cout << "Wrong binary number try again";

cout << bin1 << bin2 << bin3 << bin4 << " in binary = " << binary_decimal(bin1, bin2, bin3, bin4) << " in decimal" << endl;


system("pause");
return 0;
}


bool gate1(bool input1, bool input2, bool &innergate1output)
{
bool temp1 = input1 || input2;
bool temp2 = input1 && input2;
innergate1output = temp2;
bool temp3 = !temp2;
bool outputgate1 = temp1&&temp3;
bool bin1 = outputgate1;

return outputgate1;
}

bool gate2(bool input3, bool input4, bool &innergate1output)
{
bool temp1 = innergate1output && input3;
bool temp2 = !temp1;
bool temp3 = temp2 && innergate1output;
bool temp4 = input3 && temp2;
bool outputgate2 = temp3 || temp4;



return outputgate2;
}

bool gate3(bool input3, bool input4, bool innergate1output)
{
bool temp1 = innergate1output && input3;
bool temp2 = input4 && innergate1output;
bool temp3 = input4 && input3;
bool temp4 = temp2 || temp3;
bool outputgate3 = temp1 || temp4;
bool bin3 = outputgate3;

return outputgate3;
}

bool gate4(bool input4, bool bin2)
{
bool temp1 = input4 && bin2;
bool temp2 = !temp1;
bool temp3 = temp2 && bin2;
bool temp4 = temp2 && input4;
bool outputgate4 = temp3 || temp4;
bool bin4 = outputgate4;

return outputgate4;
}

double binary_decimal(int bin1, int bin2, int bin3, int bin4) /* Function to convert binary to decimal.*/
{
double temp1 = bin4*pow(2, 0);
double temp2 = bin3*pow(2, 1);
double temp3 = bin2*pow(2, 2);
double temp4 = bin1*pow(2, 3);
double result = temp1 + temp2 + temp3 + temp4;

return result;
}
][]
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?
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/general/195585/
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.
Here is the pic of the gates and how they suppose to work and how they should read the input and what is the output.

https://www.dropbox.com/s/9t9ktqba0v59w2l/Screenshot%20%2812%29.png?dl=0
Last edited on
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.
closed account (48T7M4Gy)
Setup the classes the rest falls into place. Then derive functions to 'hardwire' model the components on the diagram and then hook those up.
Last edited on
closed account (48T7M4Gy)
Answer: 10+11=101
Program ended with exit code: 0
Last edited on
closed account (48T7M4Gy)
Would have known that. :)
Last edited on
Topic archived. No new replies allowed.