for loops instead of if else statement [closed] - if-statement

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.

Related

Number of distinct elements in array using bits in int [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 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);

Can someone explain please what does this do : n&1? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Here is the code, I know what does it do , but I don't understand, what does the if condition do?
if(n&1)
{
for(i=n/2,j=n/2+1;;i--,j++)
if(__gcd(i,j)==1)
{
cout<<i<<" "<<j<<endl;
break;
}
}
else
{
for(i=n/2-1,j=n/2+1;;i--,j++)
if(__gcd(i,j)==1)
{
cout<<i<<" "<<j<<endl;
break;
}
}
return 0;
}
Its a bitwise operator. There's a search term for you!
The & operator provides a mask that "cancels out" bits in the first depending if they're set in the second parameter - so assume N is the number 17, that expressed in binary is 00010001, the number 1 in binary is 00000001, so masking the two together will "blank" the first set of bits, leaving you with N as 00000001.
Basically that particular if statement drops all except the last bit, which is either 0 or 1, so it is a condition detecting if N is odd or even.

Mathematic equation solved recursively in C++ [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 6 years ago.
Improve this question
I have a task to make a code which will write 100 first numbers of an equation (or a function, I don't know what this is)
A(n) = (A(n-1))^2 -n*A(n-2) where A(1) = 1 and A(2) = 1
It has to be solved recursively. I have written this code so far
#include <iostream>
using namespace std;
int rekurzija(int n){
if(n=1){
return 1;
}
if(n=2){
return 1;
}
if(n>2){
return rekurzija(n-1)*rekurzija(n-1)-n*rekurzija(n-2);
}
}
int main(){
for(int n=1;n<101;n=n+1){
cout << rekurzija(n) << endl;
}
}
The problem is that the program returns 1 hundred times instead of 1,1,-2,0,...(instead of actually solving this function). What is wrong in this code?
You are using simple assignment operator = instead of Is equals to relational operator == in your rekurzija() function for if conditions
if(n = 1) //here `n = 1`is an assignment statement
{
//something...
}
What happens if you use = instead of ==?
The if condition will always evaluate to be true if the assigned value in the assignment statement is non-zero number.
Note: An assignment to zero evaluates to be false i.e, for if(n = 0), the if block will not be entered. You don't have any such if blocks in your code.
So your first if is always evaluated to be true because you are assigning a non-zero value i.e, 1 and thus your function always returns 1. that's the reason why you get 100 1's as your answer.
So, instead try changing all the if conditions to something like:
if(n == 1)
{
//something...
}
This would check if n is equals to 1 or not. If n is equal to 1 then the if block is entered, else it would not enter the if block and the next if condition is checked.
Note: Just remember this while using the = and == operators
= is for assignment
== is for comparison
When you compare things in C++ you need to do it like:
if (a == b)
and not
if (a = b)
The latter will assign b to a and return the value of a.

if and else if statements in C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
What is the difference between this:
s = 0;
if (x > 0) s++;
if (y > 0) s++;
and this:
s = 0;
if (x > 0) s++;
else if (y > 0) s++;
Any help would be greatly appreciated.
When you write else if instead of if, program will not check the else if statement if x > 0, but when you write two if statements program will check both conditions, no matter if x > 0 or not.
In the first case the both conditions are checked because there are two different if statements.
In the second case the second condition is checked only if the first condition is evaluated to false.
Say x is 10 and y is 10. At the end of the first set of statements, s will be equal to 2. At the end of the second set of statements, s will be equal to 1.
The second example
s = 0;
if (x > 0) s++;
else if (y > 0) s++;`
will check for the y value only if x > 0 is false. The first example will execute the check regardless of x's value.

logic or syntax error? C++ [closed]

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).