Solution to Chapter 7 Challenge


#1

#include <stdio.h>

int main(int argc, const char * argv[])
{
int x = 99;
while (x<100) {
printf(“The number is %d\n”,x);
x-=3;
if ((x%5==5) || x%5==0 ) {
printf(“Yay, Found One! %d\n”,x);
continue;
}
if (x<0) {
break;
}
}
return 0;
}


#2

Thanks for posting. Looks a bit complicated though… here is mine:

int main (int argc, const char * argv[])
{
	for ( int i = 99; i >= 0; i-= 3) {
		printf("%d\n", i);
		if ( i % 5 == 0) {
			printf("Found one!\n");
		}
	}
	
	return 0;
}

#3

Tried even shorter:

[code]#include <stdio.h>

int main(int argc, const char * argv[])
{
for (int i = 99; i >= 0; i -= 3) printf("%d%s\n", i, i % 5 ? “” : " Found one!");
return 0;
}[/code]

I would be grateful for the comments if any :slight_smile:


#4

More complicated to read, but concise :slight_smile: My solution was like titicaca’s above. I like it for readability but yours is cool because of the ternary operator! Tweak comment: I added \n at the beginning of your " Found one!" text to make the output display on a separate line as requested by the challenge:

for (i = 99; i >= 0; i -= 3) printf("%d%s\n", i, i % 5 ? "" : "\nFound one!");
Always great to see someone else’s solutions. I’m trying to get used to the ternary operator. Sure makes for conciseness but my brain doesn’t go to it naturally yet…


#5

I’m running into problems with two errors, the first being “If statement has empty body” (line 7), the second being “Expected expression” (line 9) I believe it is a problem with my braces }{ because as a beginner I’m having the most trouble determining where they should go in each program. Maybe it’s an issue trying to set an if/else statement into a do/while?
Help greatly appreciated.

#include <stdio.h>

int main (int argc, const char * argv[])
{
    int n = 99;
    do {
        if (n % 5 == 0);
            printf("Found one, %d!\n", n);
        else (n--);
    } while (n > -1);
    
return 0;
}

#6

There is a syntax error in the if statement: the semicolon at the end of the line containing the if clause.
Remove that semicolon.

Pick up a copy of the K & R C-Programming Language book and read about the if statements.

But, that is not the end of it: you have a more serious problem, which you should be able to find and fix by yourself after fixing the above syntax error.


#7

Thanks a bunch, I was able to resolve my problem and my code now reads as follows.

#include <stdio.h>

int main (int argc, const char * argv[])
{
    int n = 99;
    do {
        if (n % 5 == 0)
            printf("Found one\n");
        n = n - 1;
        printf("%d\n", n);
    } while (n >= 1);
    
    return 0;
}

I did find that bigger problem, by setting the action of “n–” to occur only when “(n % 5 == 0)” didn’t exist, the program caught on the first multiple of 5 found and printed that infinitely.
Thanks again.


#8

So I misread the challenge instructions, the code above steps by 1, not 3. Whoops.


#9

Interesting to see how other people tackle the same problem.

Here’s what I came up with:

[code]#include <stdio.h>

int main(int argc, const char * argv[])
{
int i = 99;
while ((i <= 99) && (i > 0)) {
printf(“Checking %d\n”, i);
i = i - 3;
if (i % 5 == 0) {
printf(“Found one!\n”);
}
}

return 0;

}[/code]

I surprised myself how quickly I got this challenge, although I’m wondering why my “I found one!” is printing after the divisible by 5 number, and not before it, like in the book. How can I fix that?

Any suggestions for improvement?


#10

You mean it’s printing before it?
Here is the output:

Checking 99
Checking 96
Checking 93
Found one!
Checking 90
Checking 87
Checking 84
Checking 81
Checking 78
Found one!
Checking 75
Checking 72
Checking 69
Checking 66
Checking 63
Found one!
Checking 60
...

The revised version:

//  main.m

#include <stdio.h>

int main (int argc, const char * argv[])
{
    for (int i = 99; i >= 0; i -= 3)
    {
        printf ("Checking %d\n", i);
        if (i % 5 == 0) {
            printf ("Found one!\n");
        }
    }
    return 0;
}

And its output:

Checking 99
Checking 96
Checking 93
Checking 90
Found one!
Checking 87
Checking 84
Checking 81
Checking 78
Checking 75
Found one!
Checking 72
Checking 69
Checking 66
Checking 63
Checking 60
Found one!
Checking 57
Checking 54
Checking 51
Checking 48
Checking 45
Found one!
...

#11

Thanks for the reply Ibex.

Not sure what I’ve done wrong. I tried it with i=95 and the first “found one!” appeared before the number 80.

I copy and pasted the revised code into Command tool and got a this:

[quote]Checking 99
Checking 98
Checking 97
Checking 96
Checking 95 - Found one!
Checking 90 - Found one!
Checking 85 - Found one!
Checking 80 - Found one!
Checking 75 - Found one!
Checking 70 - Found one!
Checking 65 - Found one!
Checking 60 - Found one!
Checking 55 - Found one!
Checking 50 - Found one!
Checking 45 - Found one!
Checking 40 - Found one!
Checking 35 - Found one!
Checking 30 - Found one!
Checking 25 - Found one!
Checking 20 - Found one!
Checking 15 - Found one!
Checking 10 - Found one!
Checking 5 - Found one![/quote]

confused now!


#12

Don’t be confused; I gave you the wrong answer!

I am so sorry! I should have read the instructions in the book.

Check your original code against this one:

//  main.m

#include <stdio.h>

int main (int argc, const char * argv[])
{
    for (int i = 99; i >= 0; i -= 3)
    {
        printf ("Checking %d\n", i);
        if (i % 5 == 0) {
            printf ("Found one!\n");
        }
    }
    return 0;
}

Now the output should look like this:

Checking 99
Checking 96
Checking 93
Checking 90
Found one!
Checking 87
Checking 84
Checking 81
Checking 78
Checking 75
Found one!
Checking 72
Checking 69
Checking 66
Checking 63
Checking 60
Found one!
Checking 57
Checking 54
Checking 51
Checking 48
Checking 45
Found one!
Checking 42
Checking 39
Checking 36
Checking 33
Checking 30
Found one!
Checking 27
Checking 24
Checking 21
Checking 18
Checking 15
Found one!
Checking 12
Checking 9
Checking 6
Checking 3
Checking 0
Found one!

#13

Yes, sorry. I meant before.

Seems the if statement way is the more accurate way to do it then.

Thanks


#14

Here is my solution:


// Counting from 99 and displaying 'found one' when divisible by 5
for (int i = 99; i >= 0; i -= 3) { 
        
        if (i % 5 == 0) {
            printf("Found one! divisible by 5: %d\n", i);
    } else
        printf("%d\n", i);

#15

asm2hex: it’s a cool name.

Now, back to business. There is a coding error. I won’t exactly tell you what it is, but I let you discover it by giving you a clue. Change the initial value of the index variable i from 99 to 100, and see what happens when you run the program: It never terminates!


#16

[quote=“ibex10”]asm2hex: it’s a cool name.

Now, back to business. There is a coding error. I won’t exactly tell you what it is, but I let you discover it by giving you a clue. Change the initial value of the index variable i from 99 to 100, and see what happens when you run the program: It never terminates![/quote]

cool name :smiley: thanks

Now as per the challenge instructions the program should start counting backwards from 99 though 0 by 3, hence the code. If I were to start at 100 then I’d need to set the condition to i >= 0 which would work.

Let me know if I’ve satisfied the challenge :laughing:


#17

Hello!
This is my solution for this Challenge:

#include <stdio.h>

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

for(int i = 99; i >= 0; i-=3) {
    if (i % 5 == 0){
        printf("%d Found one!\n",i);
        continue;
   }
    printf("%d\n",i);
}
return 0;

}


#18

Here is my example of the challenge - this was really easy. :smiley:

[code]#include <stdio.h>
#include <stdlib.h>

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

for (int i = 99; i >=0; i-=3) {
    

    printf ( "%d\n", i);
    
    if (i % 5 ==0) {
        
        printf ( "Found one! (%d)\n", i);
    }
}
EXIT_SUCCESS;

}

[/code]


#19

The simplest answer I could come up with was:

#include <stdio.h>

int main(int argc, const char * argv[])
{
int i = 99;
while (i > 0) {
printf("%d\n", i);
if (i % 5 == 0) {
printf(“Found one!\n”);
}
i = i - 3;
}

I put the i = i - 3 after the “if” function so the “Found one!” would follow the number listed that was divisible by 5 as it shows in the book.


#20

#include <stdio.h>

int main(int argc, const char * argv[])
{
int i;
for (i = 99; i >= 0; i-=3){
if (i % 5 == 0) {
printf(“Found one!\n”);}
{ printf("%d\n", i);
}
}
return 0;
}