-3

I am fairly new to programming and I wanted to see if this was an efficient way to write a program to solve for lowest number out of 3 using a function. I could not think of more efficient way with what I have learned thus far.

#include <iostream>
using namespace std;

float SmallestNum( float a, float b, float c)
{

if ((a <= c) && (a <= b))
{
      float min;
      min = a;
      return min;
}
else if ((b <= c) && (b <= a))
{
     float min;
     min = b;
     return min;
}
else if ((c <= b) && (c <= a)) 
{
     float min;
     min = c;
     return min;
}
}


int main()
{

float a, b, c, z;



cout << "Please enter 3 seperate numbers :  " << endl << endl;
cout << "1st: ";
cin >> a;
cout << "2nd: ";
cin >> b;
cout << "3rd: ";
cin >> c;
cout << endl << endl;

z = SmallestNum(a,b,c);

cout << z << " is your lowest number" << endl;
cout << endl;

system("pause");
return 0;
}
  • 2
    If you are after a review [SE CodeReview](http://codereview.stackexchange.com/) might be a better place to post this question. – πάντα ῥεῖ Mar 22 '15 at 10:26
  • 1
    Why you create `min` and then return it, if you can just return `a`, `b` or `c`? –  Mar 22 '15 at 10:27
  • Usually you'd search for the min/max in an array. Then the code becomes shorter and less redundant. – runDOSrun Mar 22 '15 at 10:32
  • @Fireho Good point. Thanks. I think I am on wrong site for my experience. What site do I go to get help as a beginner when I run into problems? –  Mar 22 '15 at 10:32
  • 1
    @ArunA.S I don't think so, he is returning it by value(a copy of `min`), not by reference. –  Mar 22 '15 at 10:33
  • best way to teach all this stuff to myself? recommend a book? –  Mar 22 '15 at 10:37
  • Bjarne Stroustrup's beginners book? I have 3 months to try to master C++ –  Mar 22 '15 at 10:39
  • **Books:** https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Galik Mar 22 '15 at 10:39
  • sorry @Fireho , I was careless. I've deleted my comment. Thanks for correcting me. – Arun A S Mar 22 '15 at 10:44
  • @ThanksForAskin The book by Stroustrup is not a good book for beginners. You will spend much time but your knowledge will be close to zero.:) – Vlad from Moscow Mar 22 '15 at 10:52
  • @VladfromMoscow any recommendation? –  Mar 22 '15 at 11:03
  • @ThanksForAskin The problem is that I do not read books for beginners. So I can not advice one. – Vlad from Moscow Mar 22 '15 at 11:06

1 Answers1

1

In my opinion the most readable way is to use standard algorithms. For example

#include <iostream>
#include <algorithm>
#include <cstdlib>

using namespace std;

int main()
{

    float a, b, c, z;

    cout << "Please enter 3 seperate numbers :  " << endl << endl;

    cout << "1st: ";
    cin >> a;

    cout << "2nd: ";
    cin >> b;

    cout << "3rd: ";
    cin >> c;

    cout << endl << endl;

    z = std::min( { a, b, c } );

    cout << z << " is your lowest number" << endl;
    cout << endl;

    system("pause");

    return 0;
}

If you want to write the function yourself then the code can look like

#include <iostream>
#include <cstdlib>

using namespace std;

float SmallestNum( float a, float b, float c)
{
    float min = a;

    if ( b < min ) min = b;

    if ( c < min ) min = c;

    return min;
}


int main()
{

    float a, b, c, z;

    cout << "Please enter 3 seperate numbers :  " << endl << endl;

    cout << "1st: ";
    cin >> a;

    cout << "2nd: ";
    cin >> b;

    cout << "3rd: ";
    cin >> c;

    cout << endl << endl;

    z = SmallestNum( a, b, c );

    cout << z << " is your lowest number" << endl;
    cout << endl;

    system("pause");

    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • That makes much more sense to me. I see you have a bunch of experience and must be very good with math. Is this skill that can be learned through practice or do some people have it and some people dont kind of thing? –  Mar 22 '15 at 10:54
  • @ThanksForAskin I do not know. I know only that who has that skill is unemployed and who has no that skill usually has a job.:) – Vlad from Moscow Mar 22 '15 at 11:01