1

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;
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • 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 08 '18 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 08 '18 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;* –  Nov 08 '18 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 08 '18 at 20:21
  • 2
    why does it matter if this is homework or not? In any case you should grab a [book or two](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – 463035818_is_not_an_ai Nov 08 '18 at 20:23
  • 1
    I feel sorry for all those mayfly variables. No sooner are they given a value than _boom_, out of scope, dead. – Tim Randall Nov 08 '18 at 20:27
  • @user463035818 it matters because people should not be providing answers to homework problems, however if I'm just practicing on my own people shouldn't worry about posting corrected code or giving more in depth answers. – Mason penguin Holder Nov 08 '18 at 20:28
  • the problem with poor homework questions is that they simply ask for the code to be written by someone else and too often dont clearly specify the constraints (which can be really weird for homework assignments), neither is the case for this question – 463035818_is_not_an_ai Nov 08 '18 at 20:31
  • Aside from the missing `return` and variable scope, consider the case when `a == b == c`. None of your outer `if` blocks will succeed. – TypeIA Nov 08 '18 at 20:33
  • Where do you return the 3 sorted values? – Thomas Matthews Nov 08 '18 at 20:49
  • Have you learned `pass by reference` or `std::vector`? – Thomas Matthews Nov 08 '18 at 20:50
  • @TypeIA I guess you meant `a == b && a == c` – 463035818_is_not_an_ai Nov 08 '18 at 21:46
  • @user463035818 I meant what I wrote, but I didn't intend for it to be interpreted as a C or C++ expression - maybe I shouldn't have put it in `monospace`. – TypeIA Nov 08 '18 at 22:00

1 Answers1

1

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

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185