Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Write C code to satisfy the following five requirements
• Requirement 1: When MyInput1 has a value of 1, MyInput2 has a value of 0 and
MyInput3 has a value of 0, then set MyOuput1 = 1, MyOuput2 = 0 and MyOuput3 = 0;
• Requirement 2: When MyInput1 has a value of 1, MyInput2 has a value of 1 and
MyInput3 has a value of 0, then set MyOuput1 = 0, MyOuput2 = 1 and MyOuput3 = 0;
• Requirement 3: When MyInput1 has a value of 1, MyInput2 has a value of 1 and
MyInput3 has a value of 1, then set MyOuput1 = 0, MyOuput2 = 0 and MyOuput3 = 1;
• Requirement 4: For all other combinations of values for MyInput1, MyInput2,
MyInput3, then set MyOuput1 = 0, MyOuput2 = 0 and MyOuput3 = 0;
• Requirement 5: Lastly print the values of the following three variables to the user:
MyOutput1, MyOutput2 and MyOutput3.


What I have tried:

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

int main()
{
    bool MyInput1, MyInput2, MyInput3;
    bool MyOutput1, MyOutput2, MyOutput3;

    MyInput1 = 1;
    MyInput2 = 1;
    MyInput3 = 1;

    if (MyInput1&&!MyInput2&&!MyInput3)
    {
        MyOutput1 = 1;
        MyOutput2 = 0;
        MyOutput3 = 0;

    }
    else if (MyInput1&&MyInput2&&!MyInput3)
    {
        MyOutput1 = 0;
        MyOutput2 = 1;
        MyOutput3 = 0;

    }
    else if (MyInput1&&MyInput2&&MyInput3)
    {
        MyInput1 = 0;
        MyInput2 = 0;
        MyInput3 = 1;

    }
    else
    {
        MyOutput1 = 0;
        MyOutput2 = 0;
        MyOutput3 = 0;

    }
    printf("MyOutput1 = %d",MyOutput1);
    printf("MyOutput2 = %d",MyOutput2);
    printf("MyOutput3 =%d",MyOutput3);

    return 0;
}
Posted
Updated 18-Oct-20 20:11pm
v2

Look at your code:
C++
else if (MyInput1&&MyInput2&&MyInput3)
    {
        MyInput1 = 0;
        MyInput2 = 0;
        MyInput3 = 1;

    }
Compare that with a branch that works:
C++
else if (MyInput1&&MyInput2&&!MyInput3)
    {
        MyOutput1 = 0;
        MyOutput2 = 1;
        MyOutput3 = 0;

    }

Can you see a difference?





Hint: is MyInput1 the same variable as MyOutput1?
 
Share this answer
 
Comments
Nazo77 18-Oct-20 16:44pm    
Oh My goodness i didn't see that i have written a duplicate Scenario and yes that was very wrong because MyInput was being treated the same way as MyOutput
OriginalGriff 18-Oct-20 17:40pm    
:laugh: Don't worry - we all do it!
I frequently read what I meant to write rather than what I did ... :blush:
Nazo77 18-Oct-20 18:17pm    
😂😂they way i was freaking out as to why its not working yet it seemed to be perfectly written.
OriginalGriff 19-Oct-20 4:16am    
Yeah. I get that as well ... the debugger can help you there - it shows you exactly what is going on, and inspires that "face palm moment" when you realize you typed it wrong. LOL
CPallini 19-Oct-20 2:16am    
5.
You are assigning MyInput1, ..2, ..3 instead of MyOutput1, ..2, ..3 when matching condition 3. Otherwise, it looks OK to me.

If you turn on warnings, you might have got a warning about MyOutput1, ..2, ..3 not being initialized. Maybe if you had initialized the variables to, say, -1, you might have noticed your coding mistake for yourself.
 
Share this answer
 
Comments
Nazo77 18-Oct-20 16:50pm    
Code blocks did not warn me at all i don't know why but i have rectified that and the code is working perfectly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900