error in compilation in codeforces [closed] - c++

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.

Related

I get a warning when I try to run the recursion program with return keyword [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I was expecting 1 2 3 as output, but when I try to run this code:
#include <iostream>
using namespace std;
int fun(int x){
if (x>0){
return fun(x-1);
cout<<x<<endl;
}
}
int main()
{
int x=3;
fun(x);
return 0;
}
I get this warning:
warning: control reaches end of non-void function
Why doesn't it return the value and call fun(x-1)?
But the below code works perfectly. I get 3 2 1 as output.
#include <iostream>
using namespace std;
int fun(int x){
if (x>0){
cout<<x<<endl;
return fun(x-1);
}
}
int main()
{
int x=3;
fun(x);
return 0;
}
Once a function has return'ed, it can't execute any more code:
if (x>0){
return fun(x-1);
cout<<x<<endl; // <-- NEVER EXECUTED
}
The warning is because your function has a non-void return type, but is not return'ing any value when x is <= 0, thus causing undefined behavior.
Try this instead:
#include <iostream>
using namespace std;
int fun(int x){
if (x>0){
int ret = fun(x-1);
cout << x << endl;
return ret;
}
return 0;
}
int main()
{
fun(3);
return 0;
}
Online Demo

wrong output on printing subsets of array [closed]

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);
}

Codeforces question (unable to detect problem in my code) [closed]

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;
}

What is wrong with my implementation of quicksort? [closed]

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;
}

Postfix notation stack C++ [closed]

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
I am new to C++ and I want to use a stack to evaluate an expression given as an input (2+3*5+4 for example), containing only numbers, + and *. I wrote this code but it gives me Segmentation fault: 11. Could you please help me solve this or give me a hint about what could be wrong? Thank you! (I noticed there were similar questions on this site, but I couldn't use them to solve my problem)
#include <iostream>
#include <stack>
using namespace std;
bool highPrecedence(char a, char b){
if((a=='+')&&(b=='*'))
return true;
return false;
}
int main()
{
char c = 'a';
double x;
stack<char> stack;
double v[10];
int i=0;
double res;
while(true)
{
c = cin.get();
if(c=='\n'){
while(stack.size()!=0){
if (stack.top()=='*'){
double res = v[i]*v[i-1];
i--;
v[i]=res;
stack.pop();
}
if (stack.top()=='+'){
res = v[i]+v[i-1];
i--;
v[i]=res;
stack.pop();
}
}
break;
}
if ( '0'<=c && c<='9' )
{
cin.putback(c);
cin>>x;
cout<<"Operand "<<x<<endl;
i=i+1;
v[i]=x;
}
else
{
if(c!=' ') cout<< "Operator " <<c<<endl;
if (stack.size()==0)
stack.push(c);
else{
while((!highPrecedence(stack.top(),c)) && (stack.size()!=0)){
if (stack.top()=='*'){
double res = v[i]*v[i-1];
i--;
v[i]=res;
stack.pop();
}
if (stack.top()=='+'){
res = v[i]+v[i-1];
i--;
v[i]=res;
stack.pop();
}
}
stack.push(c);
}
}
}
cout<<v[0]<<endl;
}
Using stack.top() is illegal if the stack is empty.
Change while((!highPrecedence(stack.top(),c)) && (stack.size()!=0)){
to while((!stack.empty()) && (!highPrecedence(stack.top(),c))){
The initiali value of i is not good and you are printing uninitialized variable, which has indeterminate value.
Change int i=0; to int i=-1;