Why is the value of "C" changing in the following code? - c++

the output I'm getting for the given code is "0" even though I
initialized the value of c as "1"...can somebody explain it...
Why is the value of "C" changing in the following code??
#include <iostream>
using namespace std;
int c=1; // global initialized 'c' as 1..
long long f(long long n){
if(n==6){
return 2;
}
else{
c=c+1;
f(n-2);
}
}
int main()
{
long long n,ans,p;
cin>>n;
ans=f((2*n)-2);
cout<<c; //printing out the value of 'c'
return 0;
}

Because value of c is being changed in below code :
else{
c=c+1;
f(n-2);
}

Related

C++: Error in login, identify a valid triangle with greater greater then 0, all three angles provided

I have written this program, it passes all manual test conditions but says "wrong answer" when I submit online on an IDE.
Constraints
0≤a,b,c≤180
#include <iostream>
using namespace std;
int main() {
// your code goes here
double a,b,c;
cin>>a>>b>>c;
if(a+b+c==180)
cout<<"YES";
else
cout<<"NO";
return 0;
}
The above code doesn't give correct answer when either of a,bc is zero and the a+b+c=180.
So,
int main()
{
// your code goes here
double a,b,c;
cin>>a>>b>>c;
//add the below statement
if((a!=0)&&(b!=0)&&(c!=0)){
if(a+b+c==180)
cout<<"YES";
else
cout<<"NO";
}
else{
cout<<"NO";
}
return 0;
}
int main()
{
// your code goes here
double a,b,c;
cin>>a>>b>>c;
//made the changes with the help of suggestion from the forum
if((a>0)&&(b>0)&&(c>0)&&(a+b+c==180))
cout<<"YES";
else
cout<<"NO";
return 0;
}

why doesnt the bool function return false?

I'm soo sorry I searched for and read similar questions but couldn't understand/use them to solve my own.
Im writing a bool function within an if statement but the function doesn't seem to return false, what am I doing wrong.
My bool function just checks if there are more than one of the given number in an array:
bool findsame(int a[], int b){
int k=0;
for(int i=0;i<20;i++){
if(a[i]==b){
k++;
}
}
if(k>1){
return true;
}
else{
return false;
}
}
int main()
{
const int size=20;
int a[size]={4,4};
int b=4;
if(findsame(a,b)){
cout<<"true";
}
}
I think you got confused why "false" is not getting printed on console with the function returning the false value.
You need to add to an extra else statement to print false on the console:
if(findsame(a,b)){
std::cout<<"true";
}else{
std::cout<<"false";
}
Also, there are two 4 values in the array, therefore always true will get printed.
Try passing value of b other than 4 and 0.
Have a look at the following implementation where value of variable b is equal to 1:
#include<iostream>
bool findsame(int a[], int b){
int k=0;
for(int i=0;i<20;i++){
if(a[i]==b){
k++;
}
}
if(k>1){
return true;
}
else{
return false;
}
}
int main()
{
const int size=20;
int a[size]={4,4};
int b=1;
if(findsame(a,b)){
std::cout<<"true";
}else{
std::cout<<"false";
}
}
Output:
false
PS: I have also tested code for the value of b = 4 and it prints true. Check and Run the code here: https://onlinegdb.com/S1LR5PtvD

replace all negative value from array using recursion C

I want replace all negative value by zero(recursively). And I have use C and recursion. It's was my homework.
Desired output:
0 4 0 3
What I get:
0 4 -9 3
My code:
#include <stdio.h>
int zeros_value(int n, int tab[])
{
if (n==0) return 0;
if(tab[n-1] < 0){
tab[n-1]=0;
}
else{
return zero_value(n-1,tab);
}
}
int main(void)
{
int tab[4] = {0,4,-9,3};
int number = 0;
int i;
zero_value(4, tab);
for(i=0;i<4;i++)
printf("%d ", tab[i]);
return 0;
}
When you hit the first negative, the recursion doesn't continue anymore and the function returns. You don't really need to return any value from the function. You can rewrite it to make a void function.
#include <stdio.h>
void zero_value(int n, int tab[])
{
if (n==0) return;
if(tab[n-1] < 0) tab[n-1]=0;
zero_value(n-1,tab);
}
int main(void)
{
int tab[4] = {0,4,-9,3};
int number = 0;
int i;
zero_value(4, tab);
for(i=0;i<4;i++)
printf("%d ", tab[i]);
return 0;
}
I see the following problems with your code.
The function zero_values does not have a valid return statement when tab[n-1] is negative. You can see it more clearly if you change the function to:
int zeros_value(int n, int tab[])
{
if (n==0)
{
return 0;
}
if(tab[n-1] < 0)
{
tab[n-1]=0;
// No return here.
}
else
{
return zero_value(n-1,tab);
}
// No return here either.
}
Calling such functions leads undefined behavior.
The printf line in main is not right.
printf("%d%d%d%d", zeros_value(4,tab));
That line needs four arguments of type int after the format string to work correctly. Not providing enough arguments to printf is also cause for undefined behavior.
You can use solution provided in the answer by #usr to solve both problems.
If you have any valid reasons to return an int from zero_value, you need to change the implementation appropriately. It's not clear from your post what that return value is supposed to be.

Input not terminating because of a comment

This cpp code is not terminating. I have tried the code by various inputs but this is not terminating.I think there is a bug in the 52th line, when I comment the 52 line than the code is working fine.
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
// binary search for larger elements
using namespace std;
vector <int > q;
// bsearch value uses hte
#define bvector q // just define these values to use them in your functin
#define VALUE(x) bvector[x]
int b_search(int value){
int low=0,high=bvector.size(),mid;
mid=(low+high)/2;
cout << "In the bsearch";
while(low<high)
{
if(VALUE(mid)==value)
return mid;
else if(VALUE(mid)>value)//
high=mid;
else if(VALUE(mid)<value)
low=mid+1;
}
if(VALUE(low)>value)
return low;
return -1;
}
int main(){
int i;
for(scanf("%d",&i);i;scanf("%d",&i))
q.push_back(i); // this is for taking input in the vectot
sort(q.begin(),q.end());
for(i=0;i<q.size();i++)
printf("%d ",q[i]);// for printing the sorted
int j;
printf("Enter the elements you want to search");
int x;
scanf("%d",&x);
// BUG is present in this lines
cout <<"This is the end of scanf";// if this line is commented then the 54th line is not reached
j=b_search(x);
printf("%d ",j);
return 0;
}
The statement : mid=(low+high)/2; is a bit misplaced. It should be inside the while loop.
This is probably what is causing an infinite loop.

Curious behaviour of bitwise AND

I was coding and the following code doesn't give the desired output. pos&1 is supposed to return the remainder when pos is divided by 2. When I replace pos&1 by pos%2 everything works just fine. What could be the problem?
#include <iostream>
using namespace std;
int main(){
int y;
unsigned long long int pos;
cin>>y;
cin>>pos;
int f=0;
while(y>0){
y--;
if(pos&1==0){
f=1-f;
}
pos=pos/2;
}
if(f==1){
cout<<"blue\n";
}
else
cout<<"red\n";
return 0;
}
1==0 takes more precedence than pos&1. Try if((pos&1)==0){