-1

このコードの問題は、beetleSimulation の while ループにあります。x/yCount が範囲外になると終了せずに永遠に続きます。x と y は 20 をはるかに超えています。だれか助けてもらえますか?

      #include <stdio.h>
        #include <stdlib.h>
        #include <math.h>
        #define PI 3.14159265
        void beetleSimulation(int, int)

;


    int main ( int argc, char *argv[] )
    {
        if ( argc != 2 ) // argc should be 2 for correct execution 
        {
            // If the number of arguments is not 2
            printf("%d", argc);
        }
        else 
        {
           //run the bee

シミュレーションのファイル beetleSimulation(argv[1], argv[2] ); } }

void beetleSimulation(int size, int iterations){
    int i;
    int xCount = 0;
    int yCount = 0;
    int timeCount = 0;
    int overallCount = 0;
    for(i=0; i < 10; i++){
        while(xCount < 20 || xCount > -20 || yCount <20 || yCount >-20){
            timeCount += 1;
            int degree = rand() % 360;
            double radian = degree / 180 * PI;
            xCount += sin(radian);
            yCount += cos(radian);
        }

        //when beetle has died, add time it took to overall count, then go through for loop again
        overallCount += timeCount;
    }
    //calculate average time
    double averageTime = overallCount/iterations;
    printf("%d",averageTime);
}
4

2 に答える 2

0

を使用しているコードによる無限ループ||が必要&&です。
xCount < 20 || xCount > -20常に真です

// while(xCount < 20 || xCount > -20 || yCount <20 || yCount >-20){
while(xCount < 20 && xCount > -20 && yCount <20 && yCount >-20){
于 2015-02-03T01:09:42.517 に答える
0
  • beetleSimulation を宣言する必要があります

  • beetleSimulation 定義に変数の型がありません

  • あなたのprintfステートメントは間違っています。%d を使用して引用符で囲む必要があります。

  • メインはリターンなし

  • また、ロジック内で変数を宣言します。関数の先頭でそれを行う必要があります (ANSI)

  • メインでは、2 つの引数が argv[0] と argv[1] であるため、a.out NUMBER

beetlesimulation 関数には 2 つの入力が必要なので、3 つの引数が必要です。

  • while ループで変数を再定義していますが、まったく意味がありません。

http://pastebin.com/RuuVDJdp

これはあなたのコードのコンパイルバージョンですが、無限ループを実行しているように見えますが、コンパイルされます。

于 2015-02-02T23:57:34.463 に答える