Thread: C program related to logic gates!

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    12

    C program related to logic gates!

    Hi,
    have been asked to produce a c program allowing the user to simulate combinational logic gates where the user inputs binary values for A,B and C and the output circuit is displayed. have been given a template array to use with the program which contains the binary info for AND, OR and NOT gates.
    the first part of the spec tells me to Start by adding C functions for simulating additional logic gates: XOR, XNOR, NAND and NOR.

    all i am looking for is how would i go about answering the first part.... adding c functions for the XOR, XNOR etc logic gates as this part is confusing me?

    The template code is shown below!

    Code:
    #include<stdio.h>
    int And(int a, int b);
    int Or(int a, int b);
    int Not(int a);
    void main()
    {
     ///where main body of code will go
    }
    int And(int a, int b)
    {
     int output;
     if(a==0 && b==0)
      output=0;
      if(a==1 && b==0)
      output=0;
     if(a==0 && b==1)
      output=0;
     if(a==1 && b==1)
      output=1;
     return (output);
    }
    int Or(int a, int b)
    {
     int output;
     if(a==0 && b==0)
      output=0;
      if(a==1 && b==0)
      output=1;
     if(a==0 && b==1)
      output=1;
     if(a==1 && b==1)
      output=1;
     return (output);
    }
    int Not(int a)
    {
    int output;
     if(a==0 )
      output=1;
     if(a==1 )
      output=0;
      return (output);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    I suggest that you simplify, e.g.,
    Code:
    int And(int a, int b)
    {
        return a && b;
    }
    Similiarly, Or and Not should be implemented using || and ! to keep it simple.

    For NAND, you can implement it in terms of Not and And. Likewise for the other "gates". (Since NAND is a universal gate, you could implement the other functions in terms of Nand alone, but that's probably rather unnecessary.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    12
    '' For NAND, you can implement it in terms of Not and And. Likewise for the other "gates". (Since NAND is a universal gate, you could implement the other functions in terms of Nand alone, but that's probably rather unnecessary.) ''



    • I am familiar with logic gates writing them down etc but how do i go about putting that as code? obviously a NAND gate is an AND gate into a NOT gate but how do i put that as a function in my code?


  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by yellowmania
    how do i go about putting that as code? obviously a NAND gate is an AND gate into a NOT gate but how do i put that as a function in my code?
    Composition of function calls, e.g.,
    Code:
    return Not(And(a, b));
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Quote Originally Posted by yellowmania View Post
    ... but how do i put that as a function in my code?
    LaserLight has given her usual excellent advice.

    But maybe I can get you started by asking a question or two.
    From your original code or template which line is the declaration of the And function?
    Which lines are the definition of the And function?
    What name would you give your function with the NAND operaton to be consistent with the names of the other functions given in your original code or template?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program related in using RANDOM numbers using C.
    By Ashleykaye in forum C Programming
    Replies: 3
    Last Post: 09-11-2011, 10:30 AM
  2. Help with first c++ program (math-related)
    By necrovamp in forum C++ Programming
    Replies: 4
    Last Post: 09-23-2010, 12:33 PM
  3. Cannot get my program to exit properly (switch related)
    By Skarjak in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2005, 10:14 AM
  4. Logic Gates
    By JFK in forum C Programming
    Replies: 0
    Last Post: 10-21-2001, 05:56 AM
  5. non program related questions about C
    By Nova in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 03:42 PM

Tags for this Thread