SIGABRT error in my code in c++ - c++

I am getting SIGABRT error in my code. Can anyone tell me from where it might be coming?
Here's my code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
string s;
cin>>s;
int d=stoi(s,nullptr,2);
int f=__builtin_popcount(d);
cout<<f*(f-1)/2+f<<endl;
}
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
long long int t;
cin>>t;
while(t--)
{
long long int n;
cin>>n;
string s;
cin>>s;
long long int d=stoi(s,nullptr,2);
long long int f=__builtin_popcount(d);
cout<<f*(f-1)/2+f<<endl;
}`enter code here`
}
Now Try This

Related

Invalid conversion from int to int* error for a function with void return type

So, I need to get the maximum value from an array. So the first input line contains the number of array elements, then the values for the array. But I am really confused as to why this error keeps popping up when I pass an integer type array to the function. I am finding it a little difficult to deal with pointers and functions at the moment.
#include<iostream>
using namespace std;
void maxim(long long int nums[],long long int n){
int max_val=0;
for(int i=0;i<n;i++){
if(nums[i]>max_val){
max_val=nums[i];
}
}
cout<<max_val<<endl;
}
int main() {
long long int n;
cin>>n;
long long int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
maxim(arr[n],n);
return 0;
}
#include<iostream>
using namespace std;
void maxim(long long int *nums,long long int n){
long long int max_val=nums[0];
for(int i=1;i<n;i++){
if(nums[i]>max_val){
max_val=nums[i];
}
}
cout<<max_val<<endl;
}
int main() {
long long int n;
long long int *arr;
cin>>n;
arr = new long long int[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
maxim(arr,n);
delete []arr;
return 0;
}

How to initialize structure to zero everytime in while loop?

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define max 200005
struct st{
ll index,freq,val;
}s[max];;
int main(){
ll t;
cin>>t;
while(t--){
ll n;
cin>>n;
ll counter[max]={0};
for(ll i=1;i<=n;i++){
ll x;
cin>>x;
s[x].val=x;
s[x].index=i;
s[x].freq++;
cout<<s[x].freq<<endl;
}
}
return 0;
}
In this code, how I will initialize struct to zero in every while loop. I have tried memset() but It is showing error. Also I created struct object in main function. But the program crashes. Please give me hints to solve the problem.
Try this instead, its more structured, usually less error prone.
std::fill(std::begin(s), std::end(s), st({0,0,0}));
I might even be converted to memset by the compiler.
Surely there will be a better solution than this but you can add this snippet at the beginning of the while loop:
for(int j=0; j<max; j++){
s[j].val=0;
s[j].index=0;
s[j].freq=0;
}
You can also consider the idea of redeclaring the struct array every time in the loop, just like this:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define max 200005
struct st{
ll index,freq,val;
};
int main(){
ll t;
cin>>t;
while(t--){
st s[max];
ll n;
cin>>n;
ll counter[max]={0};
for(ll i=1;i<=n;i++){
ll x;
cin>>x;
s[x].val=x;
s[x].index=i;
s[x].freq++;
cout<<s[x].freq<<endl;
}
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define max 200005
struct st{
ll index,freq,val;
};
int main(){
ll t;
cin>>t;
while(t--){
st s[max]={0};
ll n;
cin>>n;
ll counter[max]={0};
for(ll i=1;i<=n;i++){
ll x;
cin>>x;
s[x].val=x;
s[x].index=i;
s[x].freq++;
cout<<s[x].freq<<endl;
}
}
return 0;
}
I did this.
The problem is solved. I used dev c++ ide, that's why the code was doing problem. Thanks all

I need help regarding weird time limit exceeded error in cpp

So there was this code which got accepted when i took its output in a vector
#include <iostream>
#include<vector>
using namespace std;
int main(){
int t; cin >> t;
while(t--){
vector<int>v;
int n,k; cin >> n >> k;
for(int i=0;i<n;i++){
int x; cin >> x;
if(x%k==0) v.push_back(1);
else v.push_back(0);
}
for(auto x:v) cout <<x <<"";
cout << endl;
}
return 0;
}
but then there is this code gives Time Limit Exceeded error when i am direct printing it
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
int t;
cin>>t;
while(t--)
{
ll k,d;
int n,i;
cin>>n>>k;
for(i=0;i<n;i++)
{
cin>>d;
if(d%k==0)
cout<<"1";
else
cout<<"0";
}
cout<<"\n";
}
}
Can you tell why ?
(The contest is now over)Here is the question in case
Edit:1 I used int instead of long long as well as printf as well as cin.tie(NULL) stuff , but still to no avail
The implementation with the cout in the for loop body is going to bottle-beck on the cout output for sure, especially given a modulo operation is very cheap in contrast.
See below question as reference:
C++: Does cout statement makes code slower
Something like this would work better:
#include <bits/stdc++.h>
#include <vector>
using namespace std;
#define ll long long int
int main()
{
int t;
cin>>t;
while(t--)
{
ll k,d;
int n,i;
cin>>n>>k;
std::vector<bool> r(n);
for(i=0;i<n;i++)
{
cin>>d;
if(d%k==0)
r[i] = true;
}
for(auto i : r)
cout<<(i ? '1' : '0')<<endl;
cout<<"\n";
}
}

I got a RunTime error SIGCONT for the below code

#include<iostream>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n,ctr=0;
cin>>n;
int a[n],ptr=750;
for(int i=0;i<n;i++){
cin>>a[i];
if(a[i]<ptr){
ctr++;
ptr=a[i];
}
}
cout<<ctr-(n/5)+1<<"\n";
}
return 0;
}
All other answers say I would have divided by zero or accessed out of bounds but here I do not find any such situation

Declaring 2-dimensional array in "Edit distance" on spoj locally gives runtime error?

While solving http://www.spoj.com/problems/EDIST/ , when I declare the 2-d array globally: (http://ideone.com/jG3jPW)
#include <iostream>
using namespace std;
long long int s[2001][2001];
int main() {
int t;
string a,b;
long long int i,j;
for(i=0;i<2001;i++)
{
s[i][0]=i;
s[0][i]=i;
}
cin>>t;
while(t>0)
{
cin>>a>>b;
t--;
for(i=1;i<=a.length();i++)
{
for(j=1;j<=b.length();j++)
{
if(a[i-1] == b[j-1])
s[i][j]=s[i-1][j-1];
else
s[i][j] = min(min(s[i-1][j],s[i-1][j-1]),s[i][j-1]) + 1;
}
}
cout<<s[i-1][j-1]<<"\n";
}
return 0;
}
no error occurs. But when i declare the same array locally(http://ideone.com/Tyj6UU),
#include <iostream>
using namespace std;
int main() {
int t;
string a,b;
long long int i,j;
long long int s[2001][2001]; //declared locally
for(i=0;i<2001;i++)
{
s[i][0]=i;
s[0][i]=i;
}
cin>>t;
while(t>0)
{
cin>>a>>b;
t--;
for(i=1;i<=a.length();i++)
{
for(j=1;j<=b.length();j++)
{
if(a[i-1] == b[j-1])
s[i][j]=s[i-1][j-1];
else
s[i][j] = min(min(s[i-1][j],s[i-1][j-1]),s[i][j-1]) + 1;
}
}
cout<<s[i-1][j-1]<<"\n";
}
return 0;
}
runtime error occurs. Why?
It seems that you are having some kind of problem with memory allocation.
In your second approach (i.e., the one with local declaration), change:
long long int s[2001][2001];
To:
long long int ** s = new long long int * [2001];
for (i=0;i<2001;i++)
s[i] = new long long int[2001];
This will solve your problem.