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;










share|improve this question



















  • 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







  • 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














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;










share|improve this question



















  • 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







  • 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












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;










share|improve this question















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++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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




    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












  • 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







  • 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







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












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






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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

























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






    share|improve this answer


























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






      share|improve this answer
























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






        share|improve this answer














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







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 8 at 22:41

























        answered Nov 8 at 20:52









        user463035818

        15.7k42561




        15.7k42561



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            Crossroads (UK TV series)

            ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế