3

I am trying to form largest number from elements of array. My implementation given below is working fine for some cases and for some other its giving error " Abort signal from abort(3) (SIGABRT)" . Why? Help me out!

#include <iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {
int t;
cin>>t;
while((t--)>0){
    int n;
    cin>>n;
    int a[n];
    string s="";
    for(int i=0;i<n;i++){
        cin>>a[i];
        if(i==0){s+=to_string(a[i]); continue;}
        string s1 = s+to_string(a[i]); //sX
        string s2 = to_string(a[i])+s; //Xs

        if(stoi(s1)>=stoi(s2))s=s1;
        else s = s2;
    }
    cout<<s<<endl;
    }

return 0;
 } 

For the following case my code is giving the error

4                  //size of array
54 546 548 60      //elements of array
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Mahesh Nagarwal
  • 55
  • 1
  • 1
  • 6
  • First please read [Why should I not #include ?](http://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) Then don't forget that C++ doesn't actually have [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array), use [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) instead (although you neither need a vector nor an array in your program). – Some programmer dude May 22 '18 at 04:06
  • As for your problem, please [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Some programmer dude May 22 '18 at 04:08
  • 2
    Since how this program will run is highly dependant on the input. You may want to mention the input you use and results. i.e. What input makes it work and what input makes it crash. – Martin York May 22 '18 at 04:09
  • I got the following output from debugging so basically now I have understood whats the problem but I still dont know how to resolve it please help me here ....terminate called after throwing an instance of 'std::out_of_range' what(): stoi Aborted – Mahesh Nagarwal May 22 '18 at 04:13
  • You concatenate the string into a larger string with a larger number. The type `int` (which is what [`std::stoi`](http://en.cppreference.com/w/cpp/string/basic_string/stol) converts to) is on a standard PC today a 32-bit signed integer type. Now what are the ranges of 32-bit signed integer types? Perhaps you should not attempt to learn programming using online-judges or competition sites, and instead [get a few good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to read and learn *properly*? – Some programmer dude May 22 '18 at 04:20

7 Answers7

4

This is due to the stoi function. This does not work with a very large number.
For a string to int conversion try the following approach.

Algorithm to manually converting a string to int:

int x = 0; // may take long long 
for(int i = 0; i < s.length(); i++)
    x = x * 10 + s[i] - '0';

Variable x will store the integer value of the string in discussion.

3

this error happens when you are looping for infinite time or the popping operation is not done , i got the same error previously , but could manage to solve after

Abhisaware
  • 29
  • 2
1

Instead of using 'stoi' function, use the following code to convert string to integer:

    int num = 0;  
    for(int i = 0; i < s.length(); i++){
        num = num * 10 + s[i] - '0';
    }
Mansi Garg
  • 11
  • 2
0

it is due to INT_MAX and INT_MIN values i.e 2^31-1 and -2^31 which can't be stored inside an integer type data; also int z=stoi(string x) converts and returns string into int

0

To get the largest number just by adding first and last element of the array (string wise), according to your program, try the following:

s = to_string(a[0]); #to get the first element

s1 = s1 + to_string(a[i]); #to get the ith element
     
s2 = to_string(a[i]) + s2;

such that you use all elements of the array instead of using only first and last element.

William Baker Morrison
  • 1,642
  • 4
  • 21
  • 33
0

If an error itself is detected by the program then this signal is generated using call to abort(). This signal is also used by standard library to report an internal error. assert() function in c++ also uses abort() to generate this signal.

0

This error generally comes when you are accessing something not available like memory etc. look at the example I have declared col as m but declaring the size of n to it,(so when my program tries to access other than m location it throws this error)

vector<vector<int>>dist(n,vector<int>(n,INF));---> instead of this 
vector<vector<int>>dist(n,vector<int>(m,INF));

These are minor errors but can take a lot of time

)

class Solution {
  public:
    int shortestPath(vector<vector<int>> &grid, pair<int, int> source,
                     pair<int, int> destination) {
        // code here
        int n=grid.size();
if(source.first==destination.first && source.second==destination.second) return 0;
        int m=grid[0].size();
        const int INF=1e9+7;
        vector<vector<int>>dist(n,vector<int>(n,INF));
        queue<pair<int,pair<int,int>>>q;
        dist[source.first][source.second]=0;
        q.push({0,{source.first,source.second}});
        int row[]={-1,0,1,0};
        int col[]={0,1,0,-1};
        while(!q.empty()){
            auto d=q.front().first;
            auto r=q.front().second.first;
            auto c=q.front().second.second;
            q.pop();
            for(int i=0;i<4;i++){
                int newr=row[i]+r;
                int newc=col[i]+c;
                
                if(newr>=0 && newc>=0 && newr<n && newc<m && grid[newr][newc]==1 && 
                1+d<dist[newr][newc]){
                    dist[newr][newc]=1+d;
                    if(newr==destination.first && newc==destination.second){
                        return 1+d;
                    }
                    q.push({1+d,{newr,newc}});
                    
                }
            }
        }
        return -1;
        
        
        
    }
    
};

above code is for binary maze (0,1) problem.(graph topic

Kumawat Lalit
  • 410
  • 4
  • 8