mergesort in c++:segmentation fault - c++

I am getting segmentation fault while implementing mergesort.
I have checked for array out of bounds.I would love some help to find out where I have gone wrong.I have tried inputs for small arrays such as of size 10 where I have taken size of temp as static(>10).I have been pulling my hair out for hours.
UPDATE: I only needed to change mid=(low+high)/2.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void merges(int arr[],int low,int mid,int high)
{
int i=low,j=mid+1,k=0;
int temp[high-low+1];
while(i<=mid && j<=high)
{
if(arr[i]<arr[j])
{
temp[k++]=arr[i];
i++;
}
else
{
temp[k++]=arr[j];
j++;
}
}
if(i>mid)
{
while(j<=high)
{
temp[k++]=arr[j];
j++;
}
}
else
{
while(i<=mid)
{
temp[k++]=arr[i];
i++;
}
}
j=0;
for(i=low;i<=high;i++)
{
arr[i]=temp[j++];
}
}
void mergesort(int arr[],int low,int high)
{
if(low<high)
{
int mid=low+high/2;
mergesort(arr,low,mid);
mergesort(arr,mid+1,high);
merges(arr,low,mid,high);
}
}
int main(){
int n;
cin >> n;
int arr[n];
for(int arr_i = 0;arr_i < n;arr_i++){
cin >> arr[arr_i];
}
int i,j,k;
mergesort(arr,0,n-1);
for(i=0;i<n;i++)
cout<<arr[i];
return 0;
}

i only needed to change mid=(low+high)/2. Thank you #Gerado Gálvez for your suggestion

You're recursively calling mergesort. Although your general case seems legitimate, I cannot ascertain your base case (i.e. to exit the recursion).

Related

sum of an array elements in cpp return wrong value

I tried to write a simple code to calculate an array elements' sum. every thing looks normal but the function return the sum value wrongly (it always multiply it by two). Although if I want just print the value, it works fine.
this is the code:
#include <iostream>
using namespace std;
void getElements(int[],int);
int sumOfElements(int[],int);
int number;
int sum=0;
int main()
{
int a[10];
getElements(a,5);
sumOfElements(a,5);
cout<<"The sum is "<<sumOfElements(a,5)<<endl;
return 0;
}
//Getting array's elements
void getElements(int numbers[],int size_)
{
for (int i=0; i<size_; i++)
{
cout<<"numbers["<<i<<"]: ";
cin>>number;
numbers[i]=number;
}
cout<<'\n';
}
//Calculation the sum of array's elements
int sumOfElements(int numbers[],int size_)
{
for(int i=0;i<size_;i++)
{
sum+=numbers[i];
}
cout<<sum<<endl;
return sum;
}
any idea? thank you in advance!
You defined int sum globally and were calling sumOfElementstwice, so sum contained twice what you expected.
Here is a modified version of your code that does what you want:
#include <iostream>
using namespace std;
void getElements(int[], int);
int sumOfElements(int[], int);
int main() {
int numbers[5];
getElements(numbers, 5);
cout << sumOfElements(numbers, 5);
return 0;
}
void getElements(int numbers[], int size) {
for (int i = 0; i < size; i++) {
cin >> numbers[i];
}
}
int sumOfElements(int numbers[], int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += numbers[i];
}
return sum;
}
Here is a modified and simpler version of your program:
#include <array>
#include <iostream>
#include <numeric>
using namespace std;
int main(){
const int num_elements_to_sum = 5;
array<int, num_elements_to_sum> elements;
for(int i=0; i<num_elements_to_sum; ++i){
cin>>elements[i];
}
int sum = accumulate(elements.begin(), elements.end(), 0);
cout<<"Sum: "<<sum<<endl;
return 0;
}
C++ has a dedicated fixed size array container, use this instead of C-style arrays. This then allows to use standard library algorithms instead of your own implementation (e.g. accumulate).

Wrong answer on SPOJ (PRIME1)

Here is the link to the problem -> PRIME1
It asks us to print all the prime numbers between two numbers m and n
I used segmented sieve to solve the problem. Stored all the primes till sqrt(10^9) and used them to get primes in a specific range. Please help me out.
#include <iostream>
#include <vector>
using namespace std;
vector<int> v;
vector<bool> prime(32022);
void precompute()
{
int n=32000;
prime[0]=prime[1]=true;
for(int i=2;i*i<=32000;i++)
{
for(int j=i*i;j<=32000;j+=i)
prime[j]=true;
}
for(int i=0;i<=32000;i++)
if(!prime[i]) v.push_back(i);
}
int main() {
precompute();
int t; scanf("%d",&t);
while(t--)
{
int m,n; scanf("%d%d",&m,&n);
if(n<=32000)
{
for(int i:v)
{
if(i>n) break;
if(i>=m && i<=n)
printf("%d\n",i);
}
}
else
{
vector<bool> prime(n-m+1,true);
for(int i:v)
{
int st=(m/i)*i;
if(st<m) st+=i;
while(st<=n)
{
prime[st-m]=false;
st+=i;
}
}
for(int i=0;i<n-m+1;i++)
{
if(prime[i]) printf("%d\n",i+m);
}
}
printf("\n");
}
}

building MIN-Heap in c++

I am trying to implement min-heap just by reversing the propeties of max_heapify int the code but I am getting a segmentation fault(core dumped) during execution.What I know somewhere the code is accessing a memory to which it does not has access to. What is the problem with this code?
#include <iostream>
using namespace std;
void min_heapify(int a[], int i, int n)
{
int l,r,smallest,loc;
l=(2*i);
r=(2*i+1);
if((l<=n) && (a[l]>a[i]))
smallest=i;
else
smallest=l;
if((r<=n) && (a[smallest]>a[r]))
smallest=r;
if(smallest != i)
{
loc=a[i];
a[i]=a[smallest];
a[smallest]=loc;
min_heapify(a,smallest,n);
}
}
void build_min_heap(int a[], int n)
{
for(int k=n/2;k>=1;k--)
{
min_heapify(a,k,n);
}
}
void heapsort(int a[], int n)
{
build_min_heap(a,n);
int temp,i;
for(i=n;i>=2;i--)
{
temp=a[i];
a[i]=a[1];
a[1]=temp;
min_heapify(a,1,i-1);
}
}
main()
{
cout<<"Enter the number of elements\n";
int n;
cin>>n;
cout<<"Enter the numbers\n";
int a[n];
for(int i=1;i<=n;i++)
cin>>a[i];
heapsort(a,n);
cout<<"::::::::::::::::The Sorted List Is::::::::::::::\n";
for(int i=1;i<=n;i++)
cout<<a[i]<<endl;
return 0;
}

Median program through uva

This is my first time using online judge, I tried a simple program to become familiar with the environment.
Here is the question.
I solved it as the following, but get a wrong answer!
#include<stdio.h>
#include<math.h>
#include<iostream>
int main()
{
int t;
int n;
int num[10];
int i,j,temp;
int s;
int fmid;
std::cin >>t;
int iter=0;
while (iter<t)
{
std::cin>>n;
if (n!=-1){
for(i=0;i<n;i++)
std::cin>>num[i];
for( i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(num[i]>num[j])
{temp=num[i];
num[i]=num[j];
num[j]=temp;
}
s=0;
if (n%2 ==0)
{
int mid=n/2-1;
int midd=mid+1;
s=(num[mid]+num[midd])/2;
fmid=s;
}
else
{s=ceil(n/2);
fmid=num[s];}
std::cout<<fmid;
}
iter++;
}
return 0;
}
Any suggestion is much appreciated.
Thanks
I would read all the numbers, store them in an array, then sort the array using std::sort in <algorithm>
Please find my code below:
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
int arr[10];
int main()
{
int N;
while ((std::cin >> N) && (N!=-1)){
for(int i=0;i<N;i++) {
std::cin >> arr[i];
}
std::sort(arr,arr+N);
if(N%2 == 1){
std::cout << arr[N/2] << std::endl;
}
else {
double ans = ((double)arr[N/2] + (double)arr[(N/2)-1]) / 2.0;
std::cout << ans << std::endl;
}
}
return 0;
}
I hope it works for you.
May I know the problem number? I will try to submit there and give you an AC solution.

How to debug this code for splitting an array by space?

I need to write a program that get a sentence and split its words by a delimiter(space);so I've wrote the code below but it doesn't seem to be working properly.
any idea's how to debug this code?
thanks in advance for your help.
here's what I come up with so far:
#include <iostream>
using namespace std;
const int BUFFER_SIZE=255;
int main()
{
char* buffer;
buffer=new char[255];
cout<<"enter a statement:"<<endl;
cin.getline(buffer,BUFFER_SIZE);
int q=0, numofwords=1;
while(buffer[q] != '\0'){
if(buffer[q]==' ') numofwords ++;
q ++;
}
char** wordsArray;
wordsArray= new char* [numofwords];
int lenofeachword=0, num=0;
int* sizeofwords=new int [numofwords];
for(int i=0;i<q;i++){
if(buffer[i]==' ')
{
sizeofwords[num]=lenofeachword;
wordsArray[num]=new char[lenofeachword];
num++;
}else{
lenofeachword++;
}
}
sizeofwords[num]=lenofeachword;
wordsArray[num]=new char[lenofeachword];
int k=0;
for(int i=0; i<numofwords;i++){
for(int j=0;j<sizeofwords[i];j++)
{
wordsArray[i][j]=buffer[k];
k++;
}
k++;
}
for(int i=0; i<numofwords;i++){
for(int j=0;j<sizeofwords[i];j++)
{
cout<<wordsArray[i][j];
}
cout<<endl;
}
}
#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
int main() {
int size, j;
char s[1005];
gets(s);
scanf("%d", &size);
j=0;
for(int i=size; i<strlen(s); i+=size) {
for(; j<i; j++) {
printf("%c", s[j]);
}
printf(" ");
}
return 0;
}
You forgot to assign zero to lenofeachword after you deretmine lenght of a word. And if int main(), you should return an int value.