因此,我正在为class的最终项目编写以下代码。我使用onlinegdb作为编译器,因为我无法让visual basic运行c。程序运行时没有任何错误,但在我将输入放入程序后,从未打印avghits的输出。代码如下:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
int attackingUnitSize, skill, strength, armorPenetration, attacks, damage= 0;
int defendingUnitSize, toughness, save, invulnerableSave = 0;
int woundRoll, saveRoll, simulations = 0;
int hitSimulations, woundSimulations, saveSimulations = 0;
int dice, successfulHits, successfulWounds, successfulSaves = 0;
int hitDiceResults[hitSimulations], woundDiceResults[successfulHits];
int saveDiceResults[successfulWounds];
int hitOnes, hitTwos, hitThrees, hitFours, hitFives, hitSixes = 0;
int woundOnes, woundTwos, woundThrees, woundFours, woundFives, woundSixes = 0;
int saveOnes, saveTwos, saveThrees, saveFours, saveFives, saveSixes = 0;
double avgHits, avgWounds, avgSaves = 0;
srand(time(0));
printf("This program will ask for characteristics from warhammer 40k and ");
printf("ask how many simulations you would like to run.\nIt will output the");
printf(" average amount of successful hit rolls, wound rolls, damage and ");
printf("save rolls made.\n");
/*States that attacking units characteristics will be entered first*/
printf("Enter the attacking units characteristics.\n");
/*Gets unit size*/
printf("Enter the units size: ");
scanf("%d", &attackingUnitSize);
/*Gets weapon/ballistic skill*/
printf("Enter units weapon skill(WS) or ballistic skill(BS): ");
scanf("%d", &skill);
/*Gets units strength*/
printf("Enter units strength(S): ");
scanf("%d", &strength);
/*Gets armor penetration*/
printf("Enter units armor penetration(AP): ");
scanf("%d", &armorPenetration);
/*Gets number of attacks*/
printf("Enter number of attacks: ");
scanf("%d", &attacks);
/*Gets damage*/
printf("Enter the damage of the weapon: ");
scanf("%d", &damage);
/*States that defending units characteristics will be entered now*/
printf("Enter the defending units characteristics.\n");
/*Gets defending unit size*/
printf("Enter units size: ");
scanf("%d", &defendingUnitSize);
/*Gets defending units toughness*/
printf("Enter defending units toughness: ");
scanf("%d", &toughness);
/*Gets defending units save*/
printf("Enter units Save: ");
scanf("%d", &save);
/*Gets defending units invulnerable save, if applicable*/
printf("Enter defending units invulnerable save(if none, enter: 0): ");
scanf("%d", &invulnerableSave);
/*Gets number of simulations*/
printf("How many simulations would you like to be done? ");
scanf("%d", &simulations);
/*Determines the lowest value for the wound roll*/
if (strength >= (toughness * 2)){
woundRoll = 2;
}
else if ((strength > toughness) && (strength < ((toughness * 2)))){
woundRoll = 3;
}
else if (strength == toughness){
woundRoll = 4;
}
else if ((strength < toughness) && ((strength * 2) > toughness)){
woundRoll = 5;
}
else{
woundRoll = 6;
}
/* Determines which save characterist to use */
save += armorPenetration;
if (invulnerableSave != 0){
if (save < invulnerableSave){
saveRoll = save;
}
else{
saveRoll = invulnerableSave;
}
}
else if(save > 6){
saveRoll = 7;
}
else{
saveRoll = save;
}
// Determines how many attacks are done and multiples that by simulations
attacks *= attackingUnitSize;
hitSimulations = simulations * attacks;
int i, j, k;
for (i = 0; i < hitSimulations; i++){
dice = rand() % 6 + 1;
hitDiceResults[i] = dice;
/* printf("%d ", diceResults[i]); intended for debugging*/
}
//gets the number of hits for each dice face
for (i = 0; i < hitSimulations; i++){
if (hitDiceResults[i] == 1){
hitOnes += 1;
}
else if (hitDiceResults[i] == 2){
hitTwos += 1;
}
else if (hitDiceResults[i] == 3){
hitThrees += 1;
}
else if (hitDiceResults[i] == 4){
hitFours += 1;
}
else if (hitDiceResults[i] == 5){
hitFives += 1;
}
else {
hitSixes += 1;
}
}
// Tallies up successful hits
if (skill == 1){
successfulHits = hitOnes + hitTwos + hitThrees + hitFours + hitFives + hitSixes;
}
else if (skill == 2){
successfulHits = hitTwos + hitThrees + hitFours + hitFives + hitSixes;
}
else if (skill == 3){
successfulHits = hitThrees + hitFours + hitFives + hitSixes;
}
else if (skill == 4){
successfulHits = hitFours + hitFives + hitSixes;
}
else if (skill == 5){
successfulHits = hitFives + hitSixes;
}
else {
successfulHits = hitSixes;
}
//rolls dice for wounds after successful hits
for(j = 0; j < successfulHits; j++){
dice = rand() % 6 + 1;
woundDiceResults[j] = dice;
}
//tallies up the wounds for each dice face
for(j = 0; j < successfulHits; j++){
if (woundDiceResults[j] == 1){
woundOnes += 1;
}
else if (woundDiceResults[j] == 2){
woundTwos += 1;
}
else if (woundDiceResults[j] == 3){
woundThrees += 1;
}
else if (woundDiceResults[j] == 4){
woundFours += 1;
}
else if (woundDiceResults[j] == 5){
woundFives += 1;
}
else {
woundSixes += 1;
}
}
//tallies up the successful wound rolls
if (woundRoll == 1){
successfulWounds = woundOnes + woundTwos + woundThrees + woundFours + woundFives + woundSixes;
}
else if (woundRoll == 2){
successfulWounds = woundTwos + woundThrees + woundFours + woundFives + woundSixes;
}
else if (woundRoll == 3){
successfulWounds = woundThrees + woundFours + woundFives + woundSixes;
}
else if (woundRoll == 4){
successfulWounds = woundFours + woundFives + woundSixes;
}
else if (woundRoll == 5){
successfulWounds = woundFives + woundSixes;
}
else {
successfulWounds = woundSixes;
}
//rolls dice equal to the number of successfulwounds
for (k = 0; k < successfulWounds; k++){
dice = rand() % 6 + 1;
saveDiceResults[k] = dice;
}
// tallies up the saves for each dice face rolled
for(k = 0; k < successfulHits; k++){
if (saveDiceResults[k] == 1){
saveOnes += 1;
}
else if (saveDiceResults[k] == 2){
saveTwos += 1;
}
else if (saveDiceResults[k] == 3){
saveThrees += 1;
}
else if (saveDiceResults[k] == 4){
saveFours += 1;
}
else if (saveDiceResults[k] == 5){
saveFives += 1;
}
else {
saveSixes += 1;
}
}
if (saveRoll == 1){
successfulSaves = saveOnes + saveTwos + saveThrees + saveFours + saveFives + saveSixes;
}
else if (saveRoll == 2){
successfulSaves = saveTwos + saveThrees + saveFours + saveFives + saveSixes;
}
else if (saveRoll == 3){
successfulSaves = saveThrees + saveFours + saveFives + saveSixes;
}
else if (saveRoll == 4){
successfulSaves = saveFours + saveFives + saveSixes;
}
else if (saveRoll == 5){
successfulSaves = saveFives + saveSixes;
}
else {
successfulSaves = saveSixes;
}
/*Debugging
printf("Hit rolls of 1 rolled: %d\n", hitOnes);
printf("Hit rolls of 2 rolled: %d\n", hitTwos);
printf("Hit rolls of 3 rolled: %d\n", hitThrees);
printf("Hit rolls of 4 rolled: %d\n", hitFours);
printf("Hit rolls of 5 rolled: %d\n", hitFives);
printf("Hit rolls of 6 rolled: %d\n", hitSixes);
*/
avgHits = successfulHits / simulations;
printf("Average number of hits across %d simulations ", simulations);
printf(" is: %f.\n", avgHits);
return 0;
}
我想解决这个问题之前,我继续写代码的其余部分.我期待它打印的结果,但什么都没有发生后,模拟输入的数量.我不能弄清楚是什么原因,因为这是我第一次遇到这个问题.任何关于如何修复的建议将是伟大的.
1条答案
按热度按时间ljsrvy3e1#
有几件事
1.可变长度数组在C中是controversial,所以不要使用它们,请使用像
malloc();
这样的动态内存分配1.您试图从一个变量定义数组大小,该变量的值是在运行时获得的。这是一个逻辑错误。您必须在计算
hitDiceResults
,woundDiceResults
,saveDiceResults
的值后移动定义。1.请查看我的代码更改,这些更改由
//<- code changed here
突出显示