2016-07-27 3 views
0

私は変わる変数の以前の値がどのように記録されるのだろうと思っていました。この問題の例は以下のコードです。変数の値を記録

int distanceFormula(int x1, int x2, int y1, int y2){ 
    int distance; 
    distance = sqrt(pow((x1-x2), 2) + pow((y1-y2), 2)); 
    return distance; 
} 

int main(){ 
    for(int i = 0; i < 2; i++){ 
    int x = rand() % 180; 
    int y = rand() % 180; 
    int x2 = rand() % 180; 
    int y2 = rand() % 180; 

    int distance = distanceFormula(x, x2, y, y2); 
    int priordistance = distanceFormula(x, x2, y, y2); 

    if(priordistance != distance){ 
     cout<<"Yes! It worked!"<<endl; 
    } 
    } 
    return 0; 
} 

コード自体は「はい!それは働いた!」と返されません。どのように距離の以前の値を記録し、その前の値と現在の値を比較するのでしょうか?

編集: ありがとうございました!心から感謝する。

実際の質問を明確にするために、上のコードは簡単なテンプレート/例です。距離の値は2番目のループの周りで変化するので、距離の最初の値をどのように記録し、その値を優先順位に設定し、距離の現在の値をpriordistance(その値は実際には距離の以前の値です) 。

+0

ですか? –

+0

同じ引数を持つ 'distanceFormula'関数を2回呼び出すので、同じ結果が得られます。それでは、なぜ「Yes!それは働いた! ' – Polb

答えて

0

以前の値を変数に書き直すだけです。

#include <cmath> 
#include <cstdlib> 
#include <iostream> 
using std::cout; 
using std::endl; 

int distanceFormula(int x1, int x2, int y1, int y2){ 
    int distance; 
    distance = sqrt(pow((x1-x2), 2) + pow((y1-y2), 2)); 
    return distance; 
} 

int main(){ 
    int priordistance = 0; // a variable used to record the value 
    for(int i = 0; i < 2; i++){ 
    int x = rand() % 180; 
    int y = rand() % 180; 
    int x2 = rand() % 180; 
    int y2 = rand() % 180; 

    int distance = distanceFormula(x, x2, y, y2); 
    if(i > 0 && priordistance != distance){ // use i > 0 to avoid compareing with meaningless value 
     cout<<"Yes! It worked!"<<endl; 
    } 
    priordistance = distance; // record the value 
    } 
    return 0; 
} 
0

これを行うにはいくつかの方法があります...あなたのforループの外側にpriordistanceを定義して、それを1回だけ再定義してください(2回ループしているので)。

しかし、これは私が単にインデックスで、あなたの参考のために「距離」のn個の数を保持する整数の配列を作成することになり、私はどうなるのかではありません、私は

int[] or int * 
0

あなたはいろいろなことを行うことができます。 forループ全体にわたって持続する価値が必要です。既に1つの事前値が得られるまで、比較のポイントはないことに注意してください。

int main(){ 
    int priordistance = 0; //lifetime outside for loop 
    for(int i = 0; i < 2; i++){ 
    int x = rand() % 180; 
    int y = rand() % 180; 
    int x2 = rand() % 180; 
    int y2 = rand() % 180; 

    int distance = distanceFormula(x, x2, y, y2); 

    if(i && priordistance != distance){ 
    //^---- have we got a prior value yet? 
     cout<<"Yes! It worked!"<<endl; 
    } 
    priordistance = distance;//remember for next time 
    } 
    return 0; 
} 
+0

私はあなたに会えてうれしいです:) –

+0

同様に:-) – doctorlove

0

あなたは次のような意味ですか?

#include <iostream> 
#include <cstdlib> 
#include <ctime> 
#include <cmath> 

int distanceFormula(int x1, int x2, int y1, int y2) 
{ 
    int distance; 

    distance = std::sqrt(std::pow(x1 - x2, 2) + std::pow(y1 - y2, 2)); 

    return distance; 
} 

int main() 
{ 
    const size_t N = 2; 
    int distance[N]; 

    std::srand((unsigned int)std::time(nullptr)); 

    for (size_t i = 0; i < N; i++) 
    { 
     int x = rand() % 180; 
     int y = rand() % 180; 
     int x2 = rand() % 180; 
     int y2 = rand() % 180; 

     distance[i] = distanceFormula(x, x2, y, y2); 
    } 

    if (distance[0] != distance[1]) std::cout << "Yes! It worked!" << std::endl; 

    return 0; 
} 

プログラムの出力は `for`ループの範囲外` priordistance`変数を定義

Yes! It worked! 
関連する問題