I HAVE THIS PROBLEM AND I DON'T KNOW HOW TO SOLVE IT

Gribouillis commented: A problem can be described with words -3

Recommended Answers

All 13 Replies

The schematic you supplied is not a problem but a design. Your post is getting downvoted for many reasons so next time take time to tell what you need to do, and where you are stumped. Don't make folk decode what you are asking.

That is, I would be guessing you want to implement the logic from the schematic into the C language, but that's me guessing.

Don't make folk guess what your questions are.

I'm not so good in C Language, can someone descripe me how to write it???

You didn't state your problem but well, read what you wrote and it's going to get downvoted.

Maybe this is some homework assignment but you left out all the details so you left folk guessing what the issue is here.

As to not so good with C, you practice and get better. You won't get better if folk write code for you.

I'll start you off, but I'm not doing all of it. Write a few helper functions, one function for every type of gate you have. I'll give you the NAND gate function. Make sure you either #include stdbool.h in order to define true, false, and bool, or don't include stdbool.h and #define these yourself at the top of the program.

bool NAND(bool input1, bool input2)
{
    if(input1 && input2)
    {
        return false;
    }
    return true;
}

Use this NAND function and make your other gate functions, like AND and OR. Once you have these helper function, you'll define five boolean input variables or one boolean array in your main function. Assign five boolean values to these inputs and call your helper functions in a way that matches your schematic. Then display the result (true or false) in main.

commented: Great help for the OP (I hope...) +15

Thank u very much AssertNull

I have used the function that he told me before but now i have this problem, what can I do ???

Don't use attachments/screenshots if you can help it. It's impossible for me to copy and paste your code and make adjustments to it. I instead have to type out the attachment and make changes. Copy and paste your code as code in the future please. It's easier for all concerned that way.

I have used the function that he told me before but now i have this problem, what can I do ???

You want to copy and paste my code verbatim and stick it above main (or stick it below main and stick the function prototype above main. You do not want to change it at all (well you CAN of course, it's your program, not mine, it just won't work when you change it as you have changed it).

What you definitely DO NOT want to do is change it as you did and you especially do not want to put the function INSIDE the main function. You want something on the lines of this:

#include <stdbool.h>
bool NAND(bool input1, bool input2);
bool AND(bool input1, bool input2);
bool XOR(bool input1, bool input2);

int main()
{
    bool xorOutput, andOutput, nandOutput;
    // change these to whatever you want, vary them, make sure the code works for all combinations.
    bool inputa = false;
    bool inputb = false;
    bool inputc = false;
    bool inputd = false;
    bool inpute = false;

    // now call a few functions
    andOutput = AND(inputa, inputb);
    xorOutput = XOR(inputc, inputd);

    // note that the NAND INPUTS will be the AND and XOR OUTPUTS
    nandOutput = NAND(andOutput, xorOutput);

    // more code below to match the schematic

    return 0;
}


bool NAND(bool input1, bool input2)
{
    if(input1 && input2)
    {
        return false;
    }
    return true;
}

bool AND(bool input1, bool input2)
{
    // write logic for this yourself.  Just return true so it compiles
    return true;
}

bool XOR(bool input1, bool input2)
{
    // write logic for this yourself.  Just return true so it compiles
    return true;
}

The program above compiles and is a skeleton for your program. You'll need to add more code. But compare your code to mine, particularly the placement of the functions. From your code, it appears that you are brand new to C, so I suggest that you put aside this particular assignment and take some basic tutorials in C first, then come back to this assignment.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
bool NAND(bool inputa, bool inputb);
bool AND(bool inputa, bool inputb);
bool XOR(bool inputa, bool inputb);
bool OR (bool inputa,bool inputb);


int main(int argc, char *argv[]) 
{
    bool XOROutput, ANDOutput, NANDOutput, OROutput;
    bool inputa = false;
    bool inputb = false;
    bool inputc = false;
    bool inputd = false;
    bool inpute = false;

    ANDOutput = AND(inputa,inputb);
    XOROutput = XOR(inputc,inputd);

    NANDOutput = NAND(ANDOutput , XOROutput);
    OROutput = OR(NANDOutput , inpute);

    return 0;
}

bool NAND (bool inputa, bool inputb)
{
    if(inputa && inputb)
    {
        return false;
    }
    return true;
}

bool AND (bool inputa, bool inputb)
{
    if (inputa && inputb)
    {
        return false;
    }
    return true;
}

bool XOR (bool inputc, bool inputd )
{
    if (inputc ^ inputd)
    {
        return false;
    }
    return true;
}

bool OR (bool nandOutput, bool inpute)
{
    if (bool nandOutput || bool inpute)
    {
        return false;
    }
    return true;
}

it's okeyy guys??

I HOPE

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

bool INPUT ();
bool AND (bool inputa, bool inputb);
bool XOR (bool inputc, bool inputd );
bool NAND (bool ANDOutput, bool XOROutput);
bool OR (bool NANDOutput, bool inpute);

int main(int argc, char *argv[]) 
{
    int i,j;

    bool XOROutput, ANDOutput, NANDOutput, OROutput;
    bool inputa = INPUT();
    bool inputb = INPUT();
    bool inputc = INPUT();
    bool inputd = INPUT();
    bool inpute = INPUT();

    ANDOutput = AND(inputa,inputb);
    XOROutput = XOR(inputc,inputd);
    NANDOutput = NAND(ANDOutput , XOROutput);
    OROutput = OR(NANDOutput , inpute);

    printf("\n%s\n", OROutput ? "true" : "false");
    return 0;
}

bool INPUT ()
{
    int num;
    scanf("%d", &num);
    if(num > 1 && num < 0)
    {
        exit(1);
    }
    if(num)
    {
        return true;
    }
    return false;
}

bool AND (bool inputa, bool inputb)
{
    if (inputa && inputb)
    {
        return true;
    }
    return false;
}

bool XOR (bool inputc, bool inputd )
{
    if (inputc ^ inputd)
    {
        return true;
    }
    return false;
}

bool NAND (bool ANDOutput, bool XOROutput)
{
    if (ANDOutput && XOROutput)
    {
        return false;
    }
    return true;
}

bool OR (bool NANDOutput, bool inpute)
{
    if (NANDOutput || inpute)
    {
        return true;
    }
    return false;
}

it's okeyy guys??

You are posting way too fast. It's your responsibility, not ours, to debug and test your code. Clearly this is NOT okay, so why are you asking whether it is okay? It suggests that you did not compile this program. The compiler will immediately point out where the error is. Anyone with any C experience should be able to immediately look at the line and fix it. Your post suggests that you made an attempt, immediately posted that attempt, THEN you tried to debug your program.

That's not how it works. You make your attempt, try to debug the program, get stuck, try some more, get stuck again, run out of ideas, THEN post your last attempt, along with your error, and a pertinent question. That's why you got down-voted. I was going to down-vote you myself, with this post, but you appear to have mostly fixed it, so you apparently CAN fix those errors without any help. That brings us back to why you made THIS post. Again, effort first on your part is required. Part of that effort is you testing and fixing your own code before posting, and also taking the time to actually formulate a question.

I HOPE

Well, does it compile? Does it run? Does it give the correct output? You have 32 possible sets of true/false inputs you can throw at this program. You don't need to brute-force it and hit your program with all 32 combinations, but how about a half a dozen? Do they all give the right answer? I also see that you intend to exit the program if the user enters anything but 0 or 1 for input. So I assume you entered a number like 5 or -4 and the program exited? You need to test the program fully.

Lastly, the computer doesn't care, but humans care, so you should probably name your function parameters input1 and input2 as I did. Why? Well, suppose I want to take your helper functions and use them with a different schematic. For example, perhaps my OR inputs in this new schematic are NOT the NAND output and input e. I want to be able to use your helper function and I want to be able to follow the logic of it. It's going to be confusing if I have a variable called NANDOutput. I might think I made a mistake because my new schematic doesn't have a NAND gate. Perhaps that means that I cannot use your helper function? The helper function should be as independent as possible from the function that CALLS it. Therefore name the variables in that function as generically and accurately as possible. Again, the computer/compiler does not care, but the human being who is trying to use your helper function in a different program does care.

You could also write your gate functions a bit shorther.
Example the AND function:

bool AND (bool inputa, bool inputb)
{
    return inputa && inputb;
}
You could perhaps (depends on the C-compiler) save a few computercycles, but the way it is written is perfect.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.