Question: Given a number k, find the sum of k positive big integers.
This is my code,it works, but our Online Judge is rejecting it, says segfault. Why does it show segfault? I could do it with two strings but why isn't this working?
#include <iostream>
#include <string.h>
using namespace std;
void add(int l,int k);
void append(char a[], int temp);
int o;
int tf=0;
int carry=0;
char b[1000000];
char a[10000][10000];
char c[1000000];
int main()
{
int k,x=0,l=0,m=0;
cin>>k;
while(x<k)
{
cin>>a[x];
if(strlen(a[x])>l)
{
l=strlen(a[x]);
}
x++;
}
x=0;
while(x<k)
{
if(strlen(a[x])<l)
{
int temp=0;
append(a[x],l-strlen(a[x]));
}
x++;
}
add(l,k);
if(carry!=0)
{
cout<<carry;
}
while(o>=0)
{
cout<<(int)b[o];
o--;
}
}
void add(int l,int k)
{
int lb=l-1;
int r=k-1;
int sum=0;
int x=0;
int neg=0;
while(lb>=0)
{
r=k-1;
sum=0;
while(r>=0)
{
sum=sum+a[r][lb]-48;
r--;
}
sum=sum+carry;
lb--;
if(sum>=10)
{
b[x]=sum%10;
carry=sum/10;
}
else
{
b[x]=sum;
carry=0;
}
sum=0;
o=x;
x++;
}
}
void append(char a[], int temp)
{
int l=0,m;
int tempb=temp;
m=strlen(a)-1;
while(temp>0)
{
c[l]='0';
l++;
temp--;
}
int z=0;
while(z<=m)
{
c[l]=a[z];
z++;
l++;
}
z=0;
while(z<=m+tempb)
{
a[z]=c[z];
z++;
}
}
Input Format:
First line contains k , which specifies the number of big numbers. Each of the next k lines contains a
big positive integer.
Output Format:
For each test case print the new big integer in a new line
Sample Input:
3
1331331
1313
453535322
Sample Output:
454867966
Constraints:
1<=k<=10
1<=number of digits in big numbers<=10000
Based on the problem statement:
Each input entry my have at most nmax = 10000 digits
Since each entry is stored as a C-style, zero-terminated string, each character array must have a length of (nmax + 1) = 10001 characters, to accomodate for the C-string terminator '\0'.
As you stored the entries into character arrays without leaving room for the zero terminator, assuming each entry was 10000-characters long:
Each entry with k>=1 overwrote the terminator or entry k-1, thus coalescing the entries together;
You thus ended with one giant string, with l = strlen(a[0]) = 100000;
From then on, all further processings were performed with these incorrect (coalesced) inputs and length, leading to buffer overrun at some point later in the execution.
Related
Given an unsorted array arr[] of size N, rotate it by D elements (clockwise).
Input
The first line of the input contains T denoting the number of testcases. First line of each test case contains two space separated elements, N denoting the size of the array and an integer D denoting the number size of the rotation. Subsequent line will be the N space separated array elements
Output
For each testcase, in a new line, output the rotated array
Example
Input
1 2 3 4 5
Output
3 4 5 1 2
#include <iostream>
#include <stack>
using namespace std;
void rotate(int *a,int s,int r) {
stack<int> st;
for(int i=0;i<r;i++) {
st.push(a[i]);
}
for(int j=r;j<s;j++) {
a[j-r] = a[j];
}
for(int k=s-1;k>r+1;k--) {
a[k] = st.top();
st.pop();
}
for(int l=0;l<s;l++) {
cout<<a[l]<<" ";
}
}
int main() {
//code
int T;
cin>>T;
while(T--) {
int N,r;
cin>>N>>r;
int A[N];
for(int i=0;i<N;i++) {
cin>>A[i];
}
rotate(A,N,r);
cout<<endl;
}
return 0;
}
I followed your logic, it seems like there is problem in your backfill part.
for(int k=s-1;k>=s-r;k--) { // change k>r+1 to k>=s-r
a[k] = st.top();
st.pop();
}
sorry my bad, int third for loop in rotate function there should be k>s-r-1
In the following code we have to first calculate the weights of uniform substrings present in our strings . Uniform sub strings are those which contain just one character like "a" or "aaa". The weight of the character is defined as a-1 b-2......z-26.
After calculating the weights of all the valid uniform substrings we will be given with various queries and we have to check whether the given no. is the array or not.
Here is the link of the code and corresponding output to it:
https://www.ideone.com/pIBPtQ
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin>>s;
int i=0,j=0,k=0;
int arr[10000];
int c=0;
while(s[i]!='\0')
{
int x=(int)s[i];
x=x-96;
arr[c++]=x;
j=i+1;
int sum=x;
while(s[j]==s[i])
{
sum+=x;
arr[c++]=sum;
j++;
}
i=j;
}
int q;
cin>>q;
for(i=0;i<q;i++)
{
int val;
cin>>val;
bool exists=find(begin(arr),end(arr),val)!=end(arr);
if(exists==true)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
cout<<"the elements of the array are:"<<endl;
for(i=0;i<c;i++)
cout<<arr[i]<<" ";
return 0;
}
You forgot to initialize arr.
Change
int arr[1000];
to
int arr[1000] = {0};
https://www.ideone.com/wIj4vp
Also x=x-96; should be better written as x -= 'a';.
this is the following code
#include<iostream>
using namespace std;
int findPivot(int a[],int startIndex,int endIndex)
{
int pivot=a[endIndex];
int pivotIndex=startIndex;
for(int i=0;i<endIndex-1;i++)
{
if(a[i]<pivot)
{
int temp=a[i];
a[i]=a[pivotIndex];
a[pivotIndex]=a[i];
pivotIndex++;
}
}
int temp=pivot;//swapping pivot element into its position.
pivot=a[pivotIndex];
a[pivotIndex]=temp;
return pivotIndex;
}
void quickSort(int a[],int startingIndex,int endingIndex)
{
int number;
if(startingIndex < endingIndex)
{
int returnValueOfPivot= findPivot(a,startingIndex,endingIndex);
//cout<<returnValueOfPivot<<endl;
quickSort(a,startingIndex,returnValueOfPivot-1);//sorting for left
quickSort(a,returnValueOfPivot+1,endingIndex);//sorting for right
}
}
int main()
{
int number;
cout<<"Enter the total number of elements"<<endl;
cin>>number;
cout<<"Enter the values"<<endl;
int a[number-1];
for(int i=0;i<number;i++)
{
cin>>a[i];
}
quickSort(a,0,number-1);
for(int i=0;i<number;i++)
{
cout<<a[i]<<",";
}
return 1;
}
There are three major problems in your code :
int a[number-1];
You are allocating 1 less space for your array. Note that, array index starts from 0. So array of 5 numbers will be like
array[5] : array[0],array[1],array[2],array[3],array[4]
Swapping array values :
int temp=pivot;//swapping pivot element into its position.
pivot=a[pivotIndex];
a[pivotIndex]=temp;
Here, you swapped pivot value with a[pivotIndex] not a[endIndex]!!
So the correct swap would have been :
int temp=a[endIndex];//swapping pivot element into its position.
a[endIndex]=a[pivotIndex];
a[pivotIndex]=temp;
for(int i=0;i<endIndex-1;i++) is incorrect loop
correct loop would be :
for(int i=startIndex;i<=endIndex-1;i++)
You need to start from the start index and end till the end index. You are currently going from 0 to end - 1. [Think of the right side array loop, it won't start with 0]
Make these changes and your code will work.
i was asked to create a recursive function that outputs the minimum number of the array that the user enters. the problem is when run the code the output is funny and the cout is repeated many times with weird values.
#include <iostream>
using namespace std;
void recursiveMinimum(int i, int f, int *a, int min)
{
if(a[i]<min)
{
min=a[i];
}
i++;
if (i<f)
{
recursiveMinimum(i, f, a, min);
}
cout<<"the minimum number is "<<min<<endl;
}
void main ()
{
int *a, b=1000, f;
a= new int [b];
cout<<"please enter the array, enter '1000' without the quotes to stop"<<endl;
for(int i=0; i<b; i++)
{
cin>>a[i];
if (a[i]==1000)
{
a[i]=NULL;
f=i;
break;
}
}
recursiveMinimum(0, f, a, a[0]);
system("pause");
}
I don't know C++ very well, but doesn't cout get called many times, specifically each time the recursive function gets called? Here is the updated recursiveMinimum function:
void recursiveMinimum(int i, int f, int *a, int min)
{
if(a[i]<min)
{
min=a[i];
}
i++;
if (i<f)
{
recursiveMinimum(i, f, a, min);
} else {
cout<<"the minimum number is "<<min<<endl;
return;
}
}
I moved the print statement into an else statement. When I isn't less than F, which should only happen when you have reached the end of the array, the program will print the minimum number and end.
I have to implement c++ code to find out the number of occurrences of a small string in a large string with overlapping allowed. For eg- if large string is acacab and small string is aca, then answer should be 2.
I'm not getting correct answer by making this code:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int i, j, k, c=0;
char lstr[30],sstr[10],tstr[10];
cout<<"Enter large string:"<<endl;
cin>>lstr;
cout<<"Enter small string:"<<endl;
cin>>sstr;
for(i=0;i<strlen(lstr);i++)
{
if(lstr[i]==sstr[0])
{
j=i;
for(k=0;k<strlen(sstr);k++,j++)
{
tstr[k]=lstr[j];
}
}
if(strcmp(tstr,sstr)==0)
c++;
}
cout<<c;
return 0;
}
It's much easier if you use C++ strings instead of C ones:
int c=0;
string lstr, sstr;
cout<<"Enter large string:"<<endl;
cin>>lstr;
cout<<"Enter small string:"<<endl;
cin>>sstr;
for (size_t pos = 0;
(pos = lstr.find(sstr, pos)) != string::npos;
pos++)
{
c++;
}
cout<<c;
This should work, add a '\0' to terminate c-style string, and move if(strcmp(tstr,sstr)==0) in the if(lstr[i]==sstr[0]) statement(otherwise you will continuing increment c when lstr[i] != sstr[0]):
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int i, j, k, c=0;
char lstr[30],sstr[10],tstr[10];
cout<<"Enter large string:"<<endl;
cin>>lstr;
cout<<"Enter small string:"<<endl;
cin>>sstr;
for(i=0;i<strlen(lstr);i++)
{
if(lstr[i]==sstr[0])
{
j=i;
for(k=0;k<strlen(sstr) && j<strlen(lstr);k++,j++)
{
tstr[k]=lstr[j];
}
tstr[k] = 0;
// ^^^^^^^^^^^^
if(strcmp(tstr,sstr)==0)
c++;
}
}
cout<<c;
return 0;
}
This will solve your problem more efficiently in O(n*m) complexity, where n is large string size and m is small string size.
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int i, j, k, c=0;
char lstr[30],sstr[10];
cout<<"Enter large string:"<<endl;
cin>>lstr;
cout<<"Enter small string:"<<endl;
cin>>sstr;
for(i=0; lstr[i]; i++) // You don't need to calculate strlen each time. This loop will break when lstr reaches null ('\0') character
{
if(lstr[i]==sstr[0])
{
for(k=0,j=i; sstr[k] && lstr[j]; k++,j++) // don't need to calculate strlen each time
{
if(sstr[k]!=lstr[j]) // Break if not match
break;
}
if(k==strlen(sstr)) // Whole string matched
c++;
}
}
cout<<c<<"\n";
return 0;
}
If you want to solve this problem in O(n) complexity you can use KMP algorithm. Which is the best choice for this kind of problems.