1

/* When I run this C++ code I got Runtime error 'SIGFPE' on Codechef. How to fix this problem?

Explanation

Test case 1 : Optimal filling is [5,0,5,0,5,0,5,0,5,0]

Test case 2 : Optimal filling is [6,6,5,6,6,0,6,6]

Test case 3 : Optimal filling is [2,1,2]. */

#include <bits/stdc++.h>
#include <iostream>
#include <array>
using namespace std;
int main()
{
   int T;
   cin >> T;
   while (T--)
   {
       int n, m;
       cin >> n >> m;
       int arr[n+1];
       memset(arr,0,(n+1)*sizeof(int));

       int x[m], y[m];
       for (int i = 0; i < m; i++)
       {
           cin >> x[i] >> y[i];
       }

       vector<pair<int, int>> myvector;
       for (int i = 0; i < m; i++) {
           myvector.push_back(make_pair(x[i], y[i]));
       }

       sort(myvector.begin(), myvector.end());
       int j=0;
       int i=1;
       do
       {
          int i=1;
          do
           {
               if (i % myvector[j].second != 0)
               {
                   arr[i] = myvector[j].first;
               }
               i++;
           }while (i<=n);
           j++;
       }while(j<m);

       int sum = 0;
       for (int i : arr)
       {
           sum += i;
       }
       cout << sum << endl;
   }
   return 0;
}
  • What are you trying to achieve? Also, please, provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Suthiro Aug 09 '21 at 05:45
  • 1
    `int arr[n+1];` isn't standard-compliant C++, and is a recipe for an automatic variable blowout on non-standard implementations that provide it as an extension. Likewise for `int x[m], y[m];` Use vectors. – WhozCraig Aug 09 '21 at 05:51
  • 1
    SIGFPE can be caused by division by zero, for example in `if (i % myvector[j].second != 0)` if `myvector[j].second` is 0. Seems like a good time to learn how to use a debugger. – Lukas-T Aug 09 '21 at 05:52
  • "SIGFPE" mean "Floating Point Exception". You could run the program in a debugger (with debugging symbols enabled), an then expect the local variables to isolate the problem. Then maybe as a specific question. – U. Windl Aug 09 '21 at 08:17

0 Answers0