Life, the Universe, and Everything - Run Time Error

So the exercise is:

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.

Thanks for the attention.

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

1 Like

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.

@laurene but the limit of numbers in the CodeChef problem is not restricted to 10 only.

It can have many numbers as input.

Therefore you are going out of bounds Hence the SIGSEGV error.

For more assistance you can look at some of the accepted solutions.

1 Like

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.

All right I`ll take a look at that.

Thanks a lot for the answers and patience guys. Bye.

@laurene there are some bugs in your program.

  1. you are asuming there are only 10 numbers.

  2. Second you are not breaking the loop.

  3. Third, you are doing so many things wrong.

Here, is the correct code link. Hope, you learn from your mistake. And practice more and more.

This is what your programm is doing:

1 (input)
1 (output)
2
2
3
3
42

(where is the last number AFTER the number 42 ???)

Process returned 0 (0x0) execution time : 4.414 s
Press any key to continue.

This is what my program does:

(input)
1
2
3
99
42
33 (the last number AFTER 42)
(output)
1
2
3
99
(the numbers BEFORE 42)

Process returned 0 (0x0) execution time : 11.351 s
Press any key to continue.

What does the practice ask for?

Thanks for your attention.

@laurene Have you read the question - “Stop processing input AFTER reading in the number 42”. So, why bother about the numbers after 42.

and you are printing the numbers even after reading 42.

And as far as practice is concern. You will learn new things by practice.

//