When I try executing this code, the code crashes before the cout statement in print_array even executes. I have no clue why !
However, if I comment out the call to mergesort in the main function, the print_array executes fine.
#include<iostream>
using namespace std;
void print_array( int A[], int n)
{
int i;
cout<<"\n Array elts:\t";
for(i=0;i<n;i++) cout<<A[i]<<" ";
}
void mergesort(int A[], int beg, int end)
{
if(beg>end) return;
mergesort(A,beg,(beg+end)/2);
mergesort(A, ((beg+end)/2)+1, end);
int B[end-beg+1],i,j,k;
i=beg; j=(beg+end)/2;
k=0;
while(i<(beg+end)/2 && j<end)
{
if(A[i] < A[j]) B[k++]=A[i++];
else B[k++]=A[j++];
}
while(i<(beg+end)/2) B[k++]=A[i++];
while(j<end) B[k++]=A[j++];
for(i=beg; i<end; i++) A[i]=B[i];
}
int main()
{
int n=10;
int A[]={1,23,34,4,56,60,71,8,99,0};
print_array(A,n);
mergesort(A,0,n);
print_array(A,n);
}
Update:
Using endl will flush the output and the print_array values will get displayed on the screen. Apart from this, the reason I got a seg fault was because I had not included the equality check in mergesort. Here is the updated code:
#include<iostream>
using namespace std;
void print_array( int A[], int n)
{
int i;
cout<<"\n Array elts:\t";
for(i=0;i<n;i++) cout<<A[i]<<" ";
}
void mergesort(int A[], int beg, int end)
{
if(beg>=end) return;
mergesort(A,beg,(beg+end)/2);
mergesort(A, ((beg+end)/2)+1, end);
int B[end-beg+1],i,j,k;
i=beg; j=(beg+end)/2;
k=0;
while(i<(beg+end)/2 && j<end)
{
if(A[i] < A[j]) B[k++]=A[i++];
else B[k++]=A[j++];
}
while(i<(beg+end)/2) B[k++]=A[i++];
while(j<end) B[k++]=A[j++];
for(i=beg; i<end; i++) A[i]=B[i];
}
int main()
{
int n=10;
int A[]={1,23,34,4,56,60,71,8,99,0};
print_array(A,n);
mergesort(A,0,n);
print_array(A,n);
}
The code is by no means doing what it should but it isn't giving seg faults anymore.
Thanks guys !
Look at the last line in mergesort: You're accessing elements in B from beg to end, but B is zero indexed. That's one problem; there may be more.
My first though is that this is a Stack Corruption which is leading to a "Stack Overflow" when you call other methods. Try getting additional information in GDB, try compiling increasing the debugging level and turning off the optimizations of gcc (i.e., -g3 -O0).
Moreover, you can use the "valgrind" software to find the corruption and post the results here. (Sorry for requesting this here, but I cannot make comments).
Related
#include<iostream>
using namespace std;
int main(){
int n;
int current_element;
cin>>n>>current_element;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int i;
for(i=n-1;i<=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}
I was trying to do linear search program but there was no output, what is wrong in the code?
I think the problem is that you declared " i " twice and that has no sense. Here is the right code:
#include<iostream>
using namespace std;
int main(){
int n, i=0;
int current_element;
cin>>n>>current_element;
int arr[n];
for(i=0;i<n;i++){
cin>>arr[i];
}
for(i=n-1;i<=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}
This way you initialize " i " once with the value of 0 and it should work.
i think its because in your for loop you said
for(int i = n-1; i <= 0; --i)
this doesnt seem like what you are trying to do as it says that it will keep running as long as i<=0 however i is never less than or equal to zero. i think you would want to write i>=0 instead. also, you have 2 "i" variables which you initialized.
It seems that you made a mistake on the second loop clause.
Your code
...
//loop condition is causing the problem
for(i=n-1;i<=0;--i){
...
Fixed code
...
//loop condition is causing the problem
for(i=n-1;i>=0;--i){
...
The loop condition causes the program to not enter the second for loop. Modifying it makes the program work.
Full code
#include<iostream>
using namespace std;
int main(){
int n;
int current_element;
cin>>n>>current_element;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int i;
//loop condition is causing the problem
for(i=n-1;i>=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}
I wrote this simple code for bubble sort but it gives some random garbage values as output. Can someone please tell me my mistake. I tried to print the output of A[i] and A[j] in the function bubbleSort and looks like it is working fine. But why is the printSortedArray not giving the correct output? Thanks!
#include <iostream>
using namespace std;
void swap(int *a, int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void printSortedArray(int A[],int size)
{
cout<<"the sorted array is"<<endl;
int i;
for(i=0;i<size;i++);
{
cout<<A[i]<<" ";
}
}
void bubbleSort(int A[],int size)
{
int i,j;
for(i=0;i<size;i++)
{
for(j=0;j<size-1-i;j++)
{
if(A[j]>A[j+1])
{
swap(A[j],A[j+1]);
}
}
}
}
int main()
{
int A[50]; int size,i;
cout<<"enter the size of the array: ";
cin>>size;
cout<<"Enter the "<<size<<" numbers to be sorted"<<endl;
for(i=0;i<size;i++)
{
cin>>A[i];
}
bubbleSort(A,size);
printSortedArray(A,size);
return 0;
}
for(i=0;i<size;i++);
The trailing semicolon does not belong there. This results in undefined behavior.
The end result is that this function printed one garbage value after the end of the array.
A recursive algorithm that gets an integer n and integer array A as input and check if the given array
A contains two integers, such that n= A[i]+A[j] where A[i] & A[j] can be at any position in array.
This is what I have come up with
#include <iostream>
using namespace std;
void func(int arr[],int size,int n)
{
if (size==1)
return;
else
{
for (int i=0;i<n;i++)
for (int j=i+1;j<n;j++)
if (arr[i]+arr[j]==n)
{
cout<<"yes";
return;
}
func(arr,size-1,n);
}
}
int main()
{
int A[] = {1,2,3,4,5};
int n = 8;
func(A,5,n);
return 0;
}
I know it's using recursion but I want a recursive function that avoids all unnecessary iterations. Any help will be greatly appreciated!!
#include <iostream>
using namespace std;
void func(int arr[],int index,int size,int n)
{
if(index==size-1)
return;
int flag=0;
for (int j=index+1;j<size;j++)
if (arr[index]+arr[j]==n)
{
flag=1;
cout<<"yes";
}
if(flag==0)
func(arr,index+1,size,n);
}
int main()
{
int A[] = {1,2,3,4,5};
int n = 6;
func(A,0,5,n);
return 0;
}
If you really want a recursive code, this may be one kind of implementation. All i have done is start from 0th index and sum it with every other element and compare it with with 'n'. If it matches fine otherwise make a recursive call with next index(prev index + 1).
I have pasted my implementation of the quicksort algorithm below. After some debugging I figured out that the recursion of the function quicksort() does not seem to terminate. But it seems to me that my algorithm is fine and I am not able to fix the bug.
/*
quicksort
*/
int a[20];
int partition(int left,int right, int*a)
{
//chose some pivot element- in this case i choose the middle one
int pivot=(left+right)/2;
int b[10],c[10],i=left,j=0;
int k=0;
int pivot_element=a[pivot];
//b is the left side ,c is the right side
while(i<=right)
{
if(a[i]!=pivot_element)
{
if(a[i]<a[pivot])
{
b[j++]=a[i];
}
else
{
c[k++]=a[i];
}
}
i++;
}
//combine
i=left;
for(int q=0;q<j;q++)
a[i++]=b[q];
a[i++]=pivot_element;
for(int p=0;p<k;p++)
a[i++]=c[p];
return j; //return the new position of the pivot
}
void quicksort(int left,int right,int *a)
{
int index=partition(left,right,a);
if(index-left>0)
quicksort(left,index,a);
if(right-index+1>0)
quicksort(index+1,right,a);
}
int main()
{
int size;
cin>>size;
for(int i=0;i<size;i++)
cin>>a[i];
quicksort(0,size-1,a);
for(int i=0;i<size;i++)
cout<<a[i]<<" ";
return 0;
}**
In your partition function, this:
return j;
should be this:
return left+j;
You could have detected this bug by testing the function, before writing other code that called it.
Your recursion lacks a stop criteria.
I've been trying to make a qsort algorithm, but so far I've failed miserably. Keep in mind I'm kind of a newbie when it comes to programming, so yeah. After I build and run, and input my array, it returns the same exact array, instead of sorting it. Here's the code in question:
#include <iostream>
using namespace std;
int v[11], i, n, st, dr;
void qsort (int v[11], int st, int dr)
{
int i=st, j=dr;
int aux;
int pivot = v[(st+dr)/2];
while(i<=j)
while(v[i]<pivot)
{
i++;
if(i<=j)
{
aux=v[i];
v[i]=v[j];
v[j]=aux;
i++;
j--;
}
}
if(st<j)
qsort(v,st,j);
if(i<dr)
qsort(v,i,dr);
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>v[i];
st=v[1];
dr=v[n];
qsort(v, st, dr);
cout<<"vectorul sortat este"<<' ';
for(i=1;i<=n;i++)
cout<<v[i]<<' ';
return 0;
}
Thanks in advance!
st and dr should be the initial and final indices where you want to sort, not the values (also, keep in mind that in C++ a vector on n elements has indices from 0 to n-1, so fix also your for loops), so you have to change
st=v[1];
dr=v[n];
to
st=0
dr=n-1
or simply:
qsort(v, 0, n-1);