Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am trying to print all the subsets of an array. But not getting the correct output.
#include<iostream>
using namespace std;
int arr[]={1,2,3,4,5};
int n=5;
void print(int a[],int cnt, int idx)
{
if(idx==n)
{
for(int i=0;i<cnt;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
return;
}
a[cnt]=arr[idx];
print(a,cnt,idx+1);
print(a,cnt+1,idx+1);
}
int main()
{
int a[5]={0};
print(a,0,0);
}
the above code only prints "5"
Please help me to rectify the same.
Replace the array with a vector. As the array in C++ is passed by reference, the print function is accessing the same array, which causes the problem.
#include <iostream>
#include<vector>
using namespace std;
int arr[]={1,2,3,4,5};
int n=5;
void print(vector<int>a,int cnt, int idx)
{
if(idx==n)
{
for(int i=0;i<cnt;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
return;
}
a[cnt]=arr[idx];
print(a,cnt,idx+1);
print(a,cnt+1,idx+1);
}
int main()
{
vector<int >a(5,0);
print(a,0,0);
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last year.
Improve this question
#include<conio.h>
#include<stdio.h>
#include<iostream>
using namespace std;
class binary_search
{
public:
int a[10],flag;
int n,i,j,index,num,temp,mid,low,high;
void getdata();
void search();
void sort_array();
};
void binary_search::sort_array()
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[j]>a[j+1])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
}
void binary_search::getdata()
{
cout<<"number of array "<<"\n";
cin>>n;
cout<<"\nEnter array : "<<"\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
sort_array();
cout<<"\nSorted Array Elements: ";
for(i=0;i<n;i++)
{
cout<<a[i];
}
}
void binary_search::search()
{
cout<<"\nEnter value to search: ";
cin>>num;
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==num)
{
cout<<"\nNumber is found at position "<<mid;
break;
}
else if(a[mid]>num)
{
high=mid-1;
}
else if(a[mid]<num)
{
low=mid +1;
}
else if(a[mid]!=num)
{
flag=false;
}
}
if(!flag)
{
cout<<"\nNumber is not found!!!";
}
}
int main()
{
binary_search b;
b.getdata();
b.search();
getch();
return 0;
}
Program is running but the expected output is not showing.
I'm not getting the message of "not found the number". I think I am missing something if any guidance I will get it will be awesome. Someone refer me to remove conion.h and getch() ; but still the output is not showing as expected.
Actually it's a code for binary search in C++
You must be facing problem in the test cases where, number of elements in array is exactly equal to 10. Because, if you look closely, the sort_array() that you have made have some logical error.
This is your sort_array() function which is using bubble sort technique.
void binary_search::sort_array()
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[j]>a[j+1])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
}
But, consider the case if n = 10, at this point int the inner loop code will try to access a[10], which is not present. So, it will go out of bound which will lead to undesirable results. So, try changing your function to the one below.
void binary_search::sort_array()
{
for(int i=0;i<n - 1;i++)
{
for(int j=0;j<n - i - 1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
}
Apart from this remove #include<conio.h> and getch()
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Over the past 2 days i was practising on codeforces . I am new to programming and currently doing implementation problems.
This question is 1337B ( KANA AND DRAGON QUEST)
https://codeforces.com/problemset/problem/1337/B
I cannot figure out my mistake in this code. Please help me out
#include <iostream>
#include <string>
using namespace std;
int main()
{
int t;
cin>>t;
for (int i=0;i<t;i++)
{
long long int x,n,m;
cin>>x>>n>>m;
for( int j=0;j<n&&x>0&&x>20;j++)
{
x=x/2+10 ;
}
x=x-m*10;
if (x>>0)
{
cout<<"NO"<<endl;
}
else
{
cout<<"YES"<<endl;
}
}
return 0;
}
Your code is correct but you made a small error at the end. You do x>>0 instead of x > 0. ">>" is a bitwise shift operator and not a greater than operator. After checking with codeforces test input it does seem to work. Here is the version with no error
if (x>0)
{
cout<<"NO"<<endl;
}
else
{
cout<<"YES"<<endl;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
The program below is for sorting a list using quicksort C++.The code typed below has compiled successfully in code::blocks and in http://cpp.sh/, but unfortunately it hangs after entering in the elements,any help will be appreciated..
void quicksort(vector<int> v,int left_index,int right_index)
{
if(left_index>=right_index)
return;
int pivot=(right_index+left_index)/2;
int left=left_index;
int right=right_index;
while(left<=right)
{
while(v[left]<v[pivot])
left++;
while(v[right]>v[pivot])
right--;
if(left<=right)
{
swap(v,left,right);
left++;right--;
}
}
quicksort(v,left_index,right);
quicksort(v,left,right_index);
}
Passing by reference is must as others have pointed out.
Keep pivot constant during a partition. pivot = v[pivot] ensures that.
outer loop bounds changed to left<=right from left<right.
The running code.
#include <iostream>
#include<vector>
using namespace std;
void print(const vector<int> &v)
{
cout<<"The sorted list is:"<<endl;
for(int i=0;i<(int)v.size();i++)
cout<<v[i]<<' ';
cout<<endl;
}
void swap(vector<int> &v,int left,int right)
{
int temp=v[left];
v[left]=v[right];
v[right]=temp;
}
void quicksort(vector<int> &v,int left_index,int right_index)
{
if(left_index>=right_index)
return;
int pivot=(right_index+left_index)/2;
pivot = v[pivot];
int left=left_index;
int right=right_index;
while(left<right)
{
while(v[left]<=pivot)
left++;
while(v[right]>pivot)
right--;
if(left<right){
swap(v,left,right);
left++;
right--;
}
}
quicksort(v,left_index,right);
quicksort(v,left,right_index);
print(v);
}
int main()
{
int no;
vector<int> v;
cout << "Please enter the elements in your list" << endl;
cout << "Enter 0 to exit..."<<endl;
while(cin >> no)
{
if(no==0)
break;
v.push_back(no);
}
quicksort(v,0,v.size()-1);
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
can anyone help me in this
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
#define ll long long int
struct points{
ll a;
ll b;
};
int cos(points x , points y)
{
return x.b<y.b;
}
int main()
{
ll n,r,avg,i,j,k;
points pt[100005];
cin>>n>>r>>avg;
for(i=0;i<n;i++)
{
cin>>pt[i].a>>pt[i].b;
}
sort(pt,pt+n,cos);
ll sum=avg*n;
for(i=0;i<n;i++)sum-=pt[i].a;
if(sum<=0)cout<<"0\n";
else
{
ll ans=0;
for(i=0;i<n;i++)
{
if(sum==0)break;
else
{
if(sum>r-pt[i].a)
{
ans=ans+pt[i].b*(r-pt[i].a);
sum=sum-(r-pt[i].a);
}
else
{
ans=ans+sum*pt[i].b;
sum-=sum;
}
}
}
cout<<ans<<endl;
}
return 0;
}
when im compiling in my system its working fine and getting the correct output but when i'm submitting in codeforces under GNUC++ 11 im getting compllation error? can u help me
Your compare function, cos(), collides with function in standard <cmath>. Rename it and it will compile.
This question is a follow-up to a question I asked earlier on Stack Overflow:
bsort example from programming pearls
I want to show you my work and have some questions about it.
#include<string.h>
#include<iostream>
using namespace std;
void swap(char *x[],int i,int j)
{
char *t=x[i];
x[i]=x[j];
x[j]=t;
}
int get_bit(char *s,int depth)
{
int required_bit=13;
int bit=required_bit&0x7;
int byte=required_bit>>3;
int val=(s[byte]>>bit)&0x1;
return val;
}
void bsort(char *x[],int l,int u,int depth)
{
if(l>=u) return ;
for(int i=l;i<=u;i++){
if(strlen(x[i])<depth){
swap(x,i,l++);
}
}
int m=l;
for(int i=l;i<u;i++)
{
if(get_bit(x[i],depth)==0)
{
swap(x,i,m++);
}
}
bsort(x,l,m-1,depth+1);
bsort(x,m,u,depth+1);
}
int main()
{
char *x[6]={"car","bus","snow","earth","dog","mouse"};
bsort(x,0,5,1);
for(int i=0;i<6;i++)
cout<<x[i]<<" "<<endl;
return 0;
}
The code above works, compiles and shows me a result. But I am interested to know whether it sorts lexicographically or otherwise? The result is this:
car
bus
dog
snow
earth
mouse
First b is before c, so are car and bus on the right place? Please tell me what is wrong with my code.