2017-02-26 5 views
1

配列内で最小の要素を見つける関数を作っています。私は変数sを参照渡しで修正しようとしています。私はC++のブランドです。私が正しく参照渡しをしているかどうかはわかりません。誰でもこれを行う正しい方法であることを確認することができますか、参照によって渡して最小値関数にアプローチするより良い方法を提案できますか?で、その後、あなたのパスを、その値を変更しないでしょう参照渡しの変数を変更する

int main() { 
    int s = 0;  
    // call your function 
    cout << s << endl; // Here you print 's', thus you confirm whether you are right or not 
} 

s場合:

#include <cstdlib> 
#include <stdlib.h> 
#include <iostream> 

using namespace std; 

int smallestElm(int numArray[], int length, int &smallest); 

int main() { 

    int n[3] = {2,5,3}; 
    int s = 0; 
    int length = 0; 

    cout << smallestElm(n, length, s) << endl; 
} 

int smallestElm(int numArray[], int length, int &smallest) { 
    smallest = numArray[0]; 
    length = sizeof (numArray)/sizeof (int); 
    for (int i = 1; i < length; i++) { 
     if (numArray[i] < smallest) { 
      smallest = numArray[i]; 
     } 
     cout << smallest << endl; 
     return 0; 

    } 
} 
+1

std :: min [http://en.cppreference.com/w/cpp/algorithm/min]について聞いたことがありますか? – ZivS

+2

なぜループの本体に戻るのですか? 2番目の要素を見た後に関数を終了します... – vu1p3n0x

+1

for-loop内に戻ると、最小要素を見つける前に戻ることがあります。 なぜ、isを使用せずに関数内で長さを再計算するときに、lengthパラメータを使用していますか? – PeterO

答えて

1

はい、これはあなたがこのようなあなたの主な機能を変更することで、自分で伝えることができるはずとして、正しいです参照は正しくありません(sは関数の本体内で値を変更するため)。機能については


、それはすべての要素をチェックする前に戻りますので、、間違っです!

#include <stdlib.h> 
#include <iostream> 

using namespace std; 

void smallestElm(int numArray[], size_t length, int &smallest); 

int main() { 

    int n[] = {2,5,3}; // size is not needed, it's automatically computed by the compiler 
    int s = 0; 
    size_t length = 3; 

    smallestElm(n, length, s); 
    cout << "smallest element = " << s << endl; 
    return 0; 
} 

void smallestElm(int numArray[], size_t length, int &smallest) { 
    smallest = numArray[0]; 
    for (int i = 1; i < length; i++) { 
     if (numArray[i] < smallest) { 
      smallest = numArray[i]; 
     } 
     cout << smallest << endl; 
    } 
} 

出力:

Georgioss-MacBook-Pro:~ gsamaras$ g++ -Wall main.cpp 
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out 
2 
2 
smallest element = 2 

がいることを忘れてはいけないので、最小の要素がある特定のために言って前に、配列のすべての要素をチェックするためにこのような何かにそれを変更

#include <algorithm> 
#include <iostream> 

using namespace std; 

int main() { 

    int n[] = {2,5,3}; 
    int *s = std::min_element(n, n + 3); // 3 size of the array 
    cout << "smallest element = " << *s << endl; 
    return 0; 
} 

出力:STLは、あなたがこのように使用できること、min_elementを提供

Georgioss-MacBook-Pro:~ gsamaras$ g++ -Wall main.cpp 
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out 
smallest element = 2 
+1

信じられないほど!このような詳細な対応をありがとうございます。私はこれで1トンを学んだ。 –

2

誰もがこの

はい、それは参照引数を宣言するための正しい方法で操作を行うための正しい方法であることを確認することができます。もちろん、参照でオブジェクトを変更することもできます。

または最小値関数に近づくためのより良い方法を提案...

良い方法は間違いなく代わりに引数を変更するのではなく、最小値を返すようになります。現在、この関数は常に0を返しますが、これは役に立たないようです。愚かな考えですが、あなたのアプローチは、参照渡しする正しい方法で参照

によってパスと

...。関数自体には複数のバグがあります。

  • 最初の反復の後にはいつも戻っているように見えるので、最初の2つの要素のうちの1つが常に「最小」になります。
  • int lengthの値は決して使用されません。使用前にオーバーライドされます。
  • sizeof (numArray)は、ポインティングされた配列のサイズには全く関係しないポインタnumArrayのサイズを返します。
  • この関数は常にnumArray[0]を使用するため、length == 0の場合は未定義の動作をします。
0

それはあなたのコードが正しいのですが、別の方法があります:以下のサンプルに示すように、関数の引数に、をint型変数のメモリのアドレスでこれを起動するには、ポインタを使用しては:

#include <stdlib.h> 
#include <iostream> 

using namespace std; 

void smallestElm(int numArray[], size_t length, int *smallest); 

int main() { 

int n[] = {2,5,3}; // size is not needed, it's automatically computed by the compiler 
int s = 0; 
size_t length = 3; 

smallestElm(n, length, &s); 
cout << "smallest element = " << s << endl; 
return 0; 
} 

void smallestElm(int numArray[], size_t length, int *smallest) { 
*smallest = numArray[0]; 
for (int i = 1; i < length; i++) { 
    if (numArray[i] < *smallest) { 
     *smallest = numArray[i]; 
    } 
    cout << *smallest << endl; 
} 
} 
関連する問題