Floating point Exception in c++ because of divide statement -


i have searched google unable find solution problem. here code-

#include <bits/stdc++.h> #include<algorithm> #include<cstdlib> #include<cstdio>  using namespace std;  long long buymaximumproducts(int n, long k, vector <int> a) {     // complete function     vector<pair<int, int>> p;     long i;     for(i=0; i<n; i++) {         p.push_back(make_pair(a[i], i+1));     }      sort(p.begin(), p.end());      if(k < p[0].first)         return 0;      long long sum=0,stocks=0;      for(i=0;i<n;i++)     {         if((sum+p[i].first*p[i].second) <= k)         {             sum+=p[i].first*p[i].second;             stocks+=p[i].second;         }          else              break;      }      long long amtleft=k-sum;     **stocks+=(long long)(amtleft/p[i].first);**     return stocks;  }  int main() {     int n;     cin >> n;     vector<int> arr(n);     for(int arr_i = 0; arr_i < n; arr_i++){        cin >> arr[arr_i];     }     long long k;     cin >> k;     long long result = buymaximumproducts(n, k, arr);     cout << result << endl;     return 0; } 

i'm getting floating point exception. think error coming because of star statement. can please tell me plausible reason , how remove it?

the program contains @ least 3 fault.

long long k; cin >> k; long long result = buymaximumproducts(n, k, arr);  long long buymaximumproducts(int n, long k, vector <int> a) { 

k 'long long' parameter k 'long'.

for(i=0;i<n;i++) {     if((sum+p[i].first*p[i].second) <= k) {         sum+=p[i].first*p[i].second;         stocks+=p[i].second;     } else          break;  } 

if never 'break' 'i' not valid for

stocks+=(long long)(amtleft/p[i].first); 

causing exception.

and if

p[i].first 

is 0 divide 0 exception.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -