gpt4 book ai didi

c - C 中的 For 循环和数组

转载 作者:行者123 更新时间:2023-12-04 11:26:39 27 4
gpt4 key购买 nike

编辑:非常感谢(大家!)发表评论!在您的指导下,我能够修正胜率和不稳定的数组数字。但是,我还没有确定赢或输所需的平均投注次数。我更新了我的代码,现在在下面。

开始原始帖子:

提前感谢阅读本文或提供建议的任何人!

因此,我正在进行刻板的赌博,直到您赢了,否则编程类(class)的介绍就结束了。代码运行良好,除非我实际添加了正面或反面部分 - 然后我的包含赌注数量的数组被弄乱了。当我使用笔记隐藏实际的抛硬币部分时,数组工作正常。当我用抛硬币运行它时,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;
}

对不起,如果代码有点乱,我添加了一些东西来尝试并希望发送最新版本。

谢谢!

最佳答案

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 检测到您在第 75 行将未初始化的值传递给 printf

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并解决你的古怪数字问题。

关于c - C 中的 For 循环和数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39864749/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com