Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely... rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits.

I submitted the code for the above problem on spoj and it says wrong answer even when writing the correct output.

The first solution I submitted was:

#include <stdio.h>
int main(void)
{
    int i;
    while (1) {
        scanf("%d", &i);
        if (i == 42)
            break;
        printf("%d", i);
    }
    return 0;
}

The above solution doesn't check whether the input is two digit or not so I wrote another one:

#include <stdio.h>
int main(void)
{
    int i;
    while (1) {
        scanf("%d", &i);
        if (i > 99)
            continue;
        if (i == 42)
            break;
        printf("%d", i);
    }
    return 0;
}

... rewrite small numbers from input to output. All numbers at input ... one or two digits.

However, both of the above solutions don't work on spoj.

share|improve this question
3  
Try adding a newline after each echo'd integer. I suspect that SPOJ is like most judges and likes line breaks between outputs. –  Corbin Dec 24 '13 at 5:45
    
If your solution doesn't work, then it is not a working piece of code. So it does not belong here. –  rick112358 Nov 6 at 16:39

2 Answers 2

up vote 5 down vote accepted

Assuming SPOJ is like other online judges, you need to work on a line basis. If you just output undelimited integers, how can the judge tell if 111 is 1, 11, 11, 1 or 111?

This is meant to just be an introduction to SPOJ's system. You're overcomplicating it a bit. It's as simple as:

#include <stdio.h>

int main() {
    int i = 0;
    while (scanf("%d\n", &i) > 0 && i != 42) {
        printf("%d\n", i);
    }
    return 0;
}

Anyway, on to a few review points:

  • Your loop is way overcomplicated. scanf returns the number of tokens extracted. The idiomatic way to use scanf (and fscanf) is to use it as the loop control
    • note that it's typically a good idea to do scanf() > 0 rather than scanf() implicitly since scanf can return EOF which typically evaluates to true
    • Really you should do scanf() == n where n is the expected number of tokens. For simple IO like this though, that's likely overkill. In more complicated cases though, you might need to be aware of how many tokens scanf read.
  • The instructions never specified that a small number if 2 digits. That's a dangerous assumption. It likely meant small to mean "fits in an int." That's their fault for not being more specific.
share|improve this answer
    
Thanks, it really was line break problem. –  yesboy Dec 24 '13 at 6:01
    
I tried while((i = getchar()) != 42) but that caused file size limit exceed error. –  yesboy Dec 24 '13 at 6:02
3  
@yesboy That's not how getchar works. It returns an character for the next character in the input buffer. In other words "42" is interpreted as the character "4" and the character "2". Character code 42 is the asterisk character (*). –  Corbin Dec 24 '13 at 6:05
    
Thanks for clarifying. –  yesboy Dec 24 '13 at 6:11

Disclaimer: I'm not sure if this code will be the solution for SPOJ.

You don't check if the input number is an int.

if (scanf("%d", &i) == 1)
{
    printf("OK\n");
} 
else
{
    printf("Not an integer.\n");
}

You don't ever tell the user what to enter.

printf("%s\n", "Enter what you think the answer is to life, the universe, and everything (hint: it's a number).");

This is more of a nitpick, but the while loop could be created in a better fashion instead of breaking.

while (i != 42)

Another nitpick, you don't add a newline after printing the int.

printf("%d\n", i);
share|improve this answer
1  
Your printf makes sense in the context of a normal program, but in this context, I think it could be harmful. Code challenge websites typically do not have user interaction of any kind. They tend to be extremely picky about having the exact same output. –  Corbin Dec 24 '13 at 5:45
    
@Corbin I agree, and that is why I don't really like code challenge websites because they are usually very nit-picky and can promote bad coding habits. Keep in mind I didn't review the program so it would pass the spoj code challenge. –  syb0rg Dec 24 '13 at 5:47
    
True, I just worry that it could have been misleading given that this was asked in the context of an online judge :) –  Corbin Dec 24 '13 at 5:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.