2017-09-17 3 views
-1

私の宿題はC++で、私の目標は3つの整数を入力して最小の数を返す関数に渡すことです。これはC++の第3週目なので、分かりません。C++関数定義のif/else文を使用して最小番号を返す

さらに、私は#include<iostream>using namespace stdで始めることができます。私はこれを何時間もしていましたが、これは私にとっては簡単ではありません。

#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){ 


} 

私はどのようにに困惑している:これは私がこれまでのところ、私は実際に理解していることをしているコードです...

を私は非常に多くの異なるものを試してみたと私はエラーのみを取得してきましたif/elseステートメントを使用して最小の番号を探し、最小の数値を関数に戻してそれを印刷する方法。

+1

何を試しましたか?ここでは、それを解決しようとするコードはないようです。具体的にどの部分に助けが必要ですか? – Carcigenicate

+0

if/elseステートメントの使い方を説明する多くの役に立つ情報は、[このリンクを参照](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) " –

答えて

-2

ここにあります。

#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; 
} 

この関数は3つの引数を受け入れる必要があります。したがって、3つのパラメータで宣言する必要があります。

あなたはelseステートメントを含める必要がある場合、この関数は、C++標準ライブラリがすでにヘッダ<algorithm>で宣言された適切なアルゴリズムstd::minを持っていることに

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

注意を払うように書き込むことができます。だからあなたはただ書くことができる

#include <algorithm> 

//... 

smallestNumber = std::min({ numberOne, numberTwo, numberThree }); 
+0

@Maxineそうしたものは、スタックオーバーフロー検索機能でグーグルで見つけられるか、見つけられます。これは、「C++関数の引数」に関するGoogleの最初の結果です。http://www.cplusplus.com/doc/tutorial/functions/ – m69

関連する問題