# My challenge solution

#1

I’m sure its pretty similar to everyone else’s solution, but wanted to make sure I wasn’t doing anything crazy wrong. My hope is to write as compactly as possible while leaving good documentation.

[code]int main(int argc, const char * argv[])
{

``````// Create and declare the two variables
float firstValue = 3.14;
float secondValue = 42.01;

// Create a new variable of type double and give it the value of the first float plus the second float
double combinedValue = firstValue + secondValue;

// Give user the two float values and the new double value
printf("%.2f plus %.2f equals %.2f \n", firstValue, secondValue, combinedValue);

//End program
return 0;
``````

}[/code]

#2

Looks good. Mine was the same but I used a float instead of a double. It is generally a good habit to use variable types of the smallest size possible to conserve memory.

#3

You can do away with those comments stating the obvious and thus not providing any useful information:

Comment stating the obvious:

``````...
// Create and declare the two variables
float firstValue = 3.14;
float secondValue = 42.01;
...``````

Much better to comment on why you are making those declarations, and on the choice of numbers:

``````// Start with two randomly chosen real numbers
float firstValue = 3.14;
float secondValue = 42.01;
...``````

Comment stating the obvious:

``````// Create a new variable of type double and give it the value of the first float plus the second float
double combinedValue = firstValue + secondValue;``````

Much better to state why you are adding them:

``````// Combine them linearly
double combinedValue = firstValue + secondValue;``````

#4

Good point, I’ll be sure to keep that in mind.

#5

[quote=“ibex10”]You can do away with those comments stating the obvious and thus not providing any useful information:

Comment stating the obvious:

``````...
// Create and declare the two variables
float firstValue = 3.14;
float secondValue = 42.01;
...``````

Much better to comment on why you are making those declarations, and on the choice of numbers:

``````// Start with two randomly chosen real numbers
float firstValue = 3.14;
float secondValue = 42.01;
...``````

Comment stating the obvious:

``````// Create a new variable of type double and give it the value of the first float plus the second float
double combinedValue = firstValue + secondValue;``````

Much better to state why you are adding them:

``` // Combine them linearly double combinedValue = firstValue + secondValue; ```[/quote]

That does seem more helpful, or at least adding the why in the meantime. I’m sure if I’m looking at some old code I’m more likely to forget “why” I did somehting, then exactly what it is doing. Thanks!

#6

I did the same thing. I’m just having trouble with printf.

I understand how to implement it, but the %.2f doesn’t make sense to me and I didn’t see it directly referenced in the chapter. It’s obvious it’s a place holder for the variable listed at the end of the string, but why %.2f?

Thanks for the help!

#7

[quote=“southwest”]I did the same thing. I’m just having trouble with printf.

I understand how to implement it, but the %.2f doesn’t make sense to me and I didn’t see it directly referenced in the chapter. It’s obvious it’s a place holder for the variable listed at the end of the string, but why %.2f?

Thanks for the help![/quote]

.2 is telling the code to only print the numbers upto 2 decimal places, so in this case 3.14 and 42.01 and 45.15. You can remove the .2 and leave it as %f and the result will show as 3.140000000 or replace .2 for .1 and result will be 3.1 … So on.

Hope this clarifies, just try editing the code and you will see.