2016-10-05 119 views
3

編輯:非常感謝你(大家!)爲您的意見!在您的指導下,我能夠解決勝利百分比和亂數陣列數。然而,我還沒有解決輸贏的平均投注數量。我更新了我的代碼,現在在下面。For循環和陣列在C

開始原帖:

謝謝你在前進的人誰讀取這個或建議達到了!

所以我正在做定型賭博,直到你贏了,或者它已經沒有了編程課程的介紹。代碼工作得很好,除非我實際上添加了頭部或尾部部分 - 那麼我的數組可以讓投注數量變得混亂。當我使用筆記來隱藏實際的投幣部分時,陣列工作正常。當我用擲硬幣來運行它時,betArray商店真的是不可思議的數字(負數,十億中的整數)。

這是迄今爲止代碼:

如果
#include<stdio.h> 
#include<stdlib.h> /*Enables use of rand()*/ 

int main() { 

    setvbuf(stdout, NULL, _IONBF, 0); /*Allow me to use repl.it*/ 

    /*Enter Gambler's name*/ 

    char gambleName[15] = ""; 
    printf("Enter gambler's name:\n"); 
    scanf("%s", gambleName); 
    printf("\nWelcome, "); 
    printf("%s! \n", gambleName); 

    /*Enter Stakes*/ 
    int availableFunds; 
    int goalFunds; 
    printf("We'll be betting $1 per bet. Enter your available funds:\n"); 
    scanf("%d", &availableFunds); /* Saves stakes as availableFunds */ 
    int seedVal=4; 
    srand(seedVal); 
    /*Butter the gamblers up*/ 
    if(availableFunds>=1) { 
     printf("%d? Wow, %s - that's a great start!\n",availableFunds, gambleName); 
     /*Enter Goal*/ 
     printf("How much do you want to win today? Enter a value up to 10000 with no commas, decimals or spaces:\n"); /*Saves goal as goalFunds*/ 
     scanf("%d",&goalFunds); 
     /*Recognize ambitious gamblers*/ 
     if (goalFunds > 10*availableFunds) { 
      printf("Wow, ambitious! Let's get started.\n"); 
     } 
     else { 
      printf("OK, let's get started!\n"); 
     } 
     printf("\n"); 
     /*begin gambling problem*/ 
     int betArray[1000]={0}; 
     int game = 0; 
     int bet=0; 
     float wins = 0; 
     int losses = 0; 
     for (game=0 ; game<1000; game++) { 
      if (availableFunds>0 && availableFunds<goalFunds) { 
       int toss = rand()%2; 
       bet+=1; 
       /*losing bet*/ 
       if (toss == 0) { 
        availableFunds -= 1; 
        losses += 1; 
       } 
       /*winning bet*/ 
       else { 
        availableFunds += 1; 
        wins += 1; 
       } 
       betArray[game+1] = bet; 
      } 
      else { 
       break; 
      } 
     } 
     int sumBet = 0; 
     for (game=0;game<1000;game++) { 
      sumBet+=betArray[game]; 
     } 
     float betAverage = sumBet/1000; 
     float winOutOfGames = wins/1000; 
     float winPercent = winOutOfGames*100; 
     /*print totals*/ 
     printf("%d games played with:\n",game); 
     printf("%.f goals reached\n",wins); 
     printf("%d down-and-out losses.\n",losses); 
     printf("You won ~%.1f%% of your games.\n",winPercent); 
     printf("On average, it took you %.f bets to win or go broke.\n",betAverage); 
     printf("\n"); 
     printf("Thanks for playing!\n"); 
     for (game = 1; game <= 50; game++) { 
      printf("Bets in game [%d] = %d\n",game,betArray[game]); 
     } 
    } 
    /* Send the broke guys packing*/ 
    else { 
     printf("$%d...? You may need to stop at an ATM... ¯\\_(ツ)_/¯ See you next  time!\n", availableFunds); 
    } 
    return 0; 
} 

對不起的代碼是有點亂,我加了一些東西,試圖想發送的最新版本。

謝謝!

+1

你使用過調試器嗎? – kaylum

+0

我感覺不好,但我不知道這意味着什麼。我在Mac OSX上,調試器上的任何建議? –

+1

裏面的if(availableFunds> 0 && availableFunds

回答

2

C編譯沒有任何警告,正如你的那樣,仍然可以包含許多內存問題,會導致奇怪和難以重現的行爲。要找到它們,請使用內存檢查程序,如Valgrind

用Valgrind的運行你的遊戲揭示了一個問題...

... 
Bets in game [27] = 28 
==27110== Conditional jump or move depends on uninitialised value(s) 
==27110== at 0x1001F08A7: __vfprintf (in /usr/lib/system/libsystem_c.dylib) 
==27110== by 0x1002166C0: __v2printf (in /usr/lib/system/libsystem_c.dylib) 
==27110== by 0x100216952: __xvprintf (in /usr/lib/system/libsystem_c.dylib) 
==27110== by 0x1001EC381: vfprintf_l (in /usr/lib/system/libsystem_c.dylib) 
==27110== by 0x1001EA21B: printf (in /usr/lib/system/libsystem_c.dylib) 
==27110== by 0x100000CAB: main (test.c:75) 
==27110== 
==27110== Conditional jump or move depends on uninitialised value(s) 
==27110== at 0x1001F0E90: __ultoa (in /usr/lib/system/libsystem_c.dylib) 
==27110== by 0x1001EE364: __vfprintf (in /usr/lib/system/libsystem_c.dylib) 
==27110== by 0x1002166C0: __v2printf (in /usr/lib/system/libsystem_c.dylib) 
==27110== by 0x100216952: __xvprintf (in /usr/lib/system/libsystem_c.dylib) 
==27110== by 0x1001EC381: vfprintf_l (in /usr/lib/system/libsystem_c.dylib) 
==27110== by 0x1001EA21B: printf (in /usr/lib/system/libsystem_c.dylib) 
==27110== by 0x100000CAB: main (test.c:75) 
==27110== 
==27110== Syscall param write(buf) points to uninitialised byte(s) 
==27110== at 0x1002F7612: write$NOCANCEL (in /usr/lib/system/libsystem_kernel.dylib) 
==27110== by 0x1001EB1F9: _swrite (in /usr/lib/system/libsystem_c.dylib) 
==27110== by 0x1001E3724: __sflush (in /usr/lib/system/libsystem_c.dylib) 
==27110== by 0x100216966: __xvprintf (in /usr/lib/system/libsystem_c.dylib) 
==27110== by 0x1001EC381: vfprintf_l (in /usr/lib/system/libsystem_c.dylib) 
==27110== by 0x1001EA21B: printf (in /usr/lib/system/libsystem_c.dylib) 
==27110== by 0x100000CAB: main (test.c:75) 
==27110== Address 0x104800e14 is on thread 1's stack 
==27110== in frame #3, created by __xvprintf (???:) 
==27110== 
Bets in game [28] = 0 
... 

這些都是堆棧跟蹤。你通常從下往上閱讀它們來找出問題的根源在哪裏。 Valgrind的檢測到你傳遞一個未初始化值printf上線75

test.c的線74-76此:

for (game = 0; game < 50; game++) { 
    printf("Bets in game [%d] = %d\n",game,betArray[game]); 
} 

問題是betArray。看看它初始化的位置就會發現問題。

int betArray[1000]; 

這隻分配內存,但它不會初始化它。 betArray包含發生在該內存位置的任何垃圾。因此你的數字。你必須初始化它。以下for循環應該設置每個元素,但它不會。

for (game=0 ; game<1000 ; game++) { 
    if (availableFunds>0 && availableFunds<goalFunds) { 
     ...blah blah blah... 
     betArray[game] = bet; 
    } 
    else if (availableFunds == 0 || availableFunds >= goalFunds) { 
    } 
} 

但你for僅環有時betArray初始化。其他時間沒有。看起來你打算填寫更多的代碼,但決定嘗試一下。所以你只剩下部分初始化的betArray

簡單的解決方案是在聲明它時立即初始化betArray

int betArray[1000] = {0}; 

This will initialize all elements to 0並修復你的數字問題。

1

正如您在對問題的評論中提到的那樣,目前的問題似乎是整數除法。在C中,當你除以兩個整數時,例如,489/1000 = 0,但是489 % 1000 = 489給出餘數。當在C中使用整數類型時,/是整數除法,並且%是模數運算符。您嘗試分隔兩個ints這裏:

int winRatio = wins/game; 

所以winRatio將是0,除非玩家贏得每場比賽,你應該改變winRatiofloat,你可能想winPercent是(或更多的比賽比的玩法!)浮子:

float winRatio = (1.0 * wins)/game; 
float winPercent = winRatio * 100; 

在第一條語句,而winsgameint s時,表達(1.0 * wins)float,因爲乘法瓦特的ith float文字1.0。出於同樣的原因,整個表達式的類型爲float,並且該值可以分配給float變量winRatio。這被稱爲「類型強制」或「類型轉換」。由於winRatiofloat,因此表達式winRatio * 100的值也是float,可以指定爲float winPercent

你不計算betAverage,我不確定你在這裏計算什麼。最後,正如在評論中也指出的那樣,bet隨着每場比賽而遞增,但每場比賽的支出爲1美元。您可能希望保持每場1美元的賭注,但也許您想增加支出以跟上增加的賭注。

+0

這幫了很多,謝謝。我認爲在我的第一個if(availableFunds> 0 && availableFunds