Thread: odd and even number pattern

1. odd and even number pattern

hello again.

I have another problem that I am working on. I am to write a program so that it readsa series of integers, ending with EOF, and checks that the integers alternate betweenodd and even, with the first being odd. If there are no integers at all, that counts as "correct". The program prints "All OK" if the pattern is followed, and "Pattern not followed" if it's not.

my current code allows just checks if there is an odd integer in the input. So if i input 2 2 2 2 3 4 4 4, it prints All Ok. if there are no odd numbers then I get, "Pattern not followed."

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

int main () {

int val, odd, ok;

odd = 0;
ok = 1;

while (EOF != scanf("%d", &val)) {
odd = odd || val % 2;
ok = odd;
}

printf(ok ? "All OK\n" : "Pattern not followed\n");

return 0;```
I would really like to figure this out on my own. I'm not asking anyone to write it for me, but I could really use some pointers. Thanks!

2. Is there a specific reason you are making the code as obfuscated as you are? Does part of this assignment include minimizing lines of code? A big pitfall with programmers is that they try to use all the aspects possible in the language to perform "tricks" to minimize keystrokes, where in the long run all it does is prevent maintainability of the code and obscure the problem at hand.

With that in mind, lets look at this problem another way. I come to you and give you the same requirements verbally that you have presented.

The first number I tell you is 1.
what are you going to do with that information?
The second number I tell you is 6
what are you going to do with that information?
The third number I tell you is 3
what are you going to do with that information?
The fourth number I tell you is 9
what are you going to do with that information?
Now I tell you I am done with my list and I want to know if I followed the pattern, right now
what are you going to say?

3. I'm not sure what obfuscated means, but I will say that most of the code is provided to me (the keyword "while", the declared variables, and the printf statement). I have 3 spaces to fill in, one allowing multiple lines, and the other two allowing one line each. I also am limited to 12, 20, and 10 characters per space, respectively.

if you give me a one, i will check to see that weather it is even or odd. If it is odd i will hold onto it and wait for the next number.
if you give me a 6 for me next number, i will say that everything is good so far since the first # was odd.
if my 3rd number is 3 I will again hold it and wait for the next number.
if my fourth number is 9, I will check it against 3, making sure that 3 is odd and 9 is even.
since the last two numbers I looked at didn't check out I will say no.

4. You still aren't breaking out of your loop on anything other than scanf returning EOF.
Code:
```while input
if !count && isodd input
ok = true
count++
if isodd input != isodd count
ignore everything until EOF and say they failed```
Something like that? Otherwise, I don't know what you are trying to do. Do you ever restart the search for a good sequence, or once they mess it up, is it messed up for good?

Quzah.

5. Great, you have your answer. All you have to do is translate that into C code.

Some notes, why don't you hold on to the second number? All you have to do is check to make sure whatever the previous number is (odd or even), is opposite the next number. This is what you are doing in your mind but not what you are saying. Remember, you need to tell C exactly what you want; you cannot infer anything to the compiler.

*note: Don't forget about if none of the numbers are integers* I do believe scanf() returns a value.....

Drop the "?, :" macro stuff. It is preventing you from seeing the problem. Obfuscated means to obscure, in this case don't overcomplicate things.

6. my problem is that I am trying to translate what I am thinking into C code, and at the same time, meet certain guidelines. my translation far exceeds my character limits! I will take your advice though and give it another shot. Unfortunately, the macro stuff is pre-written and I cannot change it.

7. Don't translate it into C first. Translate it into words. You can't turn it into C until you can actually put into words what it is you want to do. You can't program something if you don't know what it is you want to program.

Quzah.

8. Originally Posted by quzah
You still aren't breaking out of your loop on anything other than scanf returning EOF.....
Do you ever restart the search for a good sequence, or once they mess it up, is it messed up for good?
Quzah.
The way I read the problem was the the sequence was allowed to be entered from start to finish, whether or not the sequence was valid. Then after the sequence was completed the output would be whether or not it was valid. I understand this is something just for academia, but I am sure you remember these problems from your initial studies.

9. okay guys, i've been working on it for a while and I keep arriving at the same kind of problem. I keep coming up with code that just checks for odd or even numbers, but no particular sequence. I know that i need

Code:
```if(1st val is odd and 2nd val is even)
ok = true;```
i know that "ok" has to be initialized to 1, because if scanf doesn't read in an integer, upon EOF it should still say "All, ok". I just don't konw how to get the program to check for a sequence.

10. How would you do it in words? Lets work on that first.

11. 1. scan for an integer.
2. check if integer is odd or even.
3. scan second integer.
4. check if integer is odd or even.
5. if first int is odd, and second int is even, everything ok.
if first int is not odd, pattern not followed.

12. Originally Posted by christianB
1. scan for an integer.
2. check if integer is odd or even.
3. scan second integer.
4. check if integer is odd or even.
5. if first int is odd, and second int is even, everything ok.
Ok, now we are dealing with an infinite set (until EOF) so instead of "first" and "second", how could you generalize this? (Maybe current and last?)

13. 1. scan int
2. check if current int = odd
3. scan new int
4. check if new int is even (or opposite old int, or not odd)

14. Yes you do. And then set a flag for sequence like you did before. Think big picture here, you don't care if it is 3 or 1,000,000 numbers in your code. All you care about is whether or not the sequence follows the pattern, which is odd even odd...

15. by flag you mean, if first int is odd, and second int is even, everything ok.
would it be possible to do all of that in only two lines?

Popular pages Recent additions