Problem with break example


#1

Hi,
I cannot get the right answer in the break example.
for some reason I get “The answer is 0” instead of “The answer is 10

Here is my code:

#include <stdio.h>

int main (int argc, const char * argv[])
{
    int i ;
    for (int i = 0; i < 12; i++) {
        printf("Checking i = %d\n", i);
        if (i + 90 == i * i) {
            break;
        }
    }
    printf("The answer is %d.\n", i);
    return 0;
}

Can someone please take a look? :confused:


#2

You have declared two variables called i.

int i; for (int i = 0;
Should be

int i; for (i = 0;

The second declaration hides the first inside the loop. Outside the loop the first i is used.


#3

oh, my bad :geek:
I somehow didn’t realize the difference at first (a bit tricky). now I get it =)
Thank you.


#4

I almost got tripped up on this, but I notice the for loop we’re instructed to change (Page 51) has for (int i = 0;...) but the break section doesn’t bold that part to indicate changing it to int i; for (i = 0;...

It really could have just been done the same way as the for loop. I’m thinking this was an error in authoring the book. It should have been bold, or (better yet) should have remained the way it was in the for loop code.


#5

These forums are really an invaluable accompaniment to the printed book. Thank you. :smiley:


#6

Same mistake here… :sunglasses:


#7

So, did you get the console output as stated on page 52? I did not.

The problem seems to be two things:
[indent]1. “i” is initialized twice. First, just before the “For loop.” This causes the last “printf” to print the value of “i” as it was on that line; zero, by default. The second instance of “i” is in the “for loop” (for (int i = 0; i < 12; i++). That value of “i” is used in the first “printf” statement. The second “printf” statement is not printing the value of “i” as set outside the loop.

I believe the correct code is as follows:[code]#include <stdio.h>

int main(int argc, const char * argv[])
{
for (int i = 0; i < 12; i++) {
printf(“Checking i = %d\n”, i);
if (i + 90 == i * i) {
printf(“The answer is %d.\n”, i); // printf statement moved here to retain the value of i
break;
}
}
return 0;
}[/code]The first instance of “i” is removed as it is not needed. And the second “printf” statement is moved to just before the “break” statement. This gives me the printed output in the book:Checking i = 0 Checking i = 1 Checking i = 2 Checking i = 3 Checking i = 4 Checking i = 5 Checking i = 6 Checking i = 7 Checking i = 8 Checking i = 9 Checking i = 10 The answer is 10.Is there an errata page for the book?


#8

I’m seeing the same problem with the “Continue” example. Xcode even complains about the “int i;” is an unused variable. I changed the code, again, moving the “answer” printf just before the “break” command.[code]#include <stdio.h>

int main(int argc, const char * argv[])
{
int i; // un-needed instance of i Xcode shows "Unused variable 'i’
for (int i = 0; i < 12; i++) {
if (i % 3 == 0) {
continue;
}
printf(“Checking i = %d\n”, i);
if (i + 90 == i * i) {
printf(“The answer is %d.\n”, i); // Moved this line up from where it was in the book.
break;
}
}

return 0;

}[/code]Results:Checking i = 1 Checking i = 2 Checking i = 4 Checking i = 5 Checking i = 7 Checking i = 8 Checking i = 10 The answer is 10.Has the book been re-published? I have the first printing - November 2011.


#9

Just change this line:

for (int i = 0; i < 12; i++) {

to this:

for (i = 0; i < 12; i++) {


#10

“the same problem” is not the complaining by Xcode. It is the fact that the second printf statement is in the wrong place. See my post of Tue Apr 24, 2012 12:31 pm. :wink: You can see the console output does not match the example if the code is written exactly as it is in the book. At least in my set up, I have to move the second printf statement from where it is in the book to just above the ‘break’ command.if (i + 90 == i * i) { printf("The answer is %d.\n", i); // Moved this line up from where it was in the book. break;
I won’t argue that removing the “int” in the for loop argument will eliminate the Xcode complaint, the “i = 0” assignment is all that’s needed since i is istantiated (if that’s the word!) just before the for loop. That may also allow the printf statement to show the appropriate value for i instead of the original value of zero. I’m now working on the Challenge at the end of Chapter 10.

Either way, the code as written is probably a simple typo but there should be an errata page for any book that is written by fallible humans. :laughing:


#11

I think that the book sample contain a bad code to demonstrate break use. The message with the answer cannot be after the loop, because the answer could not exist in the interval from 0 to 11. In this case, the message will be printed like:

Because 12 is the last value of i in the loop.

Remark: Is a best option write “A answer is %d”, once the equation can have two distinct integer roots.

The book example is OK because 10 is a root of the equation. For any equation without roots from 0 to 11, the program will fail with the wrong information that 12 is a root.

Remark: The values from 0 to 11 are referred as positive integers. The most accuracy will be non-negative integers, once zero is between the tested values.

I have created an generic example to found integer solutions in a given interval for a quadratic equation. As you can observe bellow, I use break when all two roots are found.

[code]/*
Find and print integer roots of a quadratic equation with coeficients a, b, and c:
ax^2 + bx + c = 0.
*/
void findIntegerRoots(int a, int b, int c, int from, int to) {
int rootIndex = 0;

printf("Integer roots of (%d)x^2 + (%d)x + (%d) = 0 in the interval [%d, %d]:\n",
		a, b, c, from, to);

for (int x = from; x <= to; x++) {
	if (a * x * x + b * x + c == 0) {
		printf("x%d = %d\n", ++rootIndex, x);
		if (rootIndex == 2) // The two roots are found.
			break;
	}
}

}

int main(int argc, const char * argv[])
{
findIntegerRoots(1, -1, -90, -100, 100);

return 0;

}[/code]

My sample use only the knowledge in the book until the break statement instructions.


#12

A curiosity: other languages, like Java and C#, don’t permit two local variables to be declared with the same name inside the same method (function). The wrong behavior of declaring “i” twice in the C code illustrated in this topic (one declaration in the start of the main function and another in the initialization part of the for loop) is not possible in Java and C# languages.


#13

So happy this is an active forum… I was stuck on the for (int i = 0… ) issue for some time until I saw that I had 2 int’s going on there in both the for statement and the i declaration above it. thanks for pointing that out. :slight_smile:

Happy Coding!

-Wa-Dé