This question already has answers here:
Value of C define changes unexpectedly
(7 answers)
C++ int with preceding 0 changes entire value
(4 answers)
Closed 1 year ago.
Here I am trying to convert h = 010 to h = 001 (via right shift operator) and then using the 'and' operator with 1, I wanna print yes, but I don't know why it is printing NO.
int h = 010;
h = h>>1;
if(h & 1){
cout<< "YES";
}
else{
cout << "NO";
}
Related
This question already has answers here:
Why does a C-Array have a wrong sizeof() value when it's passed to a function? [duplicate]
(6 answers)
Passing Arrays to Function in C++
(5 answers)
Closed 4 months ago.
The community reviewed whether to reopen this question 4 months ago and left it closed:
Original close reason(s) were not resolved
I am trying to recursively count down from an array. I have done this previously in python, but I am having trouble finding the size of array. My sizeof() function is only returning a length of 2, which is odd since the array size should be 20 bytes/4bytes.
string cdown(int param[])
{
int length = sizeof(param)/sizeof(param[0]);
cout<<"length: "<< length << endl;
string blastoff = "blastoff";
for (int i=0;i<length;i++)
{
if (param[i]==0){return blastoff;}
else
{
cout<<param[i]<< " ";
param[i] = param[i+1];
param[length-1] = 0;
cdown(param);
}
}
}
int main(void)
{
int countdown[] = {5,4,3,2,1,};
cdown(countdown);
return 0;
}
Result
length: 2
5
length: 2
4
length: 2
This question already has answers here:
vector.size() - 2 leads to infinite loop - C++ [duplicate]
(2 answers)
How do unsigned integers work
(3 answers)
How compare vector size with an integer? [closed]
(3 answers)
Closed 9 months ago.
The value for i keeps decreasing forever. Technically it should stop after first iteration because ans.size()-k = 0, but it doesn't stop. If I put 0 manually over there then it works completely fine.
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> ans(1);
int k = 1;
for (int i = ans.size() - 1; i >= ans.size() - k; --i)
{
cout << i << endl;
}
}
This question already has answers here:
Why is my power operator (^) not working?
(11 answers)
What is the C++ function to raise a number to a power?
(17 answers)
Closed 3 years ago.
Print a sequence of n^n for 1 to 15. This code only prints out 0. What am I doing wrong?
int main()
{
int i, n;
i = 0;
n=1;
do {
n = n^n;
cout << n;
n++;
i++;
}while(i<15);
return 0;
This question already has answers here:
Is short-circuiting logical operators mandated? And evaluation order?
(7 answers)
Closed 6 years ago.
How is the value of b unchanged?
#include <iostream>
int main()
{
int a = 5, b = 10;
if (++a || ++b)
std::cout << a << b;
system("PAUSE");
return 0;
}
The output is 610. But how?
here's how the 'if' statement works:
if(condition1 || condition2 || condition 3){
//do this
}
now if condition1 is true (which in your code, it is since a!=0), the execution straightaway moves inside the block without checking 2 and 3.
If you wish to increment b as well, try && in place of ||
This question already has answers here:
How to convert a single char into an int [duplicate]
(11 answers)
Closed 7 years ago.
This code Find the sum of all digits that occur in a string.
Example
sumUpNumbers("2 apples, 12 oranges") = 5 //2+1+2
Can anyone explain the need for use int('0') in this code!?
int sumUpDigits(std::string inputString) {
int answer = 0;
for (int i = 0; i < inputString.size(); i++) {
if ('1' <= inputString[i] && inputString[i] <= '9') {
answer += int(inputString[i]) - int('0');
}
}
return answer;
}
It converts char into ASCII code to make number out of string
int('9') - int('0') = 9