Minimum binary tree problem running infinite loop - c++

This C++ code is not terminating in Vscode. Can you explain why?
Here is the code
minimum of two numbers
int min(int x,int y)
{
if(x<y)
return x;
else
return y;
}
filling the minimum of siblings as parent in a binary tree
void minimumtree(int arr[], int b, int n)
{
if(b==0)
return;
else
{
for(int i=0;i<n;i+2)
{
int x=(b+i)/2;
arr[x]=min(arr[b+i],arr[b+i+1]);
}
minimumtree(arr,b/2,n/2);
}
}
int main()
{
leaf input n- number of leaves, b - beginning of index of the first leaf in an array
int n,b; cin>>n>>b; int arr[n+b];
for(int i=0;i<n;i++)
cin>>arr[b+i];
query input
int t;
cin>>t;
int q[t];
for(int i=0;i<t;i++)
cin>>q[i];
minimumtree(arr,b,n);
query output
for(int i=0;i<t;i++)
cout<<arr[q[i]];
return 0;
}

be clearer may help
int min(int x,int y) {
return x<y ? x: y;
}
// filling the minimum of siblings as parent in a binary tree
void minimumtree(int arr[], int b, int n) {
if(b) {
for(int i=0; i<n; i+=2)
{
int x=(b+i)/2;
arr[x]=min(arr[b+i],arr[b+i+1]);
}
minimumtree(arr,b/2,n/2);
}
return;
}
int main() {
// leaf input n- number of leaves, b - beginning of index of the first leaf in an array
int n,b;
cin>>n>>b;
int *arr = new int[n+b]; // size is in run-time so define or set it dynamically
for(int i=0; i<n; ++i)
cin>>arr[b+i];
// query input
int t;
cin>>t;
int q = new int[t];
for(int i=0; i<t; ++i)
cin>>q[i];
minimumtree(arr,b,n);
// query output
for(int i=0;i<t;i++)
cout<<arr[q[i]];
return 0;
}

Related

What is wrong with my code in finding the length of Maximum Bitonic Subarray

Logic used is simple if a triplet x,y,z occurs such that x>y<z then give the length of the array to m and continue and it gives right output in all the test cases I can think of
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int l=0;
int m=0;
for(int i=0;i<n;i++)
{
l++;
if(i>0&&i<n-1)
{
if(a[i]<a[i-1]&&a[i]<a[i+1])
{
l=1;
}
}
if(m<l)
m=l;
}
if(m)
cout<<m<<endl;
else
cout<<l<<endl;
}
return 0;
}

When i try to run this code output is "merge.exe has stopped working"?

#include<iostream>
#define INF 10000
using namespace std;
void mergeSort(int *x,int y,int z);
void merge(int *a,int p,int q,int r);
void main()
{
int i,size;
cin>>size;
int arr[size];
for(i=0;i<size;i++)
{
cin>>arr[i];
}
mergeSort(arr,0,size-1);
for(i=0;i<size;i++)
{
cout<<arr[i]<<endl;
}
}
This function takes in the array to be sorted and the values of starting and ending index i.e, y,z.
void mergeSort(int *x,int y,int z)
{
int q;
if(y<z)
{
q=(y+z)/2;
mergeSort(x,y,q);
mergeSort(x,q+1,z);
merge(x,y,q,z);
}
}
this function merges two sorted sections of the array and p,r are starting and ending index while q is the index which divides the array into two sorted parts
void merge(int *a,int p,int q,int r)
{
int l1,l2,i,j;
l1=q-p+1;
l2=r-q;
int left[l1+1],right[l2+1];
for(int i=0;i<l1;i++)
{
left[i]=a[p+i];
}
for(int j=0;j<l2;j++)
{
right[i]=a[q+j];
}
left[l1+1]=INF;
right[l2+1]=INF;
i=0;
j=0;
for(int k=0;k<=r-p;k++)
{
if(left[i]<right[j])
{
a[k]=left[i++];
}
else
{
a[k]=right[j++];
}
}
}
You have tried to access an out of bounds index in the following lines (in merge() subroutine):
left[l1 + 1] = INF;
right[l2 + 1] = INF;
The size is l1+1 and l2+1 respectively. So, the range of valid indices will be [0, l1] and [0, l2]. The lines should thus be:
left[l1] = INF;
right[l2] = INF;
The final problem is in the line:
for(int k = 0; k <= r-p; k++)
which should be
for(int k = p; k <= r; k++)
otherwise, you will just overwrite the other values. You have to insert the merged part at the proper position.

Segmentation Fault [2D array]

#include <iostream>
using namespace std;
void input_Array(char (&A)[10][10], int x, int y);
int calc_Collision(char (&A)[10][10]);
void display(char (&A)[10][10]);
void init_Array(char (&A)[10][10]);
int main()
{
int m,n,test;
char A[10][10];
init_Array(A);
cin>>test;
while (test>0)
{
cin>>m>>n;
input_Array(A,m,n);
display(A);
cout<<"FLAG";
cout<<calc_Collision(A);
test--;
}
}
//Calculates no. of ways to select two 1's in each column
int calc_Collision(char (&A)[10][10])
{
int count=0;
int sum=0;
int select(int x, int y);
for (int j = 0; j<10; j++)
{
count=0;
for(int i = 0; i<10; i++)
{
if (A[i][j]=='1')
{
count++;
}
}
sum=sum + select(count,2);
}
return sum;
}
//Returns no. of ways to select y items from x items
int select(int x, int y)
{
int fact(int a);
return (fact(x)/(fact(y)*fact(x-y)));
}
//Returns a!
int fact(int a)
{
if (a==0)
{
return 1;
}
else
{
return (a*fact(a-1));
}
}
void display(char (&A)[10][10])
{
for (int i=0; i<10; i++)
{
for (int j=0; j<10; j++)
{
cout<<A[i][j]<<"\t";
}
cout<<"\n";
}
}
Input Format:
no. of trials
no. of rows (whitespace) no. of columns
row1 (No space, 1's or 0's only)
row2...
Output:
The 2D array
Total no. of ways to select two one's in each column
Problem:
The program displays the array fine enough.
But upon coming across calc_Collision(A), the code outputs:
Segmentation fault (core dumped)
"FLAG" is NOT displayed.
Still a beginner here, so ANY help would be appreciated.
int select(int x, int y){
int fact(int a);
return (fact(x)/(fact(y)*fact(x-y)));
}
int fact(int a){
if (a==0) {
return 1;
}
else {
return (a*fact(a-1));
}
}
Notice that if x in select is less than 2, then fact(x-y) will call itself indefinitely. This is because the variable a in fact will be negative. This occurs when the size of the input array has less than 10 columns, resulting in the last column becoming empty. This causes the iteration of count to become 0 in calc_Collision. The segmentation fault does not occur if the input has 10 columns.
Since you are only computing nC2 (N choose 2), the select function can be rewritten as:
int select(int x){
return (x*(x-1))/2;
}

Merging two arrays in ascending order

I know the logic how to merge two arrays but the problem is how to code.
This was my code n it is giving correct ans but my sir told me that do it again,please tell me what I have to add in this code,
#include<iostream>
using namespace std;
int mergeArrays(int array1[],int size1,int array2[],int size2);
int main()
{
const int size1=8;
const int size=12;
int arrayA[size1]={10,25,37,49,50,51,55,60};
int arrayB[size]={2,5,26,27,29,32,40,45,70,80,90,95};
mergeArrays(arrayA,size1,arrayB,size);
}
int mergeArrays(int array1[],int size1,int array2[],int size2)
{
int size3=size1+size2;
int *array3=new int[size3];
int k=0;
for(int i=0;i<size1;i++)
{
array3[k]=array1[i];
cout<<" "<<array3[k];
}
int j=0;
for(int i=size1;i<size2;i++)
{
array3[k]=array2[j];
}
for(int i=size1;i<size2;i++)
{
for(int j=0;j<size2;j++)
{
array3[i]=array2[j];
cout<<" "<<array3[i];
}
cout<<endl;
delete[]array3;
return array3[k++];
}
}
I had searched this in many places but could not corrected my code
I had written this code but it is not giving correct ans.
#include<iostream>
using namespace std;
int merge(int *a,int *b,int aSize,int bSize);
int main()
{
const int aSize={8};
const int bSize={12};
int arrayA[aSize]={10,25,37,49,50,51,55,60};
int arrayB[bSize]={2,5,26,27,29,32,40,45,70,80,90,95};
merge(arrayA,arrayB,aSize,bSize);
return 0;
}
int merge(int *a,int *b,int aSize ,int bSize)
{
int cSize=aSize+bSize;
int *c=new int[cSize];
int j=0,k=0;
int i=0;
while(i<=aSize&&j<=bSize )
{
if(a[aSize ]<=b[bSize])
{
c[k]=a[aSize];
k++;
i++;
}
else
{
c[k]=b[bSize];
k++;
j++;
}
}
for(int i=0;i<k;i++)
{
cout<<c[i]<<endl;
}
delete[]c;
return c[k++];
}
your sir request you do Merging two arrays in ascending order. so i think you should return a new array, fill with array1 and array2's element, and the elements should be ascending order. here is a implement.(suppose your input arraies is already in ascending order.)
#include <iostream>
using namespace std;
int mergeArrays(int array1[],int size1,int array2[],int size2, int outArray[]);
int main()
{
const int size1=8;
const int size=12;
int arrayA[size1]={10,25,37,49,50,51,55,60};
int arrayB[size]={2,5,26,27,29,32,40,45,70,80,90,95};
int outArray[size1+size];
int len = mergeArrays(arrayA,size1,arrayB,size, outArray);
cout <<" "<< len;
for (int i = 0; i< size1+size; ++i){
cout <<" " << outArray[i];
}
}
int mergeArrays(int array1[], int size1, int array2[], int size2, int outArray[])
{
int i=0, j=0, k=0;
int retSize = size1+size2;
while (k<retSize){
if (i==size1){// only left array2, copy it
for (; j<size2; ++j){
outArray[k++] = array2[j];
}
}else if (j == size2) { // only left array1, copy it
for (; i<size1; ++i){
outArray[k++] = array1[i];
}
}
else if (array1[i] > array2[j]){ // copy the min value to outArray
outArray[k++] = array2[j++];
}else{
outArray[k++] = array1[i++];
}
}
return k;
}
now, let's look at your first code:
int mergeArrays(int array1[],int size1,int array2[],int size2)
{
int size3=size1+size2;
int *array3=new int[size3];
int k=0;
for(int i=0;i<size1;i++)
{
array3[k]=array1[i]; // k is not changed, so you just assign array1's each value to array3[0]
cout<<" "<<array3[k];
}
int j=0;
// what's the purpose of this loop?
// and in loop, you don't use i, you just repeat set array3[0] = array2[0]!!
for(int i=size1;i<size2;i++)
{
array3[k]=array2[j];
}
for(int i=size1;i<size2;i++) // if array2's length bigger than array1's, will enter this loop.
{
for(int j=0;j<size2;j++)
{
array3[i]=array2[j]; // this just repeat assign array2's each value to array3[i]!!
cout<<" "<<array3[i];
}
cout<<endl;
delete[]array3;
return array3[k++]; // you delete array3, but at here you access it!! this will crash!
// also, in this for i loop, you have return, so it will only execute once.
}
// reach function end and no return if not enter for loop.
}
I haven't looked at your second code. I think you still need to do more study.

Trying to implement heapify procedure , could not find error where I am wrong

I am using a simple approach to convert an array into heap in bottom up manner. I could not find the mistake. Can some one please have a look and let me know.
Basically I am making out of a random array in bottom up manner.
for(int i=n/2;i>=1;i--)
{
heapify(i,n,arr);
}
There is some issue with heapify procedure ,which sometimes give correct result and most of times wrong.
`#include<iostream>
#include<conio.h>
using namespace std;
int min(i
nt x,int y,int z)
{
return ((x>y?x:y)>z?(x>y?x:y):z);
}
int left(int i)
{
return (2*i);enter code here
}
int right(int i)
{
return (2*i+1);
}
int parent(int i)
{
return (i/2);
}
int heapify(int i,int n,int arr[])
{
int temp;
int l=left(i);
int r=right(i);
temp=i;
if(l<=n&&arr[l]<arr[i])
temp=l;
else if(r<=n&&arr[temp]>arr[r])
temp=r;
if(temp!=i)
{
int x=arr[temp];
arr[temp]=arr[i];
arr[i]=x;
heapify(temp,n,arr);
}
//else return 0;
}
int deletemin(int n,int arr[])
{
cout<<arr[1]<<"\ndeLeted\n";
arr[1]=arr[n];
}
int insert(int x,int size,int arr[])
{
arr[size]=x;
int i=(size);
do
{
i=parent(i);
if(arr[i]!=min(arr[i],arr[left(i)],arr[right(i)]))
heapify(i,(size),arr);
else
return 0;
}while(i!=1);
}
int main()
{
int n,t,arr[100];
cin>>t;
while(t--)
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>arr[i];
for(int i=n/2;i>=1;i--)
{
heapify(i,n,arr);
}
for(int i=1;i<=n;i++)
cout<<arr[i]<<" ";
deletemin(n,arr);
n--;
heapify(1,n,arr);
for(int i=1;i<=n;i++)
cout<<arr[i]<<" ";
int y;
cout<<"what value do you want to insert\n";
cin>>y;
n++;
insert(y,n,arr);
for(int i=1;i<=n;i++)
cout<<arr[i]<<" ";
}
getch();
return 0;
}
`
One simple observation is that your min function is wrong! It doesn't return the min of 3 numbers. Do it with ifs for the moment!