-1

For my homework assignment in C++, my goal is to write a program the inputs three integers and passes them to a function that returns the smallest number. This is only my 3rd week of C++, so I don't know much.

In addition, I'm only allowed to start off with #include<iostream> and using namespace std. I had been at this for hours,this just doesn't come easy to me. I've tried so many different things and I've only been getting errors...

This is the code I have so far that I actually understand:

#include <iostream>
using namespace std;

int fsmallestNumber(int);

int main() {
    int numberOne;
    int numberTwo;
    int numberThree;
    int smallestNumber;

    cout << "Enter in 3 numbers and I will find the smallest of all three" << endl;
    cin >> numberOne >> numberTwo >> numberThree;


    cout << "The smallest of all three numbers is " << smallestNumber << endl;

}

int fsmallestNumber(int sn){


}

I'm confused on how to use the if/else statements to find the smallest number, and how to return the smallest number back into the function to print it out.

Maxine
  • 11
  • 3
  • 1
    What have you tried? There doesn't seem to be any code here that attempts to solve it. What part specifically do you need help with? – Carcigenicate Sep 17 '17 at 23:05
  • [See this link](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) for a lot of helpful information that explain "how to use the if/else statements". – Sam Varshavchik Sep 17 '17 at 23:48

1 Answers1

-2

Here you are.

#include <iostream>

using namespace std;

int fsmallestNumber( int, int, int );

int main() 
{
    int numberOne;
    int numberTwo;
    int numberThree;
    int smallestNumber;

    cout << "Enter in 3 numbers and I will find the smallest of all three" << endl;
    cin >> numberOne >> numberTwo >> numberThree;

    smallestNumber = fsmallestNumber( numberOne, numberTwo, numberThree );

    cout << "The smallest of all three numbers is " << smallestNumber << endl;
}

int fsmallestNumber( int x, int y, int z )
{
    int smallest = x;

    if ( y < smallest ) smallest = y;
    if ( z < smallest ) smallest = z;

    return smallest;
}

The function has to accept three arguments. So it must be declared with three parameters.

If you need to include an else statement then the function can be written like

int fsmallestNumber( int x, int y, int z )
{
    int smallest;

    if ( not ( y < x || z < x ) )
    {
        smallest = x;
    }
    else if ( not ( z < y ) )
    {
        smallest = y;
    }
    else
    {
        smallest = z;
    }

    return smallest;
}

Pay attention to that the C++ standard library already has suitable algorithm std::min declared in the header <algorithm>. So you could just write

#include <algorithm>

//...

smallestNumber = std::min( { numberOne, numberTwo, numberThree } );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • @Maxine Such things can be Googled or found with the Stack Overflow search function. This is the first Google result for "c++ function arguments": http://www.cplusplus.com/doc/tutorial/functions/ – m69's been on strike for years Sep 18 '17 at 02:21