0

I am learning C++ and trying to solve this problem from Hackerearth link

I wrote a basic code for this on Dev-C++ 5.11 and due to some reason, it is not running.

There is no error in compiler, I tried printing things in main() function to see how much code it is executing, and it prints None!

I even tried executing an infinite loop on 1st line of main() but, code ends without doing anything. Here is my code:

#include <bits/stdc++.h> 

using namespace std;


int main()
{
    int N = 0;
    cin >> N;
    
    int m = N;
    int x1=0, x2=0, y1=0, y2=0;
    int c = 0;
    int cost = 0, total_cost = 0;
    
    int a[1000][1000] = {0};
    
    while(m>0)
    {
        cin >> x1 >> x2 >> y1 >> y2 >> c;
        
        cost = 0;
        
        for(int i = y1; i<y2+1; i++)
        {
            for(int j = x1; j<x2+1; j++)
            {
                if(a[i][j] == 1)
                {
                    cost++;
                }
                else
                {
                    a[i][j] = 1;
                }
            }
        }
        
        total_cost += cost*c;
        
        m--;
    }
    
    cout << total_cost;

}

I am new to C++, so any kind of help will be appreciated.

  • 1
    Did you provide enough input to get through the `cin`s? Please share a sample input along with the output you expect from it. – François Andrieux Aug 29 '20 at 16:47
  • 1
    `int a[1000][1000] = {0};` may be too large for the stack. What if you move this before `main()` function (making global)? – MikeCAT Aug 29 '20 at 16:49
  • 2
    Maybe your antivirus is the problem – drescherjm Aug 29 '20 at 16:50
  • @FrançoisAndrieux It not even asking for an input. – Manasvi Sinha Aug 29 '20 at 16:51
  • @ManasviSinha Ok, so that means that the code crashes, not that it has a problem (it may has, but since it doesnt ask you for input, then it certainly crashes or killed). As people said in the comments, it is possible your program is killed due to `a[1000][1000]` being too big to be a variable within main or because the antivirus cannot play nicely with user-compiled programs – kyriakosSt Aug 29 '20 at 16:54
  • @MikeCAT That solved the issue, thank you for your help. – Manasvi Sinha Aug 29 '20 at 16:55
  • 1
    On windows the default stack size is 1MB. On linux I believe its 8 or 10 times that. – drescherjm Aug 29 '20 at 16:55
  • Also, I ran the code with `N=10` and the rest of the input as `1 2 3 4 5` at random and it does run forever. I am using Ubuntu. Finally, including `` is a bad practice. You may take a look at [this](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) question. – kyriakosSt Aug 29 '20 at 16:57
  • If your goal is to learn C++, you may be better served by using a [good C++ book](https://stackoverflow.com/a/388282/4641116), rather than by learning from a coding practice problem site. – Eljay Aug 29 '20 at 17:08

0 Answers0