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
I require some help in forming an if else code to show a message. For example, if it is more than or equal to 50, a message "Green Mark Certified" will be showed in textView box. Below are the codes that I did for a simple calculator. What I need is when int sum>=50 and <75, show "Green Mark Certified" in a textView box. Please kindly show me how to code if I want to achieve it.
public void onButtonClick(View V){
EditText e1 = (EditText)findViewById(R.id.editText);
EditText e2 = (EditText)findViewById(R.id.editText2);
EditText e3 = (EditText)findViewById(R.id.editText3);
EditText e4 = (EditText)findViewById(R.id.editText4);
EditText e5 = (EditText)findViewById(R.id.editText5);
TextView t1 = (TextView)findViewById(R.id.textView);
int num1 = Integer.parseInt(e1.getText().toString());
int num2 = Integer.parseInt(e2.getText().toString());
int num3 = Integer.parseInt(e3.getText().toString());
int num4 = Integer.parseInt(e4.getText().toString());
int num5 = Integer.parseInt(e5.getText().toString());
int sum = num1 + num2 + num3 + num4 + num5 ;
t1.setText(Integer.toString(sum));
This is how the if statement should look like:
if(sum >= 50 && sum < 75) {
t1.setText("Green Mark Certified");
}
The conditions are placed in parentheses after if, and the code to be executed is placed in brackets. && means "and", and || (which is not used) means "or".
You can use the logical && operator to check both conditions:
if (sum >= 50 && sum < 75) {
t1.setText("Green Mark Certified");
}
Related
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.
Closed 3 years ago.
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.
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.
Improve this question
int x = 5, y = 10;
if(x > 0)
y = y + 100;
else;
y = y + 200;
cout << "y = " << y << endl;
The output: 310
I know that whenever there is a semicolon after else it is treated as there is nothing. I also know that if there were no curl braces then we only execute the first statement after if.
Then why the answer here appears as the second statement was executed?
Also, if the else was not there will the second be executed also and why?
After an if and after an else there needs to be exactly one statement. The statement after if will be executed if the condition is true, the statement after else will be executed if the condition is false.
Now the important thing to understand is the following:
A single semicolon ; is a statement. It's an empty statement and putting it behind if or behind else fullfils the requirements of a statement.
A block between curly brackets {} is a statement. It groups multiple statements together, but it is one statement and thus also fulfills the requirements.
Any other statement that comes after the first statement after if or else has nothing to do with the if or else and will be executed like any other code would.
So to break down your example:
if(x > 0)
y = y + 100;
else;
y = y + 200;
is equivalent to:
if(x > 0)
y = y + 100; // first statement after if
else
; // first statement after else
y = y + 200; // separate statement uneffected by if or else
Removing the else gives us this:
if(x > 0)
y = y + 100; // first statement after if
y = y + 200; // separate statement uneffected by if
and of course the statement y = y + 200; will be executed, because it has nothing to do with the if or the else.
In this case, both statements will be executed.
if(x>0)
is true so the first part will work
y = 110
after this, then we reach else and that is the end of the line the next line does not have anything to do with the if-else block, it will always execute. hence
y = 110 + 200
, so y will have a final value of 310.
Hope this helps, cheers.
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 5 years ago.
Improve this question
I have created a C++ which compiles successfully, but when I run it I get c=0.
Can anybody explain why?
int main()
{
double U0, U, C, A, B, D;
U = 0.2;
A = U/U0;
B = 1+1/(16*pow(A, 2));
D = pow(B, 2)-(1/4)*A;
for (U0=0.2; U0<=1; U0=U0+0.2)
{
if (U <= (4*U0))
{
C= (1/2)*(B+sqrt(D));
cout <<" | U0 | "<< U0 <<" | U | "<< U <<" | C^2 | "<< C << endl;
U = U + 0.2;
}
}
return 0;
}
Because of these kind of statements:
C= (1/2)*(B+sqrt(D));
C++ interprets 1/2 as an integer operation (not a floating one), hence 1/2 = 0 (for integers)
This is an error everybody has done once in his life!
After you will always write something like 1/2. with the dot to force a division using the double type.
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 5 years ago.
Improve this question
Chef has a calculator which has two screens and two buttons. Initially, each screen shows the number zero. Pressing the first button increments the number on the first screen by 1, and each click of the first button consumes 1 unit of energy.
Pressing the second button increases the number on the second screen by the number which is currently appearing on the first screen. Each click of the second button consumes B units of energy.
Initially the calculator has N units of energy.
Now chef wonders what the maximum possible number is, that he gets on the second screen of the calculator, with the limited energy.
Here is the link: https://www.codechef.com/JULY17/problems/CALC/
Contest is ended, so I am not trying to cheat.
here is my solution to the problem:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n,b;
cin>>n>>b;
int count = 1;
int ans = n-b;
while((n - count*b)>=0)
{
if(count*(n - count*b)>ans)
ans = count*(n - count*b);
count++;
}
cout<<ans<<endl;
}
return 0;
}
I have tried every test case that i can think of... Can anyone help to find the error in my logic.
The errors that I think can be:
1: You didn't handle N<=B case
if (n<=b) {
ans = 0;
}
2: you didn't handle Subtask 2 Constraints
long long int ans = n-b;
3: lastly answer will be n-b if b is three time less than equal to n
if (n<=3*b) {
ans = n-b;
}
4: Look for a straight forward approach
k1 = ((n-(b+1))/(2*b))+1;
i2 = (double((n-(3*b)))/(2*b));
i1 = ceil(i2);
i = n-((3*b)+((i1-1)*(2*b)));
ans = ((2*b)*((k1*(k1-1))/2))+(k1*i);
cout << ans;
Hope this helps :)
It is a mostly a mathematical question:
With
N unit of energy
B energy cost of 2nd screen
p number of time 1st button clicked
s number of time 2nd button clicked
You try to maximize
s * p
whereas
p + B * s <= N
so
p <= N - B * s
and then
maximize: N * s - B * s²
s0 = 0
s1 = N / B
sMax is (s0 + s1) / 2 -> N / (2*B)
so max value is
N²/2B - N²/4B -> N²/4B
so
std::cin >> n >> b;
std::cout << n * n / b / 4 << std::endl
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 6 years ago.
Improve this question
I have an assignment to create a C++ program to find all the possible mathematic equation with operators. Below shows the question:
Given a set of numbers, for example {5, 3, 7, 3, and 11}. Find all the possible mathematic equation with operators such as +, - ,*, / in such a way that the equation will produce the given answer. For example, 5+7-3/3=11.
I need an idea how to start with the code. Is it like brute force algorithm ? I have no idea how to interchange the operators to create the possible equation. I'm not asking for full solution, just an idea how to start the coding.
You could think about it like this. +,-,*,/ can be treated as 1,2,3,4 respectively. Now, if you were to try all the different combinations for whatever size array of numbers you get, you could look at it like this.
example. 4 numbers.
1,1,1,1 1,1,1,2 1,1,1,3 1,1,1,4
1,1,2,1 1,1,2,2 1,1,2,3 1,1,2,4
and so on and so forth. Hope this might help!
I would like to state that this is not my original idea. I will add the reference below.Please find the C++ code below. It might be of some help to you as it has been to me.
#include <iostream>
#include<string>
using namespace std;
void cntdwn(double sum, double previous, string digits, double target, std::string expr) {
if (digits.length() == 0) {
if (sum + previous == target) {
std::cout<<expr << " = " << target;
}
} else {
for (int i = 1; i <= digits.length(); i++) {
double current = std::stod(digits.substr(0, i));
string remaining = digits.substr(i);
cntdwn(sum + previous, current, remaining, target, expr + " + " + std::to_string(current));
cntdwn(sum, previous * current, remaining, target, expr + " * " + std::to_string(current));
cntdwn(sum, previous / current, remaining, target, expr + " / " + std::to_string(current));
cntdwn(sum + previous, -current, remaining, target, expr + " - " + std::to_string(current));
}
}
}
void f(string digits, double target) {
for (int i = 1; i <= digits.length(); i++) {
string current = digits.substr(0, i);
cntdwn(0, std::stod(current), digits.substr(i), target, current);
}
}
int main() {
// The digits go as concatenated string
f("5373",11);
return 0;
}
Output:
5 * 3.000000 - 7.000000 + 3.000000 = 11
References:
Generate all combinations of mathematical expressions that add to target (Java homework/interview)
https://math.stackexchange.com/questions/459857/using-operators-and-4-4-4-4-digits-find-all-formulas-that-would-resolve
https://www.careercup.com/question?id=5735906000502784
Additional Note
a) This code will not make any combinations for the digits. For example if we give (4,4,4,4) as numbers and 10 as target then it will not give any result as the solution in that case should be [(44-4)/4] {this example has been picked from second reference].
b) And this problem is a classic algorithmic problme popularly known as "Countdown Problem" picked from famous UK game.