this is the problem https://www.urionlinejudge.com.br/judge/en/problems/view/1042
and the code
#include <iostream>
using namespace std;
int A,B,C
int main ()
{
cin >> A >> B >> C;
if (A > B > C)
cout <<C<<"\n"<<B<<"\n"<<A<<"\n\n";
else if (B > A > C)
cout <<C<<"\n"<<A<<"\n"<<B<<"\n\n";
else if (C > A > B)
cout <<B<<"\n"<<A<<"\n"<<C<<"\n\n";
else if (A > C > B)
cout <<B<<"\n"<<C<<"\n"<<A<<"\n\n";
else if (C > B > A)
cout <<A<<"\n"<<B<<"\n"<<C<<"\n\n";
else {
(B > C > A);
cout <<A<<"\n"<<C<<"\n"<<B<<"\n\n";}
cout <<A<<"\n"<<B<<"\n"<<C<<endl;
return 0;
}
Your main problem is that if(A > B > C) becomes if (A > (B > C)) - in other words, you're comparing A to the true or false result of B > C, which will be true for all values where A of 2 or greater, regardless of the values of B and C.
The fix is to compare A > B separately from B > C, so use if (A > B && B > C) or some similar construct.
You have to trnasform things like:
(A > B > C)
into:
(A>B && B>C)
Conditions in the if statements are wrong. For example condition
if (A > B > C)
is equivalent to
if ( ( A > B ) > C)
In this case condition ( A > B ) is evaluated to either true or false and the condition is equivalent to either to
if ( true > C)
or
if ( false > C)
It is obvious that it is not what you want to get.
So the original condition has to be rewritten like
if (A > B && B > C )
But even in this case the program will be wrong because it does not consider cases when the variables could be equal each other. So the valid condition will look like
if (A >= B && B >= C )
And the program will not compile because at least here there is a typo
int A,B,C
^^^
You forgot to place a semicolon
int A,B,C;
Also there is no sense to declare variables A, B, C as global
int A,B,C;
int main ()
{
//...
It is better to declare them as local variables of function main
int main ()
{
int A, B, C;
//...
And this code snippet is wrong
else {
(B > C > A);
cout <<A<<"\n"<<C<<"\n"<<B<<"\n\n";}
cout <<A<<"\n"<<B<<"\n"<<C<<endl;
I think you mean simply
else cout <<A<<"\n"<<C<<"\n"<<B<<endl;
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 yesterday.
Improve this question
I need to find middle number from 3 numbers the user inputs. I'm completely new to C++.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a, b, c, d, mdq, mnq, mxq, mdw, mnw, mxw, mde, mne, mxe;
cin>>a>>b>>c;
mda = c;
if (mda>a and mda<b) mdq = c;
if (mda>a and mda>b) mxa = c;
if (mdq<a and mdq<b) mnq = c;
mdw = a:
if (mdw>b and mdw<c) mdq = a;
if (mdw>b and mdw>c) mxg = a;
if (mdw<b and mdw<c) mnq = a;
mde = b;
if (mde>a and mde<c) mdq = b;
if (mde>a and mde>c) mxq = b;
if (mde<a and mde<c) mnq = b;
d = mdq+mxq+mng:
d = d-mxq-mnq;
cout<<d;
}
I tried to find middle number and it sometimes work but whenever i type 4 7 1 the output is 1.
I think you are trying far too hard. There are only six possibilities, you don't need complicated logic and you definitely don't need mathematics.
#include <iostream>
int main()
{
int a, b, c;
std::cin >> a >> b >> c;
// it's a if b <= a <= c OR c <= a <= b
if ((b <= a && a <= c) || (c <= a && a <= b))
std::cout << "first # " << a << '\n';
// it's b if a <= b <= c OR c <= b <= a
else if ((a <= b && b <= c) || (c <= b && b <= a))
std::cout << "second # " << b << '\n';
// neither of the above, must be c
else
std::cout << "third # " << c << '\n';
}
I suppose the only tricky part is how to deal with equal numbers. I used <= which means given equal numbers the earlier number will be printed. So for example given 11 22 22 the output is second # 22 not third # 22.
Consider:
#include <algorithm>
#include <iostream>
int main()
{
int a, b, c ;
std::cin >> a >> b >> c ;
if( a > b ) std::swap( a, b ) ;
if( b > c ) std::swap( b, c ) ;
if( a > b ) std::swap( a, b ) ;
std::cout << b << '\n' ;
return 0;
}
It sorts the three values in order so that the answer is always b.
An alternative that does not modify the input:
#include <algorithm>
#include <iostream>
int main()
{
int a, b, c ;
std::cin >> a >> b >> c ;
int max1 = std::max( a, b ) ;
int max2 = std::max( b, c ) ;
int max3 = std::max( a, c ) ;
int mid = std::min( max1, max2 ) ;
mid = std::min( mid, max3 ) ;
std::cout << mid << '\n' ;
return 0;
}
which uses just four additional variables rather than ten in your attempt. It works by determining the maximum of all possible pairs: a,b, b,c and a,c, giving three values that contain either the largest or second largest value - the second largest value of three is also the middle value of course, so the smaller of these three maxima is the middle value.
You need not rely on the min()/max() functions here - they can be replaced with for example the expressions a < b ? a : b ; and a > b ? a : b or even if( a > b ) max1 = a; else max1 = b; etc.
Perhaps a more obvious solution, given that there are only 6 possible orderings of a b c, and only three possible outcomes for the middle value, then you can explicitly test for each possibility. You only need to explicitly test for two possible outcomes, since if neither of those are true, the answer is the third possibility:
#include <iostream>
int main()
{
int a, b, c;
std::cin >> a >> b >> c ;
int mid = a ; // Initially assume a is the middle
// (i.e. order: b a c or c a b)
// If b is in the middle...
if( (a <= b && b <= c) || // a b c or...
(c <= b && b <= a) ) // c b a
{
mid = b ;
}
// else if c is in the middle
else if( (a <= c && c <= b) || // a c b or...
(b <= c && c <= a) ) // b c a
{
mid = c ;
}
std::cout << mid << '\n' ;
return 0 ;
}
This is essentially #john's solution (credit), but you may find the explanatory comments here easier to follow perhaps.
heyall, just going through some textbook examples for my introductory c++ course and I would really appreciate it if somebody could clarify why the following code produces an output of 51 (I would expect it to not produce any output whatsoever), many thanks!:
#include <iostream>
using namespace std;
int main()
{
constexpr int a{9};
constexpr int b{1};
constexpr int c{5};
if (a < b < c)
if (c > b > a)
if (a > c) cout << 91;
else cout << 19;
else
if (b < c) cout << 51;
else cout << 15;
else
if (b < a < c)
if (a < c) cout << 95;
else cout << 59;
else
if (b < c) cout << 57;
else cout << 75;
return 0;
}
It seems you expect this expression:
if (a < b < c)
to be true if a, b, and c are in increasing order. But what actually happens is the expression becomes:
if ((a < b) < c)
which is either:
if (0 < c)
// or
if (1 < c)
Either way, that's probably not what you want. In fact, there's no good reason to ever write the above expression.
If you want to check whether the variables are increasing, you need to write something like:
if (a < b && b < c)
In c++, comparisons like 'X<=Y<=Z' do not have their mathematical meaning without parentheses. So, in
if (a < b < c)
we are getting
a < b => 9 < 1 => 0
'0' means the condition is false, which with 'c' is returning
0 < 5 => 1
"1" being returned means that the if condition is True.
Similarly, you can check for the nested if-else loops.
Help me plz..
I just had my midterm exam and one of the questions was to 'make your own function with three parameters that returns the smallest value.'
I coded for this in C++ as below.
int smallest(int a, int b, int c) {
if (a == b && b == c) {
return a;
}
else if ((a==b&&b!=c)||(b==c&&a!=b)||(a==c&&a!=b)){
if (a < b) { //a=c <b or a< b=c
return a;
}
else if (a < c) { //a=b <c or a< b=c
return a;
}
else if (b < a) { //b=c <a or b< a=c
return b;
}
else if (c < a) { //b=c <a or c< a=b
return c;
}
else {
cout << "Congratz you got -1 point";
return 99999999;
}
}
else {
if (a < b&&a < c) {
return a;
}
else if (b < a&&b < c) {
return b;
}
else {
return c;
}
}
When I run this in VS 2017, it works but..
if this function is called as
int main(){
cout << smallest(2,1,1);
return 0;
}
I thought this would make an error because there are two else if statements that include this case (else if(b < a) and else if(c < a)...)
It is not allowed for computers to choose which statement to run.. BUT WHY THIS WORKS..??? :( Pretty basic codes but I have no idea..
The two else if statements you mention are mutally exclusive in the sense that if the first condition evaluates to true, the second one is ignored. Ie:
if (a < b) {
return a;
}
else if (a < c) { // if this is true then...
return a;
}
else if (b < a) { // this condition wont even be evaluated
return b;
}
Also you seem to have a misunderstanding concerning return. Consider this code:
int foo() {
return 3;
return 5;
}
there is nothing wrong about this code, it just happens that the second return will never be executed.
Last but not least you should consider to simplify your code. Two if should be more than sufficient to produce the correct result. Something along the line of
if (a is smaller than b and smaller than c) return a;
else return the smaller of b and c;
The evaluation of the if statements happens from top to bottom. Therefore they don't need to be mutually exclusive, and the order of the conditionals does matter (cf. switch).
Note you can return the smallest value with the simple
return std::min({a, b, c});
I have following loop as a inner loop and try to get rid of it by transforming it into a mathematical formula:
while(!(((aux = a * b) <= c) && (c >= aux + d))) --a;
a, b, c, d and aux are all of type size_t, i.e. unsigned int's
NOTE: a is decremented in every iteration within the loop's body!
I'm totally stuck at this problem. I tried to simplify the loop condition, but failed because of the unsignedness constraint.
As result I just want to get the value of a depending on b, c, d.
Replace aux with a*b at every point, and you get:
!(a * b <= c && c - a * b >= d)
<=> !(a * b <= c && c >= d + a * b)
<=> !(a * b <= c && d + a * b <= c)
If d is greater than c, the second condition will be false and therefore the loop will never terminate. So we can consider only d <= c. The second condition is stricter, so we can focus on that solely:
<=> !(d + a * b <= c)
<=> !( a * b <= c - d)
<=> !( a <= (c - d)/b) // if integer division is used
<=> ( a > (c - d)/b)
Given that you only decrement a, it either needs to fulfil the condition (a <= (c - d)/b) right from the beginning or be less than or equal to (c - d)/b. Overall we get:
a = std::min(a, (c - d)/b);
Let's simplify:
while(!(((aux = a * b) <= c) && (c - aux >= d))) --a;
Drop aux in favor of just a*b:
while(!((a*b <= c) && (c - a*b >= d))) --a;
Rewrite the !(x && y) into !x || !y:
while ((a*b > c) || (c - a*b < d)) --a;
Flip the sign on the second expression:
while ((a*b > c) || (a*b > c - d)) --a;
Which is just:
while (a*b > min(c, c-d)) --a;
Which is to say, find the smallest a such that a*b <= min(c, c-d). Unless it's already smaller than that. So:
a = min(a, min(c, c-d) / b);
Er, given that all the variables are unsigned, obviously min(c, c-d) == c - d, so:
a = min(a, (c-d)/b);
This question already has answers here:
Mathematically Find Max Value without Conditional Comparison
(18 answers)
Closed 9 years ago.
So i have too get two numbers from user input, and find the max of the two numbers without using if statements.
The class is a beginner class, and we have too use what we already know. I kinda worked something out, but it only works if the numbers are inputted with the max number first.
#include <iostream>
using namespace std;
int main()
{
int x = 0, y = 0, max = 0;
int smallest, largest;
cout << "Please enter 2 integer numbers, and i will show you which one is larger: ";
cin >> x >> y;
smallest = (x < y == 1) + (x - 1);
smallest = (y < x == 1) + (y - 1);
largest = (x < y == 1) + (y - 1);
largest = (y > x == 1) + (x + 1 - 1);
cout << "Smallest: " << smallest << endl;
cout << "Largest: " << largest << endl;
return 0;
}
Thats what i have so far, but after putting different test data in, i found out it only works for numbers such as 4,5 or 6,7. But numbers with more then 2 spaces between eachother they dont such as, 4,8 or 5, 7. Any help would be appreciated.
I saw this question in Cracking the Coding interview book.
Let’s try to solve this by “re-wording” the problem We will re-word the problem until we get something that has removed all if statements
Rewording 1: If a > b, return a; else, return b
Rewording 2: If (a - b) is negative, return b; else, return a
Rewording 3: If (a - b) is negative, let k = 1; else, let k = 0 Return a - k * (a - b)
Rewording 4: Let c = a - b Let k = the most significant bit of c Return a - k * c
int getMax(int a, int b) {
int c = a - b;
int k = (c >> ((sizeof(int) * CHAR_BIT) - 1)) & 0x1;
int max = a - k * c;
return max;
}
Source: http://www.amazon.com/Cracking-Coding-Interview-Programming-Questions/dp/098478280X
Edit: This code works even when a-b overflows.
Let k equal the sign of a-b such that if a-b >=0, then k is 1, else k=0.Let q be the inverse of k. Above code overflows when a is positive or b is negative, or the other way around. If a and b have different signs, then we want the k to equal sign(a).
/* Flips 1 to 0 and vice-versa */
public static int flip(int bit){
return 1^bit;
}
/* returns 1 if a is positive, and 0 if a is negative */
public static int sign(int a){
return flip((a >> ((sizeof(int) * CHAR_BIT) - 1)) & 0x1);
}
public static int getMax(int a, int b){
int c = a - b;
int sa = sign(a-b); // if a>=0, then 1 else 0
int sb = sign(a-b); // if b>=1, then 1 else 0
int sc = sign(c); // depends on whether or not a-b overflows
/* If a and b have different signs, then k = sign(a) */
int use_sign_of_a = sa ^ sb;
/* If a and b have the same sign, then k = sign(a - b) */
int use_sign_of_c = flip(sa ^ sb);
int k = use_sign_of_a * sa + use_sign_of_c * sc;
int q = flip(k); //opposite of k
return a * k + b * q;
}
Here is a funny solution:
int max_num = (x>y)*x + (y>=x)*y;
Assuming that you have covered bitwise operators already you can do this:
max = a-((a-b)&((a-b)>>(sizeof(int)*8-1)));
This is based off of the solution from Mathematically Find Max Value without Conditional Comparison that #user93353 pointed out in the comments above.
This may be overkill if you really are just trying to avoid if statements, not comparisons in general.
You can try this code to find max and min for two input variables.
((a > b) && (max = a)) || (max=b);
((a < b) && (min = a)) || (min=b);
For three input variables you can use similar method like this:
int main()
{
int a = 10, b = 9 , c = 8;
cin >> a >> b >> c;
int max = a, min = a;
// For Max
((a > b) && (a > c) && (max=a)) ||
((b > c) && (b > a) && (max=b)) ||
(max=c) ;
// For min
((a < b) && (a < c) && (min=a)) ||
((b < c) && (b < a) && (min=b)) ||
(min=c) ;
cout << "max = " << max;
cout << "and min = " << min;
return 1;
}
One run is:
:~$ ./a.out
1
2
3
max = 3 and min = 1
Edit
Thanks to #Tony D: This code will fail for negative numbers.
One may try this for negative numbers for two inputs to find max(not sure for this):
((a > b) && ( a > 0 && (max = a))) || ((b > a) && (max = b)) || (max = a);