Sorting numbers from largest to smallest using a function of if else statements
up vote
0
down vote
favorite
To preface this I'd like to say that THIS IS NOT HOMEWORK. I am writing this code to practice skills and learn different techniques. The challenge of this specific code is to have 3 values returned to the user using a function and if else statements only. I have successfully made what I believe to be a working function. However, my int main() outputs a zero instead of the expected 6, 12, 16 answer. Can anyone give me pointers as to where I am going wrong?
#include <iostream>
using namespace std;
int sort2(int a, int b, int c)
if (a>b && a>c)
int value1 = a;
if (b>c)
int value2 = b;
int value3 = c;
else
int value2 = c;
int value3 = b;
if (b>a && b>c)
int value1 = b;
if (a>c)
int value2 = a;
int value3 = c;
else
int value2 = c;
int value3 = a;
if (c>a && c>b)
int value1 = c;
if(a>b)
int value2 = a;
int value3 = b;
else
int value2 = b;
int value3 = a;
int main()
int result = sort2(12,16,6);
cout<<result;
c++
|
show 8 more comments
up vote
0
down vote
favorite
To preface this I'd like to say that THIS IS NOT HOMEWORK. I am writing this code to practice skills and learn different techniques. The challenge of this specific code is to have 3 values returned to the user using a function and if else statements only. I have successfully made what I believe to be a working function. However, my int main() outputs a zero instead of the expected 6, 12, 16 answer. Can anyone give me pointers as to where I am going wrong?
#include <iostream>
using namespace std;
int sort2(int a, int b, int c)
if (a>b && a>c)
int value1 = a;
if (b>c)
int value2 = b;
int value3 = c;
else
int value2 = c;
int value3 = b;
if (b>a && b>c)
int value1 = b;
if (a>c)
int value2 = a;
int value3 = c;
else
int value2 = c;
int value3 = a;
if (c>a && c>b)
int value1 = c;
if(a>b)
int value2 = a;
int value3 = b;
else
int value2 = b;
int value3 = a;
int main()
int result = sort2(12,16,6);
cout<<result;
c++
6
For one, your function returns a singleint
. Why do you expect it to print 3 values? Second you never actually return anything fromsort2
so you have undefined behavior. Except formain
, the execution of function with a non-void
return type must encounter areturn
before it reaches the end of the function.
– François Andrieux
Nov 8 at 20:09
5
Regarding the point of not returning anything, compilers warn about this. If your compiler isn't giving a warning, it needs more warning options enabled.
– chris
Nov 8 at 20:10
1
I can see no value in this rather clumsy code. Modern C has functions for this. For multiple return values you define your prototype as int sort2(int &a, int &b, int &c) and set them correspondingly in the function. In main you do cout << a << " " << b << " " << c;
– peakpeak
Nov 8 at 20:20
2
I know you are just practicing stuff, but, in most real-life situations you should just be callingstd::sort
rather than write all this code.
– Jesper Juhl
Nov 8 at 20:21
2
why does it matter if this is homework or not? In any case you should grab a book or two
– user463035818
Nov 8 at 20:23
|
show 8 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
To preface this I'd like to say that THIS IS NOT HOMEWORK. I am writing this code to practice skills and learn different techniques. The challenge of this specific code is to have 3 values returned to the user using a function and if else statements only. I have successfully made what I believe to be a working function. However, my int main() outputs a zero instead of the expected 6, 12, 16 answer. Can anyone give me pointers as to where I am going wrong?
#include <iostream>
using namespace std;
int sort2(int a, int b, int c)
if (a>b && a>c)
int value1 = a;
if (b>c)
int value2 = b;
int value3 = c;
else
int value2 = c;
int value3 = b;
if (b>a && b>c)
int value1 = b;
if (a>c)
int value2 = a;
int value3 = c;
else
int value2 = c;
int value3 = a;
if (c>a && c>b)
int value1 = c;
if(a>b)
int value2 = a;
int value3 = b;
else
int value2 = b;
int value3 = a;
int main()
int result = sort2(12,16,6);
cout<<result;
c++
To preface this I'd like to say that THIS IS NOT HOMEWORK. I am writing this code to practice skills and learn different techniques. The challenge of this specific code is to have 3 values returned to the user using a function and if else statements only. I have successfully made what I believe to be a working function. However, my int main() outputs a zero instead of the expected 6, 12, 16 answer. Can anyone give me pointers as to where I am going wrong?
#include <iostream>
using namespace std;
int sort2(int a, int b, int c)
if (a>b && a>c)
int value1 = a;
if (b>c)
int value2 = b;
int value3 = c;
else
int value2 = c;
int value3 = b;
if (b>a && b>c)
int value1 = b;
if (a>c)
int value2 = a;
int value3 = c;
else
int value2 = c;
int value3 = a;
if (c>a && c>b)
int value1 = c;
if(a>b)
int value2 = a;
int value3 = b;
else
int value2 = b;
int value3 = a;
int main()
int result = sort2(12,16,6);
cout<<result;
c++
c++
edited Nov 8 at 21:30
Ted Lyngmo
932112
932112
asked Nov 8 at 20:07
Mason penguin Holder
203
203
6
For one, your function returns a singleint
. Why do you expect it to print 3 values? Second you never actually return anything fromsort2
so you have undefined behavior. Except formain
, the execution of function with a non-void
return type must encounter areturn
before it reaches the end of the function.
– François Andrieux
Nov 8 at 20:09
5
Regarding the point of not returning anything, compilers warn about this. If your compiler isn't giving a warning, it needs more warning options enabled.
– chris
Nov 8 at 20:10
1
I can see no value in this rather clumsy code. Modern C has functions for this. For multiple return values you define your prototype as int sort2(int &a, int &b, int &c) and set them correspondingly in the function. In main you do cout << a << " " << b << " " << c;
– peakpeak
Nov 8 at 20:20
2
I know you are just practicing stuff, but, in most real-life situations you should just be callingstd::sort
rather than write all this code.
– Jesper Juhl
Nov 8 at 20:21
2
why does it matter if this is homework or not? In any case you should grab a book or two
– user463035818
Nov 8 at 20:23
|
show 8 more comments
6
For one, your function returns a singleint
. Why do you expect it to print 3 values? Second you never actually return anything fromsort2
so you have undefined behavior. Except formain
, the execution of function with a non-void
return type must encounter areturn
before it reaches the end of the function.
– François Andrieux
Nov 8 at 20:09
5
Regarding the point of not returning anything, compilers warn about this. If your compiler isn't giving a warning, it needs more warning options enabled.
– chris
Nov 8 at 20:10
1
I can see no value in this rather clumsy code. Modern C has functions for this. For multiple return values you define your prototype as int sort2(int &a, int &b, int &c) and set them correspondingly in the function. In main you do cout << a << " " << b << " " << c;
– peakpeak
Nov 8 at 20:20
2
I know you are just practicing stuff, but, in most real-life situations you should just be callingstd::sort
rather than write all this code.
– Jesper Juhl
Nov 8 at 20:21
2
why does it matter if this is homework or not? In any case you should grab a book or two
– user463035818
Nov 8 at 20:23
6
6
For one, your function returns a single
int
. Why do you expect it to print 3 values? Second you never actually return anything from sort2
so you have undefined behavior. Except for main
, the execution of function with a non-void
return type must encounter a return
before it reaches the end of the function.– François Andrieux
Nov 8 at 20:09
For one, your function returns a single
int
. Why do you expect it to print 3 values? Second you never actually return anything from sort2
so you have undefined behavior. Except for main
, the execution of function with a non-void
return type must encounter a return
before it reaches the end of the function.– François Andrieux
Nov 8 at 20:09
5
5
Regarding the point of not returning anything, compilers warn about this. If your compiler isn't giving a warning, it needs more warning options enabled.
– chris
Nov 8 at 20:10
Regarding the point of not returning anything, compilers warn about this. If your compiler isn't giving a warning, it needs more warning options enabled.
– chris
Nov 8 at 20:10
1
1
I can see no value in this rather clumsy code. Modern C has functions for this. For multiple return values you define your prototype as int sort2(int &a, int &b, int &c) and set them correspondingly in the function. In main you do cout << a << " " << b << " " << c;
– peakpeak
Nov 8 at 20:20
I can see no value in this rather clumsy code. Modern C has functions for this. For multiple return values you define your prototype as int sort2(int &a, int &b, int &c) and set them correspondingly in the function. In main you do cout << a << " " << b << " " << c;
– peakpeak
Nov 8 at 20:20
2
2
I know you are just practicing stuff, but, in most real-life situations you should just be calling
std::sort
rather than write all this code.– Jesper Juhl
Nov 8 at 20:21
I know you are just practicing stuff, but, in most real-life situations you should just be calling
std::sort
rather than write all this code.– Jesper Juhl
Nov 8 at 20:21
2
2
why does it matter if this is homework or not? In any case you should grab a book or two
– user463035818
Nov 8 at 20:23
why does it matter if this is homework or not? In any case you should grab a book or two
– user463035818
Nov 8 at 20:23
|
show 8 more comments
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
You are actually not returning anything from your function. Compilers can warn about this (not always), turn up your warning levels. Moreover your function is declared to return a single integer. And then, for example here:
if (b>c)
int value2 = b;
int value3 = c;
you declare two variables that are local to the if block. Those statements have no effect whatsoever. To be honest the best advice I can give is to grab a book or two. Anyhow...
The challenge of this specific code is to have 3 values returned to
the user using a function and if else statements only.
I understand that you want to practice if else statements and writing a function. This alone is unfortunately not sufficient to do what you want, but you can make your code work with minimal changes by returning a structure that contains the 3 values:
struct sorted_3_values
int value1;
int value2;
int value3;
;
And then change your function to
sorted_3_values sort2(int a, int b, int c)
sorted_3_values result;
if ( ... )
result.value1 = ...;
result.value2 = ...;
result.value3 = ...;
return result;
And then in main
sorted_3_values x = sort2(12,16,6);
std::cout << x.value1 << " " << x.value2 << " " << x.value3;
Be aware that if you write code like this, then you are not really using c++. At some point you should take a look at containers (eg std::vector
) and algorithms (std::find
).
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You are actually not returning anything from your function. Compilers can warn about this (not always), turn up your warning levels. Moreover your function is declared to return a single integer. And then, for example here:
if (b>c)
int value2 = b;
int value3 = c;
you declare two variables that are local to the if block. Those statements have no effect whatsoever. To be honest the best advice I can give is to grab a book or two. Anyhow...
The challenge of this specific code is to have 3 values returned to
the user using a function and if else statements only.
I understand that you want to practice if else statements and writing a function. This alone is unfortunately not sufficient to do what you want, but you can make your code work with minimal changes by returning a structure that contains the 3 values:
struct sorted_3_values
int value1;
int value2;
int value3;
;
And then change your function to
sorted_3_values sort2(int a, int b, int c)
sorted_3_values result;
if ( ... )
result.value1 = ...;
result.value2 = ...;
result.value3 = ...;
return result;
And then in main
sorted_3_values x = sort2(12,16,6);
std::cout << x.value1 << " " << x.value2 << " " << x.value3;
Be aware that if you write code like this, then you are not really using c++. At some point you should take a look at containers (eg std::vector
) and algorithms (std::find
).
add a comment |
up vote
1
down vote
accepted
You are actually not returning anything from your function. Compilers can warn about this (not always), turn up your warning levels. Moreover your function is declared to return a single integer. And then, for example here:
if (b>c)
int value2 = b;
int value3 = c;
you declare two variables that are local to the if block. Those statements have no effect whatsoever. To be honest the best advice I can give is to grab a book or two. Anyhow...
The challenge of this specific code is to have 3 values returned to
the user using a function and if else statements only.
I understand that you want to practice if else statements and writing a function. This alone is unfortunately not sufficient to do what you want, but you can make your code work with minimal changes by returning a structure that contains the 3 values:
struct sorted_3_values
int value1;
int value2;
int value3;
;
And then change your function to
sorted_3_values sort2(int a, int b, int c)
sorted_3_values result;
if ( ... )
result.value1 = ...;
result.value2 = ...;
result.value3 = ...;
return result;
And then in main
sorted_3_values x = sort2(12,16,6);
std::cout << x.value1 << " " << x.value2 << " " << x.value3;
Be aware that if you write code like this, then you are not really using c++. At some point you should take a look at containers (eg std::vector
) and algorithms (std::find
).
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You are actually not returning anything from your function. Compilers can warn about this (not always), turn up your warning levels. Moreover your function is declared to return a single integer. And then, for example here:
if (b>c)
int value2 = b;
int value3 = c;
you declare two variables that are local to the if block. Those statements have no effect whatsoever. To be honest the best advice I can give is to grab a book or two. Anyhow...
The challenge of this specific code is to have 3 values returned to
the user using a function and if else statements only.
I understand that you want to practice if else statements and writing a function. This alone is unfortunately not sufficient to do what you want, but you can make your code work with minimal changes by returning a structure that contains the 3 values:
struct sorted_3_values
int value1;
int value2;
int value3;
;
And then change your function to
sorted_3_values sort2(int a, int b, int c)
sorted_3_values result;
if ( ... )
result.value1 = ...;
result.value2 = ...;
result.value3 = ...;
return result;
And then in main
sorted_3_values x = sort2(12,16,6);
std::cout << x.value1 << " " << x.value2 << " " << x.value3;
Be aware that if you write code like this, then you are not really using c++. At some point you should take a look at containers (eg std::vector
) and algorithms (std::find
).
You are actually not returning anything from your function. Compilers can warn about this (not always), turn up your warning levels. Moreover your function is declared to return a single integer. And then, for example here:
if (b>c)
int value2 = b;
int value3 = c;
you declare two variables that are local to the if block. Those statements have no effect whatsoever. To be honest the best advice I can give is to grab a book or two. Anyhow...
The challenge of this specific code is to have 3 values returned to
the user using a function and if else statements only.
I understand that you want to practice if else statements and writing a function. This alone is unfortunately not sufficient to do what you want, but you can make your code work with minimal changes by returning a structure that contains the 3 values:
struct sorted_3_values
int value1;
int value2;
int value3;
;
And then change your function to
sorted_3_values sort2(int a, int b, int c)
sorted_3_values result;
if ( ... )
result.value1 = ...;
result.value2 = ...;
result.value3 = ...;
return result;
And then in main
sorted_3_values x = sort2(12,16,6);
std::cout << x.value1 << " " << x.value2 << " " << x.value3;
Be aware that if you write code like this, then you are not really using c++. At some point you should take a look at containers (eg std::vector
) and algorithms (std::find
).
edited Nov 8 at 22:41
answered Nov 8 at 20:52
user463035818
15.7k42561
15.7k42561
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53215370%2fsorting-numbers-from-largest-to-smallest-using-a-function-of-if-else-statements%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
6
For one, your function returns a single
int
. Why do you expect it to print 3 values? Second you never actually return anything fromsort2
so you have undefined behavior. Except formain
, the execution of function with a non-void
return type must encounter areturn
before it reaches the end of the function.– François Andrieux
Nov 8 at 20:09
5
Regarding the point of not returning anything, compilers warn about this. If your compiler isn't giving a warning, it needs more warning options enabled.
– chris
Nov 8 at 20:10
1
I can see no value in this rather clumsy code. Modern C has functions for this. For multiple return values you define your prototype as int sort2(int &a, int &b, int &c) and set them correspondingly in the function. In main you do cout << a << " " << b << " " << c;
– peakpeak
Nov 8 at 20:20
2
I know you are just practicing stuff, but, in most real-life situations you should just be calling
std::sort
rather than write all this code.– Jesper Juhl
Nov 8 at 20:21
2
why does it matter if this is homework or not? In any case you should grab a book or two
– user463035818
Nov 8 at 20:23