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.
Example
Input:
1
2
88
42
99
Output:
1
2
88
So… My solution was this one:
/*
Life, the Universe, and Everything
Written by laurene
21/07/2013
*/
#include <stdio.h>
int main()
{
int num[10];
int i = 0;
while(1)
{
scanf ("%d", &num[i]);
i++;
if (num[i-1] == 42)
{
scanf ("%d", &num[i]);
for (i = 0; num[i] != 42; i++)
{
printf ("%d\n", num[i]);
}
return(0);
}
}
return(0);
}
The Result is something like this:
1
2
3
42
44
1
2
3
Process returned 0 (0x0) execution time : 4.757 s
Press any key to continue.
So! I can type even one number after typing 42, and there is the output, all the numbers before 42!
But! The site compiler keeps telling that my solution does have a Run Time Error, that I can’t figure out what it is…
And the Admin solution to this problem gives a different answer, you guys can check in the submittion list.
I would love to know what that Run Time Erros is all about.
Well, try not to post code here.
As for the error, there is an error occurring since you are reading more numbers than required. Moreover, you are never breaking out of the loop (So, in any case it’ll run on forever)
.
Where or How should I post the code?
I saw other posts with code too…
And… I am not reading more numbers than required, because the size of the vector is 10 and I’ve typed only 5 numbers.
If I’m not breaking out of the loop, why did the program have finished succesfully?
I’m using CodeBlocks, it does use GNU GCC Compiler; mingw32-gcc.
I guess this might be a problem…
Thanks a lot for the answer and patience.
Wish the best for you.
Bye.
Sorry for getting back this late, but as @viaan pointed out, the input is not limited to only 10 numbers.
As for the posting code thing, there might be other posts with code but don’t post a lot of code and ask what’s wrong. Post only the relevant portions.