Sort three numbers using only if-statements
$begingroup$
Beginner here, trying to make a small program that sorts three numbers from smallest to largest only by using if
s. Any thoughts on how to improve this?
#include <iostream>
using namespace std;
int main ()
{
int num1, num2, num3;
int smallest, middle, biggest;
cin >> num1 >> num2 >> num3;
cout << endl;
if ((num1 < num2) && (num1 < num3))
{
smallest = num1;
if (num2 > num3)
{
biggest = num2;
middle = num3;
}
}
if ((num1 < num2) && (num3 << num1))
{
smallest = num1;
if (num2 < num3)
{
middle = num2;
biggest = num3;
}
}
if ((num1 > num2) && (num3 > num1))
{
middle = num1;
if (num2 < num3)
{
smallest = num2;
biggest = num3;
}
}
if ((num1 < num2) && (num3 < num1))
{
middle = num1;
if (num2 > num3)
{
biggest = num2;
smallest = num3;
}
}
if ((num1 > num2) && (num1 > num3))
{
biggest = num1;
if (num3 > num2)
{
middle = num3;
smallest = num2;
}
}
if ((num1 > num2) && (num1 > num3))
{
biggest = num1;
if (num2 > num3)
{
middle = num2;
smallest = num3;
}
}
cout << smallest << ", " << middle << ", " << biggest << endl;
return 0;
}
c++ beginner algorithm sorting
$endgroup$
add a comment |
$begingroup$
Beginner here, trying to make a small program that sorts three numbers from smallest to largest only by using if
s. Any thoughts on how to improve this?
#include <iostream>
using namespace std;
int main ()
{
int num1, num2, num3;
int smallest, middle, biggest;
cin >> num1 >> num2 >> num3;
cout << endl;
if ((num1 < num2) && (num1 < num3))
{
smallest = num1;
if (num2 > num3)
{
biggest = num2;
middle = num3;
}
}
if ((num1 < num2) && (num3 << num1))
{
smallest = num1;
if (num2 < num3)
{
middle = num2;
biggest = num3;
}
}
if ((num1 > num2) && (num3 > num1))
{
middle = num1;
if (num2 < num3)
{
smallest = num2;
biggest = num3;
}
}
if ((num1 < num2) && (num3 < num1))
{
middle = num1;
if (num2 > num3)
{
biggest = num2;
smallest = num3;
}
}
if ((num1 > num2) && (num1 > num3))
{
biggest = num1;
if (num3 > num2)
{
middle = num3;
smallest = num2;
}
}
if ((num1 > num2) && (num1 > num3))
{
biggest = num1;
if (num2 > num3)
{
middle = num2;
smallest = num3;
}
}
cout << smallest << ", " << middle << ", " << biggest << endl;
return 0;
}
c++ beginner algorithm sorting
$endgroup$
5
$begingroup$
Why the requirement of only usingif
? You might discover new things by trying other tools too :)
$endgroup$
– Morwenn
May 27 '15 at 9:54
2
$begingroup$
I still haven't reached that part of the book :)
$endgroup$
– liquid3
May 27 '15 at 9:58
add a comment |
$begingroup$
Beginner here, trying to make a small program that sorts three numbers from smallest to largest only by using if
s. Any thoughts on how to improve this?
#include <iostream>
using namespace std;
int main ()
{
int num1, num2, num3;
int smallest, middle, biggest;
cin >> num1 >> num2 >> num3;
cout << endl;
if ((num1 < num2) && (num1 < num3))
{
smallest = num1;
if (num2 > num3)
{
biggest = num2;
middle = num3;
}
}
if ((num1 < num2) && (num3 << num1))
{
smallest = num1;
if (num2 < num3)
{
middle = num2;
biggest = num3;
}
}
if ((num1 > num2) && (num3 > num1))
{
middle = num1;
if (num2 < num3)
{
smallest = num2;
biggest = num3;
}
}
if ((num1 < num2) && (num3 < num1))
{
middle = num1;
if (num2 > num3)
{
biggest = num2;
smallest = num3;
}
}
if ((num1 > num2) && (num1 > num3))
{
biggest = num1;
if (num3 > num2)
{
middle = num3;
smallest = num2;
}
}
if ((num1 > num2) && (num1 > num3))
{
biggest = num1;
if (num2 > num3)
{
middle = num2;
smallest = num3;
}
}
cout << smallest << ", " << middle << ", " << biggest << endl;
return 0;
}
c++ beginner algorithm sorting
$endgroup$
Beginner here, trying to make a small program that sorts three numbers from smallest to largest only by using if
s. Any thoughts on how to improve this?
#include <iostream>
using namespace std;
int main ()
{
int num1, num2, num3;
int smallest, middle, biggest;
cin >> num1 >> num2 >> num3;
cout << endl;
if ((num1 < num2) && (num1 < num3))
{
smallest = num1;
if (num2 > num3)
{
biggest = num2;
middle = num3;
}
}
if ((num1 < num2) && (num3 << num1))
{
smallest = num1;
if (num2 < num3)
{
middle = num2;
biggest = num3;
}
}
if ((num1 > num2) && (num3 > num1))
{
middle = num1;
if (num2 < num3)
{
smallest = num2;
biggest = num3;
}
}
if ((num1 < num2) && (num3 < num1))
{
middle = num1;
if (num2 > num3)
{
biggest = num2;
smallest = num3;
}
}
if ((num1 > num2) && (num1 > num3))
{
biggest = num1;
if (num3 > num2)
{
middle = num3;
smallest = num2;
}
}
if ((num1 > num2) && (num1 > num3))
{
biggest = num1;
if (num2 > num3)
{
middle = num2;
smallest = num3;
}
}
cout << smallest << ", " << middle << ", " << biggest << endl;
return 0;
}
c++ beginner algorithm sorting
c++ beginner algorithm sorting
edited May 27 '15 at 17:04
200_success
129k15153415
129k15153415
asked May 27 '15 at 9:46
liquid3liquid3
53115
53115
5
$begingroup$
Why the requirement of only usingif
? You might discover new things by trying other tools too :)
$endgroup$
– Morwenn
May 27 '15 at 9:54
2
$begingroup$
I still haven't reached that part of the book :)
$endgroup$
– liquid3
May 27 '15 at 9:58
add a comment |
5
$begingroup$
Why the requirement of only usingif
? You might discover new things by trying other tools too :)
$endgroup$
– Morwenn
May 27 '15 at 9:54
2
$begingroup$
I still haven't reached that part of the book :)
$endgroup$
– liquid3
May 27 '15 at 9:58
5
5
$begingroup$
Why the requirement of only using
if
? You might discover new things by trying other tools too :)$endgroup$
– Morwenn
May 27 '15 at 9:54
$begingroup$
Why the requirement of only using
if
? You might discover new things by trying other tools too :)$endgroup$
– Morwenn
May 27 '15 at 9:54
2
2
$begingroup$
I still haven't reached that part of the book :)
$endgroup$
– liquid3
May 27 '15 at 9:58
$begingroup$
I still haven't reached that part of the book :)
$endgroup$
– liquid3
May 27 '15 at 9:58
add a comment |
5 Answers
5
active
oldest
votes
$begingroup$
I will try to take several things into account but to keep things simple. A few remarks:
First, it seems that you have a typo here:
if ((num1 < num2) && (num3 << num1))
I think that you meant
num3 < num1
instead ofnum3 << num1
in your second condition.
As @Josay says, you better write small functions. I would add that you better separate the input/output operations (that you can keep in
main
) and the sorting function. In my opinion, this would be a good enough signature for the sorting function:
void sort3(int& a, int& b, int& c);
You would give three variables to it and it would sort them in-place so that you end up with $ a le b le c $.
Now, let's choose an algorithm. For three values, the easiest is to implement a bubble sort which shouldn't be really slower than other algorithms (when you want to sort more values, it becomes horribly slower though).
void sort3(int& a, int& b, int& c)
{
if (a > b)
{
std::swap(a, b);
}
if (b > c)
{
std::swap(b, c);
}
if (a > b)
{
std::swap(a, b);
}
}
Most sorting algorithms heavily rely on swapping values. The one I just implemented sorts your values with only three comparisons and at most three swaps and is, in my opinion, far simpler to understand than what you had.
Note that there are more efficient algorithms but I deliberately chose to present one that is not that bad while easy to understand.
This is not a problem in your case, but we can't stress out enough that
using namespace std;
is often considered bad practice. It is the case when used in a header file, especially in a library header file since it will pollute the global namespace of every file including it. That's not a problem for you since you're probably doing everything in a.cpp
file but it's better to keep that in mind.You don't need to
return 0;
at the end ofmain
. If the compiler reaches the end of themain
function without having encountered areturn
statement, it automagically adds areturn 0;
for you. Note that it only works withmain
though. Dropping this line is an interesting way to document that your program cannot return error codes and that it will only ever return0
.
$endgroup$
$begingroup$
Thanks for your answers, i'm afraid i can't give thumbs up because i need more rep
$endgroup$
– liquid3
May 27 '15 at 10:50
$begingroup$
why did you test if a > b twice?
$endgroup$
– Ritchie Shatter
May 27 '15 at 12:33
1
$begingroup$
@RitchieShatter If the values are $3, 2, 1$ the first two swaps will make the values $2, 1, 3$ so you need to swapa
andb
again.
$endgroup$
– Morwenn
May 27 '15 at 12:37
2
$begingroup$
@RitchieShatter - don't know if this helps, but if you've studied how bubble sort works, think of this code as a bubble sort with the loops unrolled; the smallest values bubble down from the top.
$endgroup$
– Pete Becker
May 27 '15 at 20:41
1
$begingroup$
"You don't need toreturn 0;
at the end of main"—technically correct, of course, but is it really advisable not to? Nothing good will come of not returning zero…a careless port to C (not C++), for example, will cause the exit status to be undefined.
$endgroup$
– wchargin
May 27 '15 at 22:40
|
show 3 more comments
$begingroup$
A few pieces of advice:
write small functions you can test
write tests
I did it for you and discovered problems quickly :
#include <iostream>
using namespace std;
void sort(int num1, int num2, int num3, int* smallest, int* middle, int* biggest)
{
if ((num1 < num2) && (num1 < num3))
{
*smallest = num1;
if (num2 > num3)
{
*biggest = num2;
*middle = num3;
}
}
if ((num1 < num2) && (num3 << num1))
{
*smallest = num1;
if (num2 < num3)
{
*middle = num2;
*biggest = num3;
}
}
if ((num1 > num2) && (num3 > num1))
{
*middle = num1;
if (num2 < num3)
{
*smallest = num2;
*biggest = num3;
}
}
if ((num1 < num2) && (num3 < num1))
{
*middle = num1;
if (num2 > num3)
{
*biggest = num2;
*smallest = num3;
}
}
if ((num1 > num2) && (num1 > num3))
{
*biggest = num1;
if (num3 > num2)
{
*middle = num3;
*smallest = num2;
}
}
if ((num1 > num2) && (num1 > num3))
{
*biggest = num1;
if (num2 > num3)
{
*middle = num2;
*smallest = num3;
}
}
}
//==========================================================================
int main ()
{
int num1, num2, num3;
int smallest, middle, biggest;
//cin >> num1 >> num2 >> num3;
//sort(num1, num2, num3, &smallest, &middle, &biggest);
//cout << endl;
//cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 1; num2 = 2; num3 = 3;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 4; num2 = 4; num3 = 4;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 5; num2 = 5; num3 = 6;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 7; num2 = 8; num3 = 7;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 9; num2 = 10; num3 = 10;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 11; num2 = 13; num3 = 12;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 15; num2 = 14; num3 = 16;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 19; num2 = 18; num3 = 17;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 21; num2 = 22; num3 = 20;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 25; num2 = 23; num3 = 24;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
return 0;
}
gives :
1, 2, 3
1, 2, 3
1, 2, 3
7, 2, 3
9, 2, 3
11, 12, 13
14, 15, 16
17, 18, 19
20, 21, 22
23, 24, 25
$endgroup$
$begingroup$
Thanks for the answer, the problem is when i type 2 same numbers
$endgroup$
– liquid3
May 27 '15 at 10:13
add a comment |
$begingroup$
Your code compiles, but your program is not exactly small. I like what you are trying to do. When I learn something new, I like to write a program to help showcase it. The problem is you need to bring the correct tools for the job.
Analogously You probably could hammer a nail with a screwdriver, but I wouldn't recommend it.
There are multiple ways you could get this code under 10 lines.
use of "else" and "else if" may help. however you will make your life considerably easier using logical operators. For example
#define max(x,y) ( x > y ? x : y )
you can make a simple preprocessor command that will give you the maximum value.
The logic for that command would read as if x is greater than y the answer is x, otherwise it's y.
The swap command mentioned above would work well to.
I highly recommend before limiting yourself to one statement you read a little about c++ operators. Try here.
http://www.cplusplus.com/doc/tutorial/operators/
$endgroup$
$begingroup$
You can get into some rather dangerous waters with that max macro if you do things such asmax(foo + 1, bar)
. Code golf is great for people who understand the full set of tricks for the language, but code that is understandable to the coder(or next person) is often something to strive for. Clever code needs you to be more clever to debug it - and if the code is at your edge of cleverness this can cause problems.
$endgroup$
– user22048
May 27 '15 at 14:06
2
$begingroup$
Why define amax
macro when there is already the saferstd::max
in the standard library? :(
$endgroup$
– Morwenn
May 27 '15 at 14:07
$begingroup$
Interesting. I wasn't aware of std::max. nor std::swap. I guess I need to look through the std libraries at some point. MichaelT, your point is well taken. However, I still feel that a strong understanding of operators should be an early fundamental for learning c/c++
$endgroup$
– mreff555
May 27 '15 at 17:12
add a comment |
$begingroup$
Assuming you define min and max (or use std::min, std::max), you can do this:
int low, mid, high;
// Find the minimum of number 1 and the minimum of number 2 and number 3.
low = std::min(num1, std::min(num2, num3));
// Find the maximum of the minimum of number 1 and number 2 and the minimum of number 2 and number 3.
mid = std::max(std::min(num1, num2), std::min(num2, num3));
// Find the maximum of number 1 and the maximum of number 2 and number 3.
high = std::max(num1, std:max(num2, num3));
Which basically moves all of your if-else logic into standard library calls.
By the way, a popular method for switching numbers in place without a temporary integer goes as follows:
x = x ^ y;
y = y ^ x;
x = x ^ y;
Which replaces the need for a temp variable. Try it out and see.
if(a > b) {
a = a ^ b;
b = b ^ a;
a = a ^ b;
}
if(b > c) {
b = b ^ c;
c = c ^ b;
b = b ^ c;
}
if(a > b) {
a = a ^ b;
b = b ^ a;
a = a ^ b;
}
Of course, you could instead use std::swap. However, given all the alternatives, if performance was a non-issue (e.g. absolute clock cycle count was unimportant), I'd impress simply using std::min and std::max.
$endgroup$
add a comment |
$begingroup$
#include <iostream>
int min (int g, int a, int b){
if (g < a && g < b){
return g;
}
else if (a < g && a < b){
return a;
}
else if (b < a && b < g){
return b;
}
}
int max (int g, int a, int b){
if (g > a && g >> b){
return g;
}
else if(a > g && a > b)
{
return a;
}
else if (b > g && b > a)
{
return b;
}
}
int sort(int *g, int *a, int *b){
int small = min(*g ,*a, *b);
int big = max(*g, *a, *b);
int mid ;
if ((big != *g) && (small < *g)){
mid= *g;
}
if ((big != *a) && (small < *a)){
mid= *a;
}
if ((big != *b) && (small < *b)){
mid= *b;
}
std::cout << "*output :"<< std::endl;
std::cout << " small: " << small<< std::endl;
std::cout << " mid: " << mid<< std::endl;;
std::cout << " big: " << big<< std::endl;;
}
int main(){
int first,secnd,thered;
do {
std::cout << "*what numbers to sort?";
std::cout << std::endl;
std::cin >> first >>secnd >> thered ;
std::cout << std::endl;
if (first != 0) {
sort(&first, &secnd, &thered);
}
}while(first != 0);
}
New contributor
$endgroup$
$begingroup$
You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process.
$endgroup$
– Jamal♦
22 mins ago
add a comment |
protected by Jamal♦ 22 mins ago
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
I will try to take several things into account but to keep things simple. A few remarks:
First, it seems that you have a typo here:
if ((num1 < num2) && (num3 << num1))
I think that you meant
num3 < num1
instead ofnum3 << num1
in your second condition.
As @Josay says, you better write small functions. I would add that you better separate the input/output operations (that you can keep in
main
) and the sorting function. In my opinion, this would be a good enough signature for the sorting function:
void sort3(int& a, int& b, int& c);
You would give three variables to it and it would sort them in-place so that you end up with $ a le b le c $.
Now, let's choose an algorithm. For three values, the easiest is to implement a bubble sort which shouldn't be really slower than other algorithms (when you want to sort more values, it becomes horribly slower though).
void sort3(int& a, int& b, int& c)
{
if (a > b)
{
std::swap(a, b);
}
if (b > c)
{
std::swap(b, c);
}
if (a > b)
{
std::swap(a, b);
}
}
Most sorting algorithms heavily rely on swapping values. The one I just implemented sorts your values with only three comparisons and at most three swaps and is, in my opinion, far simpler to understand than what you had.
Note that there are more efficient algorithms but I deliberately chose to present one that is not that bad while easy to understand.
This is not a problem in your case, but we can't stress out enough that
using namespace std;
is often considered bad practice. It is the case when used in a header file, especially in a library header file since it will pollute the global namespace of every file including it. That's not a problem for you since you're probably doing everything in a.cpp
file but it's better to keep that in mind.You don't need to
return 0;
at the end ofmain
. If the compiler reaches the end of themain
function without having encountered areturn
statement, it automagically adds areturn 0;
for you. Note that it only works withmain
though. Dropping this line is an interesting way to document that your program cannot return error codes and that it will only ever return0
.
$endgroup$
$begingroup$
Thanks for your answers, i'm afraid i can't give thumbs up because i need more rep
$endgroup$
– liquid3
May 27 '15 at 10:50
$begingroup$
why did you test if a > b twice?
$endgroup$
– Ritchie Shatter
May 27 '15 at 12:33
1
$begingroup$
@RitchieShatter If the values are $3, 2, 1$ the first two swaps will make the values $2, 1, 3$ so you need to swapa
andb
again.
$endgroup$
– Morwenn
May 27 '15 at 12:37
2
$begingroup$
@RitchieShatter - don't know if this helps, but if you've studied how bubble sort works, think of this code as a bubble sort with the loops unrolled; the smallest values bubble down from the top.
$endgroup$
– Pete Becker
May 27 '15 at 20:41
1
$begingroup$
"You don't need toreturn 0;
at the end of main"—technically correct, of course, but is it really advisable not to? Nothing good will come of not returning zero…a careless port to C (not C++), for example, will cause the exit status to be undefined.
$endgroup$
– wchargin
May 27 '15 at 22:40
|
show 3 more comments
$begingroup$
I will try to take several things into account but to keep things simple. A few remarks:
First, it seems that you have a typo here:
if ((num1 < num2) && (num3 << num1))
I think that you meant
num3 < num1
instead ofnum3 << num1
in your second condition.
As @Josay says, you better write small functions. I would add that you better separate the input/output operations (that you can keep in
main
) and the sorting function. In my opinion, this would be a good enough signature for the sorting function:
void sort3(int& a, int& b, int& c);
You would give three variables to it and it would sort them in-place so that you end up with $ a le b le c $.
Now, let's choose an algorithm. For three values, the easiest is to implement a bubble sort which shouldn't be really slower than other algorithms (when you want to sort more values, it becomes horribly slower though).
void sort3(int& a, int& b, int& c)
{
if (a > b)
{
std::swap(a, b);
}
if (b > c)
{
std::swap(b, c);
}
if (a > b)
{
std::swap(a, b);
}
}
Most sorting algorithms heavily rely on swapping values. The one I just implemented sorts your values with only three comparisons and at most three swaps and is, in my opinion, far simpler to understand than what you had.
Note that there are more efficient algorithms but I deliberately chose to present one that is not that bad while easy to understand.
This is not a problem in your case, but we can't stress out enough that
using namespace std;
is often considered bad practice. It is the case when used in a header file, especially in a library header file since it will pollute the global namespace of every file including it. That's not a problem for you since you're probably doing everything in a.cpp
file but it's better to keep that in mind.You don't need to
return 0;
at the end ofmain
. If the compiler reaches the end of themain
function without having encountered areturn
statement, it automagically adds areturn 0;
for you. Note that it only works withmain
though. Dropping this line is an interesting way to document that your program cannot return error codes and that it will only ever return0
.
$endgroup$
$begingroup$
Thanks for your answers, i'm afraid i can't give thumbs up because i need more rep
$endgroup$
– liquid3
May 27 '15 at 10:50
$begingroup$
why did you test if a > b twice?
$endgroup$
– Ritchie Shatter
May 27 '15 at 12:33
1
$begingroup$
@RitchieShatter If the values are $3, 2, 1$ the first two swaps will make the values $2, 1, 3$ so you need to swapa
andb
again.
$endgroup$
– Morwenn
May 27 '15 at 12:37
2
$begingroup$
@RitchieShatter - don't know if this helps, but if you've studied how bubble sort works, think of this code as a bubble sort with the loops unrolled; the smallest values bubble down from the top.
$endgroup$
– Pete Becker
May 27 '15 at 20:41
1
$begingroup$
"You don't need toreturn 0;
at the end of main"—technically correct, of course, but is it really advisable not to? Nothing good will come of not returning zero…a careless port to C (not C++), for example, will cause the exit status to be undefined.
$endgroup$
– wchargin
May 27 '15 at 22:40
|
show 3 more comments
$begingroup$
I will try to take several things into account but to keep things simple. A few remarks:
First, it seems that you have a typo here:
if ((num1 < num2) && (num3 << num1))
I think that you meant
num3 < num1
instead ofnum3 << num1
in your second condition.
As @Josay says, you better write small functions. I would add that you better separate the input/output operations (that you can keep in
main
) and the sorting function. In my opinion, this would be a good enough signature for the sorting function:
void sort3(int& a, int& b, int& c);
You would give three variables to it and it would sort them in-place so that you end up with $ a le b le c $.
Now, let's choose an algorithm. For three values, the easiest is to implement a bubble sort which shouldn't be really slower than other algorithms (when you want to sort more values, it becomes horribly slower though).
void sort3(int& a, int& b, int& c)
{
if (a > b)
{
std::swap(a, b);
}
if (b > c)
{
std::swap(b, c);
}
if (a > b)
{
std::swap(a, b);
}
}
Most sorting algorithms heavily rely on swapping values. The one I just implemented sorts your values with only three comparisons and at most three swaps and is, in my opinion, far simpler to understand than what you had.
Note that there are more efficient algorithms but I deliberately chose to present one that is not that bad while easy to understand.
This is not a problem in your case, but we can't stress out enough that
using namespace std;
is often considered bad practice. It is the case when used in a header file, especially in a library header file since it will pollute the global namespace of every file including it. That's not a problem for you since you're probably doing everything in a.cpp
file but it's better to keep that in mind.You don't need to
return 0;
at the end ofmain
. If the compiler reaches the end of themain
function without having encountered areturn
statement, it automagically adds areturn 0;
for you. Note that it only works withmain
though. Dropping this line is an interesting way to document that your program cannot return error codes and that it will only ever return0
.
$endgroup$
I will try to take several things into account but to keep things simple. A few remarks:
First, it seems that you have a typo here:
if ((num1 < num2) && (num3 << num1))
I think that you meant
num3 < num1
instead ofnum3 << num1
in your second condition.
As @Josay says, you better write small functions. I would add that you better separate the input/output operations (that you can keep in
main
) and the sorting function. In my opinion, this would be a good enough signature for the sorting function:
void sort3(int& a, int& b, int& c);
You would give three variables to it and it would sort them in-place so that you end up with $ a le b le c $.
Now, let's choose an algorithm. For three values, the easiest is to implement a bubble sort which shouldn't be really slower than other algorithms (when you want to sort more values, it becomes horribly slower though).
void sort3(int& a, int& b, int& c)
{
if (a > b)
{
std::swap(a, b);
}
if (b > c)
{
std::swap(b, c);
}
if (a > b)
{
std::swap(a, b);
}
}
Most sorting algorithms heavily rely on swapping values. The one I just implemented sorts your values with only three comparisons and at most three swaps and is, in my opinion, far simpler to understand than what you had.
Note that there are more efficient algorithms but I deliberately chose to present one that is not that bad while easy to understand.
This is not a problem in your case, but we can't stress out enough that
using namespace std;
is often considered bad practice. It is the case when used in a header file, especially in a library header file since it will pollute the global namespace of every file including it. That's not a problem for you since you're probably doing everything in a.cpp
file but it's better to keep that in mind.You don't need to
return 0;
at the end ofmain
. If the compiler reaches the end of themain
function without having encountered areturn
statement, it automagically adds areturn 0;
for you. Note that it only works withmain
though. Dropping this line is an interesting way to document that your program cannot return error codes and that it will only ever return0
.
edited May 23 '17 at 12:40
Community♦
1
1
answered May 27 '15 at 10:19
MorwennMorwenn
15k248113
15k248113
$begingroup$
Thanks for your answers, i'm afraid i can't give thumbs up because i need more rep
$endgroup$
– liquid3
May 27 '15 at 10:50
$begingroup$
why did you test if a > b twice?
$endgroup$
– Ritchie Shatter
May 27 '15 at 12:33
1
$begingroup$
@RitchieShatter If the values are $3, 2, 1$ the first two swaps will make the values $2, 1, 3$ so you need to swapa
andb
again.
$endgroup$
– Morwenn
May 27 '15 at 12:37
2
$begingroup$
@RitchieShatter - don't know if this helps, but if you've studied how bubble sort works, think of this code as a bubble sort with the loops unrolled; the smallest values bubble down from the top.
$endgroup$
– Pete Becker
May 27 '15 at 20:41
1
$begingroup$
"You don't need toreturn 0;
at the end of main"—technically correct, of course, but is it really advisable not to? Nothing good will come of not returning zero…a careless port to C (not C++), for example, will cause the exit status to be undefined.
$endgroup$
– wchargin
May 27 '15 at 22:40
|
show 3 more comments
$begingroup$
Thanks for your answers, i'm afraid i can't give thumbs up because i need more rep
$endgroup$
– liquid3
May 27 '15 at 10:50
$begingroup$
why did you test if a > b twice?
$endgroup$
– Ritchie Shatter
May 27 '15 at 12:33
1
$begingroup$
@RitchieShatter If the values are $3, 2, 1$ the first two swaps will make the values $2, 1, 3$ so you need to swapa
andb
again.
$endgroup$
– Morwenn
May 27 '15 at 12:37
2
$begingroup$
@RitchieShatter - don't know if this helps, but if you've studied how bubble sort works, think of this code as a bubble sort with the loops unrolled; the smallest values bubble down from the top.
$endgroup$
– Pete Becker
May 27 '15 at 20:41
1
$begingroup$
"You don't need toreturn 0;
at the end of main"—technically correct, of course, but is it really advisable not to? Nothing good will come of not returning zero…a careless port to C (not C++), for example, will cause the exit status to be undefined.
$endgroup$
– wchargin
May 27 '15 at 22:40
$begingroup$
Thanks for your answers, i'm afraid i can't give thumbs up because i need more rep
$endgroup$
– liquid3
May 27 '15 at 10:50
$begingroup$
Thanks for your answers, i'm afraid i can't give thumbs up because i need more rep
$endgroup$
– liquid3
May 27 '15 at 10:50
$begingroup$
why did you test if a > b twice?
$endgroup$
– Ritchie Shatter
May 27 '15 at 12:33
$begingroup$
why did you test if a > b twice?
$endgroup$
– Ritchie Shatter
May 27 '15 at 12:33
1
1
$begingroup$
@RitchieShatter If the values are $3, 2, 1$ the first two swaps will make the values $2, 1, 3$ so you need to swap
a
and b
again.$endgroup$
– Morwenn
May 27 '15 at 12:37
$begingroup$
@RitchieShatter If the values are $3, 2, 1$ the first two swaps will make the values $2, 1, 3$ so you need to swap
a
and b
again.$endgroup$
– Morwenn
May 27 '15 at 12:37
2
2
$begingroup$
@RitchieShatter - don't know if this helps, but if you've studied how bubble sort works, think of this code as a bubble sort with the loops unrolled; the smallest values bubble down from the top.
$endgroup$
– Pete Becker
May 27 '15 at 20:41
$begingroup$
@RitchieShatter - don't know if this helps, but if you've studied how bubble sort works, think of this code as a bubble sort with the loops unrolled; the smallest values bubble down from the top.
$endgroup$
– Pete Becker
May 27 '15 at 20:41
1
1
$begingroup$
"You don't need to
return 0;
at the end of main"—technically correct, of course, but is it really advisable not to? Nothing good will come of not returning zero…a careless port to C (not C++), for example, will cause the exit status to be undefined.$endgroup$
– wchargin
May 27 '15 at 22:40
$begingroup$
"You don't need to
return 0;
at the end of main"—technically correct, of course, but is it really advisable not to? Nothing good will come of not returning zero…a careless port to C (not C++), for example, will cause the exit status to be undefined.$endgroup$
– wchargin
May 27 '15 at 22:40
|
show 3 more comments
$begingroup$
A few pieces of advice:
write small functions you can test
write tests
I did it for you and discovered problems quickly :
#include <iostream>
using namespace std;
void sort(int num1, int num2, int num3, int* smallest, int* middle, int* biggest)
{
if ((num1 < num2) && (num1 < num3))
{
*smallest = num1;
if (num2 > num3)
{
*biggest = num2;
*middle = num3;
}
}
if ((num1 < num2) && (num3 << num1))
{
*smallest = num1;
if (num2 < num3)
{
*middle = num2;
*biggest = num3;
}
}
if ((num1 > num2) && (num3 > num1))
{
*middle = num1;
if (num2 < num3)
{
*smallest = num2;
*biggest = num3;
}
}
if ((num1 < num2) && (num3 < num1))
{
*middle = num1;
if (num2 > num3)
{
*biggest = num2;
*smallest = num3;
}
}
if ((num1 > num2) && (num1 > num3))
{
*biggest = num1;
if (num3 > num2)
{
*middle = num3;
*smallest = num2;
}
}
if ((num1 > num2) && (num1 > num3))
{
*biggest = num1;
if (num2 > num3)
{
*middle = num2;
*smallest = num3;
}
}
}
//==========================================================================
int main ()
{
int num1, num2, num3;
int smallest, middle, biggest;
//cin >> num1 >> num2 >> num3;
//sort(num1, num2, num3, &smallest, &middle, &biggest);
//cout << endl;
//cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 1; num2 = 2; num3 = 3;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 4; num2 = 4; num3 = 4;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 5; num2 = 5; num3 = 6;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 7; num2 = 8; num3 = 7;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 9; num2 = 10; num3 = 10;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 11; num2 = 13; num3 = 12;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 15; num2 = 14; num3 = 16;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 19; num2 = 18; num3 = 17;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 21; num2 = 22; num3 = 20;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 25; num2 = 23; num3 = 24;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
return 0;
}
gives :
1, 2, 3
1, 2, 3
1, 2, 3
7, 2, 3
9, 2, 3
11, 12, 13
14, 15, 16
17, 18, 19
20, 21, 22
23, 24, 25
$endgroup$
$begingroup$
Thanks for the answer, the problem is when i type 2 same numbers
$endgroup$
– liquid3
May 27 '15 at 10:13
add a comment |
$begingroup$
A few pieces of advice:
write small functions you can test
write tests
I did it for you and discovered problems quickly :
#include <iostream>
using namespace std;
void sort(int num1, int num2, int num3, int* smallest, int* middle, int* biggest)
{
if ((num1 < num2) && (num1 < num3))
{
*smallest = num1;
if (num2 > num3)
{
*biggest = num2;
*middle = num3;
}
}
if ((num1 < num2) && (num3 << num1))
{
*smallest = num1;
if (num2 < num3)
{
*middle = num2;
*biggest = num3;
}
}
if ((num1 > num2) && (num3 > num1))
{
*middle = num1;
if (num2 < num3)
{
*smallest = num2;
*biggest = num3;
}
}
if ((num1 < num2) && (num3 < num1))
{
*middle = num1;
if (num2 > num3)
{
*biggest = num2;
*smallest = num3;
}
}
if ((num1 > num2) && (num1 > num3))
{
*biggest = num1;
if (num3 > num2)
{
*middle = num3;
*smallest = num2;
}
}
if ((num1 > num2) && (num1 > num3))
{
*biggest = num1;
if (num2 > num3)
{
*middle = num2;
*smallest = num3;
}
}
}
//==========================================================================
int main ()
{
int num1, num2, num3;
int smallest, middle, biggest;
//cin >> num1 >> num2 >> num3;
//sort(num1, num2, num3, &smallest, &middle, &biggest);
//cout << endl;
//cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 1; num2 = 2; num3 = 3;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 4; num2 = 4; num3 = 4;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 5; num2 = 5; num3 = 6;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 7; num2 = 8; num3 = 7;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 9; num2 = 10; num3 = 10;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 11; num2 = 13; num3 = 12;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 15; num2 = 14; num3 = 16;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 19; num2 = 18; num3 = 17;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 21; num2 = 22; num3 = 20;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 25; num2 = 23; num3 = 24;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
return 0;
}
gives :
1, 2, 3
1, 2, 3
1, 2, 3
7, 2, 3
9, 2, 3
11, 12, 13
14, 15, 16
17, 18, 19
20, 21, 22
23, 24, 25
$endgroup$
$begingroup$
Thanks for the answer, the problem is when i type 2 same numbers
$endgroup$
– liquid3
May 27 '15 at 10:13
add a comment |
$begingroup$
A few pieces of advice:
write small functions you can test
write tests
I did it for you and discovered problems quickly :
#include <iostream>
using namespace std;
void sort(int num1, int num2, int num3, int* smallest, int* middle, int* biggest)
{
if ((num1 < num2) && (num1 < num3))
{
*smallest = num1;
if (num2 > num3)
{
*biggest = num2;
*middle = num3;
}
}
if ((num1 < num2) && (num3 << num1))
{
*smallest = num1;
if (num2 < num3)
{
*middle = num2;
*biggest = num3;
}
}
if ((num1 > num2) && (num3 > num1))
{
*middle = num1;
if (num2 < num3)
{
*smallest = num2;
*biggest = num3;
}
}
if ((num1 < num2) && (num3 < num1))
{
*middle = num1;
if (num2 > num3)
{
*biggest = num2;
*smallest = num3;
}
}
if ((num1 > num2) && (num1 > num3))
{
*biggest = num1;
if (num3 > num2)
{
*middle = num3;
*smallest = num2;
}
}
if ((num1 > num2) && (num1 > num3))
{
*biggest = num1;
if (num2 > num3)
{
*middle = num2;
*smallest = num3;
}
}
}
//==========================================================================
int main ()
{
int num1, num2, num3;
int smallest, middle, biggest;
//cin >> num1 >> num2 >> num3;
//sort(num1, num2, num3, &smallest, &middle, &biggest);
//cout << endl;
//cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 1; num2 = 2; num3 = 3;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 4; num2 = 4; num3 = 4;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 5; num2 = 5; num3 = 6;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 7; num2 = 8; num3 = 7;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 9; num2 = 10; num3 = 10;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 11; num2 = 13; num3 = 12;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 15; num2 = 14; num3 = 16;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 19; num2 = 18; num3 = 17;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 21; num2 = 22; num3 = 20;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 25; num2 = 23; num3 = 24;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
return 0;
}
gives :
1, 2, 3
1, 2, 3
1, 2, 3
7, 2, 3
9, 2, 3
11, 12, 13
14, 15, 16
17, 18, 19
20, 21, 22
23, 24, 25
$endgroup$
A few pieces of advice:
write small functions you can test
write tests
I did it for you and discovered problems quickly :
#include <iostream>
using namespace std;
void sort(int num1, int num2, int num3, int* smallest, int* middle, int* biggest)
{
if ((num1 < num2) && (num1 < num3))
{
*smallest = num1;
if (num2 > num3)
{
*biggest = num2;
*middle = num3;
}
}
if ((num1 < num2) && (num3 << num1))
{
*smallest = num1;
if (num2 < num3)
{
*middle = num2;
*biggest = num3;
}
}
if ((num1 > num2) && (num3 > num1))
{
*middle = num1;
if (num2 < num3)
{
*smallest = num2;
*biggest = num3;
}
}
if ((num1 < num2) && (num3 < num1))
{
*middle = num1;
if (num2 > num3)
{
*biggest = num2;
*smallest = num3;
}
}
if ((num1 > num2) && (num1 > num3))
{
*biggest = num1;
if (num3 > num2)
{
*middle = num3;
*smallest = num2;
}
}
if ((num1 > num2) && (num1 > num3))
{
*biggest = num1;
if (num2 > num3)
{
*middle = num2;
*smallest = num3;
}
}
}
//==========================================================================
int main ()
{
int num1, num2, num3;
int smallest, middle, biggest;
//cin >> num1 >> num2 >> num3;
//sort(num1, num2, num3, &smallest, &middle, &biggest);
//cout << endl;
//cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 1; num2 = 2; num3 = 3;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 4; num2 = 4; num3 = 4;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 5; num2 = 5; num3 = 6;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 7; num2 = 8; num3 = 7;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 9; num2 = 10; num3 = 10;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 11; num2 = 13; num3 = 12;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 15; num2 = 14; num3 = 16;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 19; num2 = 18; num3 = 17;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 21; num2 = 22; num3 = 20;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
num1 = 25; num2 = 23; num3 = 24;
sort(num1, num2, num3, &smallest, &middle, &biggest);
cout << smallest << ", " << middle << ", " << biggest << endl;
return 0;
}
gives :
1, 2, 3
1, 2, 3
1, 2, 3
7, 2, 3
9, 2, 3
11, 12, 13
14, 15, 16
17, 18, 19
20, 21, 22
23, 24, 25
edited Mar 1 '16 at 8:52
answered May 27 '15 at 10:01
JosayJosay
25.7k14087
25.7k14087
$begingroup$
Thanks for the answer, the problem is when i type 2 same numbers
$endgroup$
– liquid3
May 27 '15 at 10:13
add a comment |
$begingroup$
Thanks for the answer, the problem is when i type 2 same numbers
$endgroup$
– liquid3
May 27 '15 at 10:13
$begingroup$
Thanks for the answer, the problem is when i type 2 same numbers
$endgroup$
– liquid3
May 27 '15 at 10:13
$begingroup$
Thanks for the answer, the problem is when i type 2 same numbers
$endgroup$
– liquid3
May 27 '15 at 10:13
add a comment |
$begingroup$
Your code compiles, but your program is not exactly small. I like what you are trying to do. When I learn something new, I like to write a program to help showcase it. The problem is you need to bring the correct tools for the job.
Analogously You probably could hammer a nail with a screwdriver, but I wouldn't recommend it.
There are multiple ways you could get this code under 10 lines.
use of "else" and "else if" may help. however you will make your life considerably easier using logical operators. For example
#define max(x,y) ( x > y ? x : y )
you can make a simple preprocessor command that will give you the maximum value.
The logic for that command would read as if x is greater than y the answer is x, otherwise it's y.
The swap command mentioned above would work well to.
I highly recommend before limiting yourself to one statement you read a little about c++ operators. Try here.
http://www.cplusplus.com/doc/tutorial/operators/
$endgroup$
$begingroup$
You can get into some rather dangerous waters with that max macro if you do things such asmax(foo + 1, bar)
. Code golf is great for people who understand the full set of tricks for the language, but code that is understandable to the coder(or next person) is often something to strive for. Clever code needs you to be more clever to debug it - and if the code is at your edge of cleverness this can cause problems.
$endgroup$
– user22048
May 27 '15 at 14:06
2
$begingroup$
Why define amax
macro when there is already the saferstd::max
in the standard library? :(
$endgroup$
– Morwenn
May 27 '15 at 14:07
$begingroup$
Interesting. I wasn't aware of std::max. nor std::swap. I guess I need to look through the std libraries at some point. MichaelT, your point is well taken. However, I still feel that a strong understanding of operators should be an early fundamental for learning c/c++
$endgroup$
– mreff555
May 27 '15 at 17:12
add a comment |
$begingroup$
Your code compiles, but your program is not exactly small. I like what you are trying to do. When I learn something new, I like to write a program to help showcase it. The problem is you need to bring the correct tools for the job.
Analogously You probably could hammer a nail with a screwdriver, but I wouldn't recommend it.
There are multiple ways you could get this code under 10 lines.
use of "else" and "else if" may help. however you will make your life considerably easier using logical operators. For example
#define max(x,y) ( x > y ? x : y )
you can make a simple preprocessor command that will give you the maximum value.
The logic for that command would read as if x is greater than y the answer is x, otherwise it's y.
The swap command mentioned above would work well to.
I highly recommend before limiting yourself to one statement you read a little about c++ operators. Try here.
http://www.cplusplus.com/doc/tutorial/operators/
$endgroup$
$begingroup$
You can get into some rather dangerous waters with that max macro if you do things such asmax(foo + 1, bar)
. Code golf is great for people who understand the full set of tricks for the language, but code that is understandable to the coder(or next person) is often something to strive for. Clever code needs you to be more clever to debug it - and if the code is at your edge of cleverness this can cause problems.
$endgroup$
– user22048
May 27 '15 at 14:06
2
$begingroup$
Why define amax
macro when there is already the saferstd::max
in the standard library? :(
$endgroup$
– Morwenn
May 27 '15 at 14:07
$begingroup$
Interesting. I wasn't aware of std::max. nor std::swap. I guess I need to look through the std libraries at some point. MichaelT, your point is well taken. However, I still feel that a strong understanding of operators should be an early fundamental for learning c/c++
$endgroup$
– mreff555
May 27 '15 at 17:12
add a comment |
$begingroup$
Your code compiles, but your program is not exactly small. I like what you are trying to do. When I learn something new, I like to write a program to help showcase it. The problem is you need to bring the correct tools for the job.
Analogously You probably could hammer a nail with a screwdriver, but I wouldn't recommend it.
There are multiple ways you could get this code under 10 lines.
use of "else" and "else if" may help. however you will make your life considerably easier using logical operators. For example
#define max(x,y) ( x > y ? x : y )
you can make a simple preprocessor command that will give you the maximum value.
The logic for that command would read as if x is greater than y the answer is x, otherwise it's y.
The swap command mentioned above would work well to.
I highly recommend before limiting yourself to one statement you read a little about c++ operators. Try here.
http://www.cplusplus.com/doc/tutorial/operators/
$endgroup$
Your code compiles, but your program is not exactly small. I like what you are trying to do. When I learn something new, I like to write a program to help showcase it. The problem is you need to bring the correct tools for the job.
Analogously You probably could hammer a nail with a screwdriver, but I wouldn't recommend it.
There are multiple ways you could get this code under 10 lines.
use of "else" and "else if" may help. however you will make your life considerably easier using logical operators. For example
#define max(x,y) ( x > y ? x : y )
you can make a simple preprocessor command that will give you the maximum value.
The logic for that command would read as if x is greater than y the answer is x, otherwise it's y.
The swap command mentioned above would work well to.
I highly recommend before limiting yourself to one statement you read a little about c++ operators. Try here.
http://www.cplusplus.com/doc/tutorial/operators/
answered May 27 '15 at 13:59
mreff555mreff555
1662
1662
$begingroup$
You can get into some rather dangerous waters with that max macro if you do things such asmax(foo + 1, bar)
. Code golf is great for people who understand the full set of tricks for the language, but code that is understandable to the coder(or next person) is often something to strive for. Clever code needs you to be more clever to debug it - and if the code is at your edge of cleverness this can cause problems.
$endgroup$
– user22048
May 27 '15 at 14:06
2
$begingroup$
Why define amax
macro when there is already the saferstd::max
in the standard library? :(
$endgroup$
– Morwenn
May 27 '15 at 14:07
$begingroup$
Interesting. I wasn't aware of std::max. nor std::swap. I guess I need to look through the std libraries at some point. MichaelT, your point is well taken. However, I still feel that a strong understanding of operators should be an early fundamental for learning c/c++
$endgroup$
– mreff555
May 27 '15 at 17:12
add a comment |
$begingroup$
You can get into some rather dangerous waters with that max macro if you do things such asmax(foo + 1, bar)
. Code golf is great for people who understand the full set of tricks for the language, but code that is understandable to the coder(or next person) is often something to strive for. Clever code needs you to be more clever to debug it - and if the code is at your edge of cleverness this can cause problems.
$endgroup$
– user22048
May 27 '15 at 14:06
2
$begingroup$
Why define amax
macro when there is already the saferstd::max
in the standard library? :(
$endgroup$
– Morwenn
May 27 '15 at 14:07
$begingroup$
Interesting. I wasn't aware of std::max. nor std::swap. I guess I need to look through the std libraries at some point. MichaelT, your point is well taken. However, I still feel that a strong understanding of operators should be an early fundamental for learning c/c++
$endgroup$
– mreff555
May 27 '15 at 17:12
$begingroup$
You can get into some rather dangerous waters with that max macro if you do things such as
max(foo + 1, bar)
. Code golf is great for people who understand the full set of tricks for the language, but code that is understandable to the coder(or next person) is often something to strive for. Clever code needs you to be more clever to debug it - and if the code is at your edge of cleverness this can cause problems.$endgroup$
– user22048
May 27 '15 at 14:06
$begingroup$
You can get into some rather dangerous waters with that max macro if you do things such as
max(foo + 1, bar)
. Code golf is great for people who understand the full set of tricks for the language, but code that is understandable to the coder(or next person) is often something to strive for. Clever code needs you to be more clever to debug it - and if the code is at your edge of cleverness this can cause problems.$endgroup$
– user22048
May 27 '15 at 14:06
2
2
$begingroup$
Why define a
max
macro when there is already the safer std::max
in the standard library? :($endgroup$
– Morwenn
May 27 '15 at 14:07
$begingroup$
Why define a
max
macro when there is already the safer std::max
in the standard library? :($endgroup$
– Morwenn
May 27 '15 at 14:07
$begingroup$
Interesting. I wasn't aware of std::max. nor std::swap. I guess I need to look through the std libraries at some point. MichaelT, your point is well taken. However, I still feel that a strong understanding of operators should be an early fundamental for learning c/c++
$endgroup$
– mreff555
May 27 '15 at 17:12
$begingroup$
Interesting. I wasn't aware of std::max. nor std::swap. I guess I need to look through the std libraries at some point. MichaelT, your point is well taken. However, I still feel that a strong understanding of operators should be an early fundamental for learning c/c++
$endgroup$
– mreff555
May 27 '15 at 17:12
add a comment |
$begingroup$
Assuming you define min and max (or use std::min, std::max), you can do this:
int low, mid, high;
// Find the minimum of number 1 and the minimum of number 2 and number 3.
low = std::min(num1, std::min(num2, num3));
// Find the maximum of the minimum of number 1 and number 2 and the minimum of number 2 and number 3.
mid = std::max(std::min(num1, num2), std::min(num2, num3));
// Find the maximum of number 1 and the maximum of number 2 and number 3.
high = std::max(num1, std:max(num2, num3));
Which basically moves all of your if-else logic into standard library calls.
By the way, a popular method for switching numbers in place without a temporary integer goes as follows:
x = x ^ y;
y = y ^ x;
x = x ^ y;
Which replaces the need for a temp variable. Try it out and see.
if(a > b) {
a = a ^ b;
b = b ^ a;
a = a ^ b;
}
if(b > c) {
b = b ^ c;
c = c ^ b;
b = b ^ c;
}
if(a > b) {
a = a ^ b;
b = b ^ a;
a = a ^ b;
}
Of course, you could instead use std::swap. However, given all the alternatives, if performance was a non-issue (e.g. absolute clock cycle count was unimportant), I'd impress simply using std::min and std::max.
$endgroup$
add a comment |
$begingroup$
Assuming you define min and max (or use std::min, std::max), you can do this:
int low, mid, high;
// Find the minimum of number 1 and the minimum of number 2 and number 3.
low = std::min(num1, std::min(num2, num3));
// Find the maximum of the minimum of number 1 and number 2 and the minimum of number 2 and number 3.
mid = std::max(std::min(num1, num2), std::min(num2, num3));
// Find the maximum of number 1 and the maximum of number 2 and number 3.
high = std::max(num1, std:max(num2, num3));
Which basically moves all of your if-else logic into standard library calls.
By the way, a popular method for switching numbers in place without a temporary integer goes as follows:
x = x ^ y;
y = y ^ x;
x = x ^ y;
Which replaces the need for a temp variable. Try it out and see.
if(a > b) {
a = a ^ b;
b = b ^ a;
a = a ^ b;
}
if(b > c) {
b = b ^ c;
c = c ^ b;
b = b ^ c;
}
if(a > b) {
a = a ^ b;
b = b ^ a;
a = a ^ b;
}
Of course, you could instead use std::swap. However, given all the alternatives, if performance was a non-issue (e.g. absolute clock cycle count was unimportant), I'd impress simply using std::min and std::max.
$endgroup$
add a comment |
$begingroup$
Assuming you define min and max (or use std::min, std::max), you can do this:
int low, mid, high;
// Find the minimum of number 1 and the minimum of number 2 and number 3.
low = std::min(num1, std::min(num2, num3));
// Find the maximum of the minimum of number 1 and number 2 and the minimum of number 2 and number 3.
mid = std::max(std::min(num1, num2), std::min(num2, num3));
// Find the maximum of number 1 and the maximum of number 2 and number 3.
high = std::max(num1, std:max(num2, num3));
Which basically moves all of your if-else logic into standard library calls.
By the way, a popular method for switching numbers in place without a temporary integer goes as follows:
x = x ^ y;
y = y ^ x;
x = x ^ y;
Which replaces the need for a temp variable. Try it out and see.
if(a > b) {
a = a ^ b;
b = b ^ a;
a = a ^ b;
}
if(b > c) {
b = b ^ c;
c = c ^ b;
b = b ^ c;
}
if(a > b) {
a = a ^ b;
b = b ^ a;
a = a ^ b;
}
Of course, you could instead use std::swap. However, given all the alternatives, if performance was a non-issue (e.g. absolute clock cycle count was unimportant), I'd impress simply using std::min and std::max.
$endgroup$
Assuming you define min and max (or use std::min, std::max), you can do this:
int low, mid, high;
// Find the minimum of number 1 and the minimum of number 2 and number 3.
low = std::min(num1, std::min(num2, num3));
// Find the maximum of the minimum of number 1 and number 2 and the minimum of number 2 and number 3.
mid = std::max(std::min(num1, num2), std::min(num2, num3));
// Find the maximum of number 1 and the maximum of number 2 and number 3.
high = std::max(num1, std:max(num2, num3));
Which basically moves all of your if-else logic into standard library calls.
By the way, a popular method for switching numbers in place without a temporary integer goes as follows:
x = x ^ y;
y = y ^ x;
x = x ^ y;
Which replaces the need for a temp variable. Try it out and see.
if(a > b) {
a = a ^ b;
b = b ^ a;
a = a ^ b;
}
if(b > c) {
b = b ^ c;
c = c ^ b;
b = b ^ c;
}
if(a > b) {
a = a ^ b;
b = b ^ a;
a = a ^ b;
}
Of course, you could instead use std::swap. However, given all the alternatives, if performance was a non-issue (e.g. absolute clock cycle count was unimportant), I'd impress simply using std::min and std::max.
answered May 27 '15 at 21:27
phyrfoxphyrfox
54248
54248
add a comment |
add a comment |
$begingroup$
#include <iostream>
int min (int g, int a, int b){
if (g < a && g < b){
return g;
}
else if (a < g && a < b){
return a;
}
else if (b < a && b < g){
return b;
}
}
int max (int g, int a, int b){
if (g > a && g >> b){
return g;
}
else if(a > g && a > b)
{
return a;
}
else if (b > g && b > a)
{
return b;
}
}
int sort(int *g, int *a, int *b){
int small = min(*g ,*a, *b);
int big = max(*g, *a, *b);
int mid ;
if ((big != *g) && (small < *g)){
mid= *g;
}
if ((big != *a) && (small < *a)){
mid= *a;
}
if ((big != *b) && (small < *b)){
mid= *b;
}
std::cout << "*output :"<< std::endl;
std::cout << " small: " << small<< std::endl;
std::cout << " mid: " << mid<< std::endl;;
std::cout << " big: " << big<< std::endl;;
}
int main(){
int first,secnd,thered;
do {
std::cout << "*what numbers to sort?";
std::cout << std::endl;
std::cin >> first >>secnd >> thered ;
std::cout << std::endl;
if (first != 0) {
sort(&first, &secnd, &thered);
}
}while(first != 0);
}
New contributor
$endgroup$
$begingroup$
You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process.
$endgroup$
– Jamal♦
22 mins ago
add a comment |
$begingroup$
#include <iostream>
int min (int g, int a, int b){
if (g < a && g < b){
return g;
}
else if (a < g && a < b){
return a;
}
else if (b < a && b < g){
return b;
}
}
int max (int g, int a, int b){
if (g > a && g >> b){
return g;
}
else if(a > g && a > b)
{
return a;
}
else if (b > g && b > a)
{
return b;
}
}
int sort(int *g, int *a, int *b){
int small = min(*g ,*a, *b);
int big = max(*g, *a, *b);
int mid ;
if ((big != *g) && (small < *g)){
mid= *g;
}
if ((big != *a) && (small < *a)){
mid= *a;
}
if ((big != *b) && (small < *b)){
mid= *b;
}
std::cout << "*output :"<< std::endl;
std::cout << " small: " << small<< std::endl;
std::cout << " mid: " << mid<< std::endl;;
std::cout << " big: " << big<< std::endl;;
}
int main(){
int first,secnd,thered;
do {
std::cout << "*what numbers to sort?";
std::cout << std::endl;
std::cin >> first >>secnd >> thered ;
std::cout << std::endl;
if (first != 0) {
sort(&first, &secnd, &thered);
}
}while(first != 0);
}
New contributor
$endgroup$
$begingroup$
You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process.
$endgroup$
– Jamal♦
22 mins ago
add a comment |
$begingroup$
#include <iostream>
int min (int g, int a, int b){
if (g < a && g < b){
return g;
}
else if (a < g && a < b){
return a;
}
else if (b < a && b < g){
return b;
}
}
int max (int g, int a, int b){
if (g > a && g >> b){
return g;
}
else if(a > g && a > b)
{
return a;
}
else if (b > g && b > a)
{
return b;
}
}
int sort(int *g, int *a, int *b){
int small = min(*g ,*a, *b);
int big = max(*g, *a, *b);
int mid ;
if ((big != *g) && (small < *g)){
mid= *g;
}
if ((big != *a) && (small < *a)){
mid= *a;
}
if ((big != *b) && (small < *b)){
mid= *b;
}
std::cout << "*output :"<< std::endl;
std::cout << " small: " << small<< std::endl;
std::cout << " mid: " << mid<< std::endl;;
std::cout << " big: " << big<< std::endl;;
}
int main(){
int first,secnd,thered;
do {
std::cout << "*what numbers to sort?";
std::cout << std::endl;
std::cin >> first >>secnd >> thered ;
std::cout << std::endl;
if (first != 0) {
sort(&first, &secnd, &thered);
}
}while(first != 0);
}
New contributor
$endgroup$
#include <iostream>
int min (int g, int a, int b){
if (g < a && g < b){
return g;
}
else if (a < g && a < b){
return a;
}
else if (b < a && b < g){
return b;
}
}
int max (int g, int a, int b){
if (g > a && g >> b){
return g;
}
else if(a > g && a > b)
{
return a;
}
else if (b > g && b > a)
{
return b;
}
}
int sort(int *g, int *a, int *b){
int small = min(*g ,*a, *b);
int big = max(*g, *a, *b);
int mid ;
if ((big != *g) && (small < *g)){
mid= *g;
}
if ((big != *a) && (small < *a)){
mid= *a;
}
if ((big != *b) && (small < *b)){
mid= *b;
}
std::cout << "*output :"<< std::endl;
std::cout << " small: " << small<< std::endl;
std::cout << " mid: " << mid<< std::endl;;
std::cout << " big: " << big<< std::endl;;
}
int main(){
int first,secnd,thered;
do {
std::cout << "*what numbers to sort?";
std::cout << std::endl;
std::cin >> first >>secnd >> thered ;
std::cout << std::endl;
if (first != 0) {
sort(&first, &secnd, &thered);
}
}while(first != 0);
}
New contributor
New contributor
answered 25 mins ago
Subice45Subice45
1
1
New contributor
New contributor
$begingroup$
You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process.
$endgroup$
– Jamal♦
22 mins ago
add a comment |
$begingroup$
You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process.
$endgroup$
– Jamal♦
22 mins ago
$begingroup$
You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process.
$endgroup$
– Jamal♦
22 mins ago
$begingroup$
You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process.
$endgroup$
– Jamal♦
22 mins ago
add a comment |
protected by Jamal♦ 22 mins ago
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
5
$begingroup$
Why the requirement of only using
if
? You might discover new things by trying other tools too :)$endgroup$
– Morwenn
May 27 '15 at 9:54
2
$begingroup$
I still haven't reached that part of the book :)
$endgroup$
– liquid3
May 27 '15 at 9:58