Question

Can you write the following code without using any logical operators? cout << “Are you hungry?...

Can you write the following code without using any logical operators?

cout << “Are you hungry? (0-no, 1, yes) << endl;

cin >> choice;

if(choice == 1 && store == OPEN) { //get some food from the store

} else if(choice == 1 && store == CLOSED) {

//make some food at home

}

else { //I’m not hungry }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

For the following C++ program, to make it run without using any conditional statements, we make use of a 2D array of strings named ans[2][2].

This array will contain all possible outcomes for values entered by the user for hungry and store. So every time, regardless of the user input ans[choice][store] will be printed.

So, if user is hungry(1) and store is closed(0) then ans[1][0] will be printed, if he is hungry(1) and store is open(1), then ans[0][1] will be printed and so on...

CODE:

#include <iostream>

using namespace std;

int main()
{
int store,choice;
//string array for all possible answers:
string ans[2][2]={ "make some food at home","make some food at home",
"get some food from the store", "make some food at home"};
cout << "Are you hungry? (0-no, 1-yes)" << endl;
cin >> choice;
cout<<"Is store open?(0-closed, 1-open)"<<endl;
cin>>store;
cout<<ans[choice][store]; //printing the answer

return 0;
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Can you write the following code without using any logical operators? cout << “Are you hungry?...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT