2016-07-27 41 views
0

Ich frage mich, wie würde man den vorherigen Wert einer Variablen aufzeichnen, die sich ändert. Ein Beispiel für dieses Problem ist dieser Code unten:den Wert einer Variablen aufzeichnen

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

Der Code selbst wird nicht zurückgegeben "Ja! Es hat funktioniert!" Wie würde man den vorherigen Wert der Entfernung aufzeichnen und dann diesen vorherigen Wert mit dem aktuellen Wert vergleichen?

Edit: Danke für die schnellen Kommentare! Schätze es wirklich.

Um die tatsächliche Frage zu klären, ist der Code oben nur eine schnelle Vorlage/Beispiel. Da sich der Wert der Entfernung bei der zweiten Schleife ändert, wie würde man den ersten Wert der Entfernung AUFZEICHNEN und diesen Wert auf Prioritätswert setzen und dann den aktuellen Wert der Entfernung mit der Prioritätsentfernung vergleichen (dessen Wert tatsächlich nur der vorherige Wert der Entfernung ist) .

+0

Definieren der 'priordistance' Variable außerhalb des Schutzbereichs der' for' Schleife? –

+0

Sie rufen die Funktion "distanceFormula" mit den gleichen Argumenten zweimal auf, sodass das gleiche Ergebnis erzielt wird. Warum würde es dann "Ja!" Ausgeben? Es hat funktioniert! '? – Polb

Antwort

0

Einfach den vorherigen Wert in einer Variablen ändern.

#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

Es gibt ein paar Möglichkeiten, wie Sie dies tun könnten ... Sie könnten priordistance außerhalb Ihrer for-Schleife definieren, und stellen Sie sicher, dass Sie es nur einmal neu definieren (da Sie zweimal loopen).

Dies ist jedoch nicht das, was ich tun würde, würde ich einfach ein Array von ganzen Zahlen erzeugen, die eine Anzahl von n ‚Abstände‘ für Ihre Referenz durch den Index halten i

int[] or int * 
0

Sie verschiedene Dinge tun können. Sie müssen den Wert in Ihrer for Schleife beibehalten. Beachten Sie, dass es keinen Sinn hat zu vergleichen, bis Sie bereits einen vorherigen Wert erhalten haben.

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

Ich bin froh dich zu sehen :) –

+0

Und auch :-) – doctorlove

0

Meinst du etwas wie das Folgende?

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

der Programmausgabe ist

Yes! It worked! 
Verwandte Themen