Thread: odd and even number pattern

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    35

    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!
    Last edited by christianB; 07-17-2011 at 12:27 AM.

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    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?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    35
    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. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Last edited by quzah; 07-17-2011 at 12:59 AM.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    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.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    35
    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. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by quzah View Post
    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.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    35
    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. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    How would you do it in words? Lets work on that first.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    Registered User
    Join Date
    Jul 2011
    Posts
    35
    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. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by christianB View Post
    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?)
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  13. #13
    Registered User
    Join Date
    Jul 2011
    Posts
    35
    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)
    Last edited by christianB; 07-17-2011 at 03:16 AM.

  14. #14
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    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...
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  15. #15
    Registered User
    Join Date
    Jul 2011
    Posts
    35
    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 subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-15-2010, 01:35 AM
  2. C++ Pattern Help
    By Lestat in forum C++ Programming
    Replies: 10
    Last Post: 10-24-2007, 02:05 PM
  3. Hmm.. Ai? Finding the pattern in number squences?
    By Zeusbwr in forum C++ Programming
    Replies: 8
    Last Post: 04-02-2005, 06:13 PM
  4. (pattern *) pat & pattern * pat
    By siubo in forum C Programming
    Replies: 1
    Last Post: 04-08-2003, 10:03 PM
  5. pattern
    By Unregistered in forum C Programming
    Replies: 16
    Last Post: 05-04-2002, 07:37 PM