# Challenge Solution

#21

Why is that ibex? I would be really interested to know the potential pitfalls in doing this.

In the Big Nerd Ranch book they call a function from the printf function, for e.g. on pg. 51 the function abs() gets called in the printf there. Maybe that is more permissable because it is a mathematical and pre-defined function which might make a difference?

Appreciate your help and thanks for responding!

#22

The Reason: Getting in the habit of minimising the number of lines inevitably leads to hard-to-understand and hard-to-maintain code.

To demonstrate this, let’s do a contrived but a simple exercise.

Shown below are different versions of the same code written in different styles - concisely and verbosely.

Verbose Style
1-1.c (Version 1)

``````void Foo (int, int);
void Bar (int, int);
void FooBar (int, int, int);

const int fooVal = Foo (2, 3);
const int barVal = Bar (2, 3);
FooBar (1, fooVal, barVal, 6);``````

1-1.c (Version 2)

``````void Foo (int, int);
void Bar (int, int);
void FooBar (int, int, int);

const int fooVal = Foo (2, 3);
const int barVal = Bar (4, 3);
FooBar (1, fooVal, barVal, 6);``````

Concise Style
1-2.c (Version 1)

``````void Foo (int, int);
void Bar (int, int);
void FooBar (int, int, int);

FooBar (1, Foo (2, 3), Bar (2, 3), 6);``````

1-2.c (Version 2)

``````void Foo (int, int);
void Bar (int, int);
void FooBar (int, int, int);

FooBar (1, Foo (2, 3), Bar (4, 3), 6);``````

Now do the following.

Create 4 files from above - 1-1.c, 1-1.c.1, 1-2.c, and 1-2.c.1:

1. Copy and paste version 1 of 1-1.c to a text file named 1-1.c;
2. Copy and paste version 2 of 1-1.c to a text file named 1-1.c.1;
3. Copy and paste version 1 of 1-2.c to a text file named 1-2.c;
4. Copy and paste version 2 of 1-2.c to a text file named 1-2.c.1;

Using your favourite tool (for example, command-line diff tool or FileMerge App), compare:

• 1-1.c to 1-1.c.1; and
• 1-2.c to 1-2.c.1

In which style of code did you find it easier to be able to tell precisely what has changed?

#23

Hello,

Thank you for all your takes! It really start to be understanding, and I think that it’s a wonderful idea to not publish the solutions in the book but let everyone try and communicate on the forum.

For my part (I am brand new in programming) I often receive the debugger with "Expected ')'
But what does it mean in this code (which fails when I try to build because of "Expected ‘)’

``````#include <stdio.h>

int main(int argc, const char * argv[]) {
int numberBase = 5;
int squaredNr = numberBase * numberBase;

printf("\"%d" squared is "%d", numberBase, squaredNr\n);
return 0;
}``````

#24

[quote]For my part (I am brand new in programming) I often receive the debugger with "Expected ')'
But what does it mean in this code (which fails when I try to build because of "Expected ‘)’
[/quote]
Hello MimaGo,

When communicating with a human being or a machine in a language they understand, it is important that you follow the rules (syntax + semantics) of that language. This is especially true when you are communicating with a machine.

In the context of the problem you are experiencing with your code, the machine is the compiler that the Xcode is using.

Unlike humans (because they can be more forgiving), compilers are very fussy about the rules of the language they understand. You must strictly follow the rules of the language. If you break the rules, they complain and stop listening.

Because you did not follow the rules, the compiler got upset and spat out the errors to you.

Now compare your code to this:

``````#include <stdio.h>

int main(int argc, const char * argv[]) {
int numberBase = 5;
int squaredNr = numberBase * numberBase;

printf ("\"%d squared is %d", numberBase, squaredNr);
return 0;
}``````

Can you see the differences?

I hope this helps.

#25

I think he wants to print the quote signs, so I will offer this variation of the printf()

[code]#include <stdio.h>

int main(int argc, const char * argv[]) {
int numberBase = 5;
int squaredNr = numberBase * numberBase;

``````printf("\"%d\" squared is \"%d\"\n", numberBase, squaredNr);
return 0;
``````

}[/code]
Mitch

#26

[quote=“ibex10”]
Now compare your code to this:

``````#include <stdio.h>

int main(int argc, const char * argv[]) {
int numberBase = 5;
int squaredNr = numberBase * numberBase;

printf ("\"%d squared is %d", numberBase, squaredNr);
return 0;
}``````

Can you see the differences?

I hope this helps.[/quote]

Thank you Sandro, YES it helped a lot.

Here is my code, now, that works, and that resolves the challenge:

[code]#include <stdio.h>

int main(int argc, const char * argv[]) {
int numberBase = 5;
int squaredNr = numberBase * numberBase;

``````printf ("\"%d\" squared is \"%d\"", numberBase, squaredNr);
return 0;
``````

}[/code]

I didn’t understood at first the importance of the repeated backslash to get " as a character.
Now, the result is
"5" squared is "25"
as challenged in the book.

YES ! Thank you very much !

#27

I came up with the following code for this challenge:

[code]#include <stdio.h>

int squaredInt(int value)
{
int numSquared = value * value;
printf(""%d" squared is “%d”", value, numSquared);

``````return numSquared;
``````

}

int main(int argc, const char * argv[])
{
squaredInt(5);
return 0;
}[/code]

#29

I think Big Nerd Ranch wanted us to call a function and also include quotations around 5 and 25, so here is what I came up with.

#30

and here is what my final result looked like: