# Life, the Universe, and Everything

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.

-
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

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.
-
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
@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);

-
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