0

Can anyone solve this runtime error?

   Status :Time limit exceeded
Time:
5 secs
Memory:
5.368 Mb
Input
4 8 4
1 2 1 2 5
3 5 1 3 4
1 2 4 5 11
1 1 1 3 12
Runtime Error
SIGTSTP
#include <iostream>
using namespace std;

int main() {
    // your code goes here
    int n,m,k,cert=0;
    cin>>n>>m>>k;
    int a;
    for(int z=0;z<n;z++)
    {
        int sum=0;
        for(int i=0;i<k;k++)
        {
          cin>>a;
          sum+=a;
        }
        cin>>a;
        if(sum>=m && a<=10)
        cert++;
    }
    cout<<cert;
    return 0;
}
273K
  • 29,503
  • 10
  • 41
  • 64
pooaraz
  • 3
  • 1
  • 5
    That's not a runtime error from incorrect operation. Its a failed contest entry for being too slow. You probably want to see if you can think up a better algorithm to use. – Avi Berger Aug 09 '22 at 04:35
  • 1
    What is this code supposed to do? What are the values you are inputting? – PaulMcKenzie Aug 09 '22 at 04:42
  • I am not going to try and decipher these loops as to what this code is supposed to do (you should state in the question what this code is supposed to accomplish). However if it is one of those "contest" questions, please be aware that those questions almost always have naive, slow, solutions that will always fail when given a large input size. Then it's your job to find a better solution. – PaulMcKenzie Aug 09 '22 at 04:46
  • Ah, as @Ted Lyngmo noticed, you do actually have a bug/typo that is causing the slowness and incorrect operation as well. – Avi Berger Aug 09 '22 at 04:53
  • @AviBerger -- Even with the fixed code, if `n` or `k` are large values, that may give the "Time Out Error", which is why I mentioned about the nature of these "competition" questions. – PaulMcKenzie Aug 09 '22 at 04:56

2 Answers2

1

It's because your code runs for a very long time.

Look at this loop:

for (int i = 0; i < k; k++) {

k will increase, while i stays at 0 so i < k will always be true.

When k at some point reaches its limit, INT_MAX, and you do k++, it'll will cause signed integer overflow and the program therefore has undefined behavior.

Without knowing what your code is supposed to do, it's not possible to give an exact advice how to make it work, but I suspect that you want this instead:

for (int i = 0; i < k; i++) {
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
1

Someone already answered this question, but there's one more interesting thing to discuss. I noticed this question because it has the cppcheck tag. I guess you unintentionally touched upon an exciting topic :). As I can see, it’s your first question, so perhaps you thought that the "cppcheck" tag means "I have a problem in my code. Could you please check it?". Actually, "cppcheck" is the name of a static analyzer for searching code errors in C and C++.

Well, I found this topic interesting because actually static analysis can easily detect an error in the code fragment above. With a static analyzer you don't need to ask such questions. Therefore, analyzers can save your time – they will help review your code and find an error.

Unfortunately, Cppcheck failed to find the error – I went to the Cppcheck Online Demo webpage and checked the code fragment from this question. Unfortunately, this analyzer didn’t issue any warnings about the loop. Well, analyzers are not perfect, especially such relatively simple static analyzer. But it has at least two advantages — it's free and open source.

Well, I didn’t give up – I used another analyzer. That was the Online mode of the PVS-Studio static analyzer: https://godbolt.org/z/TEsdeG7oM

And can you imagine? PVS-Studio issued the warning that describes this error: V683 Consider inspecting the loop expression. It is possible that the i variable should be incremented instead of the k variable.

I hope you will find this information useful and interesting enough to use the static analyzer’s clues for learning C++ more efficiently.

AndreyKarpov
  • 1,083
  • 6
  • 17