-2

I was trying to solve a problem( https://www.codechef.com/LOCMAY17/problems/LOC172) on an online judge. Following is the code for the problem.When i submit the judge gives runtime error(). All the variables have been used acc to constraints.Please help me in finding out what is going wrong? the code is given below:

#include<iostream>
#include<stdlib.h>
using namespace std;
main()
{
long long int h,w;
while(1)
{
    cin>>h>>w;
    if(h==-1 && w==-1)
        exit(1);
    int n,i,j;
    cin>>n;
    long long int arr[n][4];
    for(i=1;i<=n;i++)
    {
        cin>>arr[i][1]>>arr[i][2]>>arr[i][3];
        arr[i][4]=0;
    }

    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            if((llabs(arr[j][2]-arr[i][2]))<=arr[j][3])
                arr[i][4]++;
        }
    }
    long long int max=arr[1][4];
    for(i=2;i<=n;i++)
    {
        if(arr[i][4]>max)
            max=arr[i][4];
    }
    cout<<max<<endl;
}
}

I am a beginner at this site,thanks in advance.

  • Does CodeChef accept dynamic arrays? – CinCout May 29 '17 at 08:32
  • 3
    Technically your program is not a valid C++ program, because C++ doesn't have [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array). Use [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) instead. – Some programmer dude May 29 '17 at 08:32
  • `long long int arr[n][4];` should not compile. `n` is not a constant. – nakiya May 29 '17 at 08:32
  • Also remember that array indexes are *zero* based. So an array of four elements have indexes from `0` to `3` (inclusive). – Some programmer dude May 29 '17 at 08:33
  • for(i=1;i<=n;i++)? Where starts an array index? –  May 29 '17 at 08:33
  • 2
    Lastly, perhaps you should try to find [a good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to help you learn C++, instead of sites like codechef? – Some programmer dude May 29 '17 at 08:34
  • @Someprogrammerdude,i tried doing int arr[100][4] for that,but still it is giving run time error. – nikita0112 May 29 '17 at 08:41
  • @Someprogrammerdude,i also changed indexes from 1 to 4 to 0 to 3,still not working – nikita0112 May 29 '17 at 08:42
  • C++ doesn't have implicit int rule (neither has C by the way), hence `main()` is invalid. If your compiler accepts tjis program, throw it away and find a non-broken one. – n. m. could be an AI May 29 '17 at 08:47
  • @n.m.,I did it int main and return 0,still same runtime error is there. – nikita0112 May 29 '17 at 08:57
  • I didn't say that it's the reason of your error. I said that your compiler is broken because it accepted this broken code, and that the source that taught you to write `main()` is broken because it taught you to write broken code. If you want to continue using either one, it's up to you. – n. m. could be an AI May 29 '17 at 09:12
  • Now if you want to see a reason why your program crashes, it's this: `for(i=1;i<=n;i++)`. You don't use arrays like that in C++, but I think someone has already pointed that out. In fact you don't use arrays at all but it's a different story. – n. m. could be an AI May 29 '17 at 09:21

1 Answers1

1

Is hard to find a run time error on a code that is not compiling...

this is invalid

long long int arr[n][4];

because you are doing this:

int n,i....

but n must actually be a compile-time constant, this is just because C++ doesn't allow variable-length arrays

having say that, arrays are 0 base index data containers so this here is causing a UB:

for (i = 1; i <= n; i++)

because you are leaving the element at index 0 intact and writing to a place out of the bounds of the array.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97