1

here's the code

#include <iostream>
#include <fstream>

using namespace std;

ifstream f("v.in");
ofstream g("v.out");

int v[2000][2000];

int main()
{
    int s = 0;
    int imax, jmax, smax = 0;
    int n, m;
    int d;

    f >> m >> n;
    for(int i = 1; i <= m; i++)
    {
        for(int j = 1; j<=n; j++)
        {
            f >> v[i][j];
        }
    }
        
    int i = 1, j = 1;
    for(i = 1; i + 2 <= n; i++)
    {
        for(d = 1; d < m && i + d * 2 <= n; d++)
        {
            int i2 = i + d * 2;
            s = 0;
            int ii1 = i;
            int ii2 = i2;
            
            for(j = 1; j <= d + 1; j++)
            {
                s = s + v[j][ii1++];
            }
            

            for(j=1;j<d+1;j++)
            {
                s = s + v[j][ii2--];
            }
            
            if(s > smax)
            {
                smax = s;
                imax = i;
                jmax = d + 1;
            }
        }
    }

    g << smax << ' ' << imax << ' ' << jmax;

    f.close();
    g.close();

    return 0;
}

the program must read the matrix in V starting from first line. every time i get the terminated with status -1073741510 and i cant find why.

1 ≤ m, n ≤ 1500

It has to show the max sum route showing, smax, and starting column and last row as in the example:

v.in

5 10
1 2 1 1 1 1 1 1 1 1
1 2 -1 20 -1 -1 -1 1 -1 2
-3 -3 -4 -5 -6 -7 -8 -9 20 -1
-2 -4 -5 -6 -6 -7 -8 -9 -10 -1
-20 -40 -4 -5 -6 -6 -7 -8 -9 -10

v.out

22 3 2

I am begginer , please help

Rokas Višinskas
  • 533
  • 4
  • 12
  • 1
    You get a *crash* and you need to use a *debugger* to catch the crash as it happens, to locate when and where in your code it happens. Using the debugger you can also examine variables and their values at the point of the crash, to see that they look valid. – Some programmer dude Aug 31 '20 at 11:23
  • Please attach a debugger and tells us which line actually causes the crash – Botje Aug 31 '20 at 11:23
  • i used the code blocks debugger to see where it happens but it was working good – Vlad Popescu Aug 31 '20 at 11:26
  • There is so much unchecked indexing trickery that it would be very surprising if it were correct. – molbdnilo Aug 31 '20 at 11:27
  • And *please* don't use competition/online judge sites to learn programming. Get [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to read, or even take classes. Such sites only teaches one thing: How to write bad code and acquire bad habits. – Some programmer dude Aug 31 '20 at 11:28
  • If the problem's maximum size is 2000 in each direction, you're indexing outside the arrays if either `m` or `n` is 2000. – molbdnilo Aug 31 '20 at 11:31
  • @Someprogrammerdude ,i will try to find some good books, – Vlad Popescu Aug 31 '20 at 11:32
  • @molbdnilo the maximum size is 1500,i know i put too much ,i wanted to avoid indexing outside the arrays – Vlad Popescu Aug 31 '20 at 11:34
  • @VladPopescu Instead of `int v[2000][2000];` use a `std::vector> v(m, std::vector(n);` and then remember that indexing is zero-based in C++ so you can access `v[0][0]` to `v[m-1][n-1]` (inclusive). You can use the safe `at()` `vector` member function to get exceptions if you try to access the array out of bounds `v.at(m-1).at(n-1)` which can help when debugging. – Ted Lyngmo Aug 31 '20 at 11:55
  • @TedLyngmo thanks, i will try to do it – Vlad Popescu Aug 31 '20 at 11:59
  • Oups, it should have been: `std::vector> v(m, std::vector(n));` - I missed a parenthesis. – Ted Lyngmo Aug 31 '20 at 12:01
  • On windows `-1073741510` is `0xC000013A` which is `STATUS_CONTROL_C_EXIT` here: [https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/596a1078-e883-4972-9bbc-49e60bebca55](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/596a1078-e883-4972-9bbc-49e60bebca55) – drescherjm Aug 31 '20 at 12:11
  • 1
    Even the first for loop starts indexing @ 1 for some reason. It should be `(int i = 0; i < m; ++i)` and not `(int i = 1; i <= m; ++i)`. This doesn't create problems right now since the matrix is over-sized, but this is a huge code smell. – Rokas Višinskas Aug 31 '20 at 12:43

0 Answers0