0

I have a following code, and although I know it is a problem with vectors I can't find it, so any help would be appreciated! Maybe it processes an empty vector but I tried removing and adding something and still nothing happens.

int main()
{
    int t;
    cin >> t;
    int n[100000];
    int m[100000];
    int komplement[100000];
    int najmanjiceodeo[100000];
    int najmanjiceodeo1[100000];
    for (int k = 0;k < t;k++)
    {
        cin >> n[k]>>m[k];
        komplement[k] = binomnikoef(n[k] + 1, 2);
        najmanjiceodeo[k] = (m[k] + 1) * (floor((n[k] - m[k]) / (m[k] + 1)));
        najmanjiceodeo1[k] = (floor((n[k] - m[k]) / (m[k] + 1)));
    }
    vector <int> v1;
    for (int k = 0;k < t;k++)
    {
        int S[100000];
        S[k] = komplement[k];
        for (int i = 0;i < m[k] + 1;i++)
        {
            if (i < (n[k] - m[k]) - najmanjiceodeo[k])
            {
                v1.push_back(najmanjiceodeo1[k] + 1);
            }
            else
            {
                v1.push_back(najmanjiceodeo1[k]);
            }
            if (!v1.empty())
            {
                S[k] -= binomnikoef(v1[i] + 1, 2);
            }
        }
        v1.clear();
        cout << S[k] << endl;
    }
Daokr23
  • 27
  • 4
  • 2
    You're not supposed to allocate **several megabytes** on the stack. A modern OS only gives you a few kilobytes. Switch to heap allocation instead. – Botje Nov 03 '20 at 13:00
  • If this is windows you are over the default 1MB stack size with all the arrays. – drescherjm Nov 03 '20 at 13:01
  • Komplement is just a binomial coefficient vector[i]+1 choose 2,sorry for mentioning that! – Daokr23 Nov 03 '20 at 13:02
  • Yeah I tried lowering array's size and it works now, thanks! – Daokr23 Nov 03 '20 at 13:03
  • Make sure to read the linked duplicate target. If you do need a large array, you can allocate it on the heap instead. – cigien Nov 03 '20 at 13:05
  • Refactor to use std::vector for all, initialize or resize to get the desired size, use a constant to ensure all sizes are the same. – Surt Nov 03 '20 at 13:09

0 Answers0