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 4 years ago.
Improve this question
I have this code
what is wrong in my code
it must display how many distinct elements are there
string name;
long long maps;
int i,j,test,l,counts=0,k;
cin>>test;
for(i=0;i<test;i++){
counts=maps=0;
cin>>name;
l = name.length();
for(j=0;j<l;j++){
k=(toupper(name[j])-'A');
cout<<endl<<(maps&(1<<k))<<" "<<k;
if(0 == maps&(1<<k)){
counts++;
maps|=(1<<k);
}else{
cout<<" "<<(int)(maps&(1<<k))==0;
}
}
cout<<counts;
}
what is wrong check witht the output i cant find why i is not working
The problem is here:
if(0 == maps&(1<<k)){
The equality operator == has higher precedence than the bitwise-AND operator &. So the above evaluates to this:
if ((0 == maps) & (1 << k )) {
You need to add parenthesis to get the desired behavior:
if (0 == (maps & (1 << k))) {
You'll need to do the same in your cout call:
cout<<" "<<(int)((maps&(1<<k))==0);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I dont really understand i have written return in the end but still it gives error
CODE :
int factorial(int num)
{
int N;
if (num > 1)
{
N = (num * factorial(num--));
}
else
return N;
}
int main()
{
cout << factorial(5);
return 0;
}
ERROR : warning: control reaches end of non-void function [-Wreturn-type]
16 | }
Your issue is that you don't return anything. If you look at the flow of the program you can see that for num > 1 you do the factorial stuff and for num <= 1 you just return N. For num > 1 the return statement is never reached. This issue can be fixed by removing the else, BUT that leaves an other issue mentioned, namely that for num <= 1 N is never initialised. If you initialise it to 1 that should solve that, but as people pointed out you don't need N, you can do return num * factorial(num - 1); and simply return 1 for num <= 1. The final problem with your code is that you do num * factorial(num--). factorial(num--) will call factorial(num), when you would need factorial(num-1), because num-- is the post-decrement operator.
Other suggestions in the comments are good to heed as well, like implementing guards from integer overflow and the like.
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
cin >> a >> b >> n;
int ans=0;
c=max(a,b);
d=min(a,b);
while(n>c)
if(d+c>n) {
ans++;
break;
}
cout << ans;
}
why if I insert 1,2,2 as input the result will be 0 instead of one
If you had a debugger that you could step through the code with, the mistake would have been easy to find.
When you get to the while loop, a = 1, b = 2, n = 2, c = 2, d = 1 and ans = 0.
Since the condition n > c is false (because !(2 > 2)) the body does not get executed and you get what you started with.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
program:
what else can I use in this program instead of using multiple if else statement
my desire number is until a thousand.
do {
System.out.print("Enter number: ");
a=Integer.parseInt(br.readLine());
if(a==1)
System.out.println(o[0]);
if(a==2)
System.out.println(o[1]);
if(a==3)
System.out.println(o[2]);
if(a==4)
System.out.println(o[3]);
if(a==5)
System.out.println(o[4]);
if(a==6)
System.out.println(o[5]);
}
while(a!=0);
if (a==0){
System.out.println("You entered number zero");
}
do {
System.out.print("Enter number: ");
a = Integer.parseInt(br.readLine());
if (a != 0 && a < o.length) {
System.out.println(o[a-1]);
}
} while(a != 0);
if (a == 0) {
System.out.println("You entered number zero");
}
Start by analyzing the pattern: given if(a==X) System.out.println(o[Y]);, what is the relation of X (and therefor a) to Y?
Now eliminate all the if-else statements to take this into account - the program should be left with a single System.out.println(o[..]); line (where the expression .. transforms a by the pattern identified above).
Then, what is the domain of X (and a by extension)? That is, for what values of X should this println operation occur?
Add an if statement around the above println; this will prevent output when the user enters 0 (or 7), for instance.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
int rVals[];
string rNum;
for (i=0; i < rNum.length(); ++i) {
if((rVals[i] < rVals[i+1]) && (rNum[i] =='C' || rNum[i]=='X' || rNum[i]=='I')){
continue; //checks to see if preceeding value is < the next value
} else {
valid = false;
cout << "you can't subtract by M, D, L, or V\n" << endl;
break;
}
}
rVals[] is a dynamic array and is set correctly. No matter what the input is the if statement seems to evaluate to false. what is wrong with the if statement?
Take a look at this: rVals[i] < rVals[i+1]. If rVals length is 10 for instance and i is 9 rVals[i+1] will "point" to the 11th element of the array (since the indexing of an array is starting from 0 and between 0 and 9 you heave 10 elements - the size of our array).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to get the binary representation of a decimal number. I have looked all over the internet but could not find anything useful.
Can anyone provide me with sample code?
Note that I want it to run on both 32-bit and 64-bit architecture.
Thanks!
Just isolate the bits one by one, starting from the highest, and print the appropriate character:
#include <limits.h>
#include <stdio.h>
void print_binary(unsigned x)
{
for (int i = sizeof(x) * CHAR_BIT; i--; )
{
putchar('0' + ((x >> i) & 1));
}
}
int main()
{
print_binary(123);
}
If you want the result in a string instead of printed to the console, I'm sure you can adjust the code.
Get the bits from the bottom up.
Then reverse the string when done.
string bits(long n)
{
string tmp ;
while ( n ) { tmp << ( n & 1 ) ? "1" : "0" ; n >>= 1 ; }
tmp= reverse( tmp) ;
return tmp ;
}
Try William Clinger's paper "How to read floating point numbers accurately".