Add to Array-Form of Integer - c++

Line 17: Char 11: runtime error: signed integer overflow: 9223372036854775807 + 516 cannot be represented in type 'long long' (solution.cpp)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:26:11
Code is below:
class Solution {
public:
vector<int> addToArrayForm(vector<int>& num, int k) {
string str;
int n=num.size();
long long int number;
for(int i=0;i<n;i++)
{
str+=to_string(num[i]);///converting vector into string
}
stringstream shru(str);///converting string into number
shru>>number;
number+=k;
cout<<number;
str=to_string(number);
int len=str.length();
num.resize(len);
int last;
for(int i=len-1;i>=0;i--)
{
last=number%10;
num[i]=last;
number=number/10;
}
return num;
}
};
I tried using long long int but I don't know what is going wrong. It is tagged easy but still coudn't do it.

Related

Why these two binary-to-decimal conversion functions give different results? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Why is my integer math with std::pow giving the wrong answer?
(5 answers)
Closed 9 months ago.
This piece of code is taken from a bigger code, where it was creating a problem. I am able to reproduce the same error here.
The code requires unsigned long long integers to deal with large values. The requirement is to convert a given binary number (stored in a vector) to a decimal number (unsigned long long). It is supposed to be a simple code. But it turns out the function configuration_to_int_1 produces wrong result for some binary numbers. If you run the code, you will see that in the very last step of the loop, it adds an even and an odd number to give an even output, which is bizarre!! After spending an entire day to understand the root of this problem, I rewrote the function as configuration_to_int_2
where I have just stored the value of pow(2,i) in a long long integer at the beginning and used this variable whenever pow(2,i) was required. This gives me the correct value!!
Can anyone explain to me, what was going wrong in the first case? Thanks in advance. Here is the code:
#include <stdio.h>
#include <bitset>
#include <vector>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
unsigned long long hash=9007199256641537;
int NLL, Nphi;
NLL=3;
Nphi=18;
vector<unsigned short int> basis(3*Nphi,0);
unsigned long long int configuration_to_int_1(vector<unsigned short int> conf, int NLL, int Nphi);
unsigned long long int configuration_to_int_2(vector<unsigned short int> conf, int NLL, int Nphi);
void int_to_occupation(unsigned long long int num, int Nphi, int NLL, vector<unsigned short int>& occupation);
int_to_occupation(hash, Nphi, NLL, basis);
configuration_to_int_1(basis, NLL, Nphi);
configuration_to_int_2(basis, NLL, Nphi);
return 0;
}
void int_to_occupation(unsigned long long int num, int Nphi, int NLL, vector<unsigned short int>& occupation)
{
bitset<64> bb(num);
for (int r = 0; r < NLL*Nphi; ++r)
{
occupation[r] = bb[r];
}
}
unsigned long long int configuration_to_int_1(vector<unsigned short int> conf, int NLL, int Nphi)
{
unsigned long long int x=0;
for(int i=0;i< NLL*Nphi;i++)
{
if (conf[i]==1)
{
cout<<x<<" + "<<pow(2,i)<<" = ";
x=x+pow(2,i);
cout<<x<<endl;
}
}
return x;
}
unsigned long long int configuration_to_int_2(vector<unsigned short int> conf, int NLL, int Nphi)
{
unsigned long long int x=0,c;
for(int i=0;i< NLL*Nphi;i++)
{
if (conf[i]==1)
{
c=pow(2,i);
cout<<x<<" + "<<c<<" = ";
x=x+c;
cout<<x<<endl;
}
}
return x;
}

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;
}

convert string to array of integer in c++

Someone will help me, in understanding the while loop in following code about what it does? for example, take input from the user: 1 2 3 8 (input size not given) and one value (any index) show size of an array. it is printing the maximum value of the array. here the answer is 8.
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
int t;
cin >> t;
while(t--)
{
string x;
getline(cin, x);
ll p;
istringstream iss(x);// use of this function
vector<ll> v;
ll ans;
while(iss>>p)// what this loop do
{
v.push_back(p);
}
ll size=v.size()-1;
sort(v.begin(), v.end());
if(size==v[size])
{
ans=v[size-1];
}
else
{
ans=v[size];
}
cout<<ans<<"\n";
}
return 0;
}
istringstream iss(x) creates a string stream called iss consisting of the string x. iss >> p extracts the next element from the iss stream and puts it into p. the return value is int because the variable p is of type int.
while(iss>>p) // get an int value from string stream iss
{
v.push_back(p); // push the int value to the vector
}
you have to use cin.ignore() after cin. otherwise the next getline function will take only new line character. like this:
cin >> t;
cin.ignore();
string str;
cin>>str;
int size = str.size(),arr[size]; //array of size equal to string size
for(int i=0;i<size;i++)
{
arr[i] = (int)str[i]-'0'; //typecasting to int
}
for str[i]-'0' the explanation is :
What is the purpose of using str[i]-'0' where str is a string?
Error may occur if unable to typecast
Using function stoi() (for int values) or stol()(for long values) also optionfor integer values.
int n=56; // use #include<bits/stdc++.h>
string s=to_string(n); // we get string "56" that's for integer to string
int x=stoi(substr(index,length));// for string to integer

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.

C++ Reverse a number of any length

I want to reverse the number given by the user. I wrote the code in such a way that it takes the number of digits of number and the input and reverses it. As even the limit of 'long long' is 19 digits, what can I do so that the code works even if the number of digits in the input are greater than 20? [Without using third party libraries]
#include<iostream>
using namespace std;
void make_int(int a[],long long int n)
{
int i=0;
while(n)
{
a[i]=n%10;
i++;
n=n/10;
}
for(int j=0;j<i;j++)
cout<<a[j];
}
int main()
{
int N;
cin>>N;
int *tc = new int[N];
long long int num;
cin>>num;
make_int(tc,num);
return 0;
}
Read the number in as a std::string and reverse that, since it's actual numerical value is unimportant for what you want to do.