SPOILER WARNING: My solutions for Challenge 2


#1

I thought I would share my solutions for Challenge 2 p. 21. If it is not ok to share solutions please delete this post.

[code]#include <stdio.h>

int main (int argc, const char * argv[])
{
// Question 1
int l = 5;
int m = 7;

if (((l < m) && (l + m < 10)) || (m < l)) {
    printf("True\n");
} else {
    printf("False\n");
}


// Question 2
if (((l < m) && (l + m > 10)) && (42 > 32)) {
    printf("True\n");
} else {
    printf("False\n");
}


// Question 3
if ((5 != 0) || (l == m)) {
    printf("True\n");
} else {
    printf("False\n");
}


// Question 4
int i = 20;
int j = 25;

int k = (i > j) ? 10 : 5;

if (5 < j - k) { // first expression
    printf("The first expression is true.");
} else if (j > i) { // second expression
    printf("The second expression is true.");
} else {
    printf("Neither expression is true.");
}

return 0;

}
[/code]

Maybe someone will find this helpful :wink:


#2

I was trying to do the second challenge of the book, but i faced with something i couldn’t understand:

int k = (i > j) ? 10 : 5

why (i > j) if i = 20 and j = 25? (20 > 25)?
And, why it is 10 : 5?

Could you explain me? Please.


#3

[quote=“renan976”]I was trying to do the second challenge of the book, but i faced with something i couldn’t understand:

int k = (i > j) ? 10 : 5

why (i > j) if i = 20 and j = 25? (20 > 25)?
And, why it is 10 : 5?

Could you explain me? Please.[/quote]

Hi renan976,

Its been a while since I did the exercise, so I don’t have the challenge in front of me, but let me see if I can help you out :wink:

int k = (i > j) ? 10 : 5;

The last line of code might be confusing at first as it uses the conditional operator, but we can rewrite it like this,

int k; if (i > j) { //false because i < j int k = 10; else { int k = 5 }

So, if you look at the first line of code with the conditional operator

int k = (i > j) ? 10 : 5;

you will see that if the condition (i > j) is TRUE then int k will be assigned the value 10. Conversely, if (i > j) is FALSE, the value of k will be 5.

If you don’t feel comfortable with the conditional operator, write out the code using if-else statements and once you get a hang of it you can come back to the conditional operator.

I hope that helps :wink:


#4

thank you :slight_smile:
I’m almost understanding it :smiley:

Just one more thing.
If i = 20 and j = 25; why (i > j)?

Forgot it, I’ve already get it :wink:


#5

Cool it sinks in eventually when you’ve done it enough times :laughing:

I had a look in the book and that’s just the way the code is in the question. Of course it could have been (i < j) or (j > i) or something else. You can mess around with the code and experiment and try to guess the result before you run the code. In the end the important part is that you understand how if/else works.


#6

Forgive my ignorance, please, but I’m reading the Kindle version of this book and I don’t see questions 1-3 in the book from the code submitted by the original poster.

I tried to reference the page cited (p. 21) but I have no example questions other than the Challenge at the end of the chapter. Am I missing something?

Thanks!