Need help on these questions

sarrocks
7 years ago

0

Guys I need a little help on these questions , all are of C language.
1.) If (condition)
printf(“are you”);
else
printf(“How”);
The question is to replace (condition) with something that will give output -
“How are you” in a single run.

2.)int a=10,b=20,c=30,d;
a=b==c;
if(a==0)
printf(“%d,%d,%d,%d”,a,b,c,d);
else
printf(“%d,%d,%d,%d”,d,c,b,a);

Find output of the program
Please also comment on the difficulty level of these for amateurs.
Thank you.

13replies
7voices
241views
SIGKILL [r4v463]
7 years ago | edited 7 years ago

0

Is it a homework for school or something like that?

The first question doesn’t have any sense since we don’t have any context (variables for example).
The second question is really easy.

Both questions have missing \n at the end of printf.

Darwin [DIDIx13]
7 years ago

0

1.) Can’t be possible if you can just edit (condition)

Why ? Because If else fonction need, a boolean value, which can only be “true” or “false”. You can’t have both.

Or if you can edit more just do an elseif.

2.) Can’t work either.

For multiple reason :

D is Unassigned.

a = b == c; Mean nothing : You can’t convert like this bool to int.
Explanation : a = 13 // You assigne 13 to a that’s ok
b == c // It’s a boolean question, is b = c ? And the answer is false.
So you can’t say a = false because a is an integer wich means he understand number, false is a boolean value not a number.
Exemple : bool a = true; // We just assigne true to a.
a = b ==c // if b = c assigne true to a, else assigne false.

I’m still here to answer if you have any question.

Have fun !

SIGKILL [r4v463]
7 years ago

0

[quote=DIDIx13]a = b == c; Mean nothing : You can’t convert like this bool to int.[/quote]
You’re wrong on this one, he has specified that he was on C and the result of a comparison is an int and not a boolean in C.

Darwin [DIDIx13]
7 years ago

0

Really ? Sorry in C# I remember it was.

Sorry about it ^^

Ice Booda [0x1ceb00da]
7 years ago | edited 7 years ago

0

I believe on C# that would also work. A bit rusty on my C# knowledge bit isn’t the bool just a typedef uint

And I think the value of d depends on the compiler how it initializes variables

Darwin [DIDIx13]
7 years ago

0

+0x1ceb00da

printf doesn’t exist, you should use Console.WriteLine(); as exemple in the console.


0

True enough, I was merely commenting on the bool part. Now at vacation and only have my phone but will test this when I get back home.

Darwin [DIDIx13]
7 years ago

0

The bool part doesn’t work

a = b == c;

Will return you an error in C#

dloser
7 years ago

0

First one is definitely possible; it’s kind of a trick question. But yeah… sound like homework or challenge or something.

Smyler [WHGhost]
7 years ago | edited 7 years ago

0

Yep, I like the first one’s ideas.

sarrocks
7 years ago

0

Guys Please forget about syntax errors and all that…..
We are only asked to give the logic to get the solution, syntax doesnt matter please help me with the logic.


0

ah yes now with a fresh head I see that 1 is also possible. Forgot that it has a return value

Pawda [Memoria]
7 years ago

0

@sarrocks
For the first question, if you read carefully, you need to print “How are you”, however, the printf calls in the if/sel only displays “How” and “are you”, this should already ring a bell and helps you find an easy answer :)

As for the second one… If you cannot figure it out, why not try to run the code yourself?

The difficulty of these two exercises is pretty basic imo and would fit in the first week of C learning.
The question n1 could have been more complex but it doesn’t seem to be the case since you didn’t mention any restriction.

@DIDIx13
You actually can go into the if and the else on the same run in C, but it’s not needed in this case.

Discussion thread has been locked. You can no longer add new posts.
1 of 14

This site only uses cookies that are essential for the functionality of this website. Cookies are not used for tracking or marketing purposes.

By using our site, you acknowledge that you have read and understand our Privacy Policy, and Terms of Service.

Dismiss