English 中文(简体)
Dice 程序, 需要删除数组或允许动态数组大小 [封闭]
原标题:Dice program, need to either eliminate array or allow for dynamic array size [closed]
  • 时间:2012-05-26 05:41:07
  •  标签:
  • c++

我几乎用骰子滚动程序完成了,我唯一的问题是,由于我所指定的阵列的大小,我无法输入超过5000卷。虽然我想我可以简单地将阵列的大小增加到一些荒谬的数字,但我宁愿不使用基于输入 remaining Rolls. 的动态大小的阵列。 有没有人可以帮忙?

注:这是经编辑的和最后的编码,有效。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

using namespace std;

int roller(){ // loop to simulate dice roll 
    int die1, die2;
    int total;
    die1 = rand()%6+1;
    die2 = rand()%6+1;
    total=die1+die2;
    return total;
}

int main(){
    int numberOfRolls;
    int remainingRolls;
    int eights=0; // declare counter array to hold frequencies of dice results
    int i;
    int value;
    float percentage;
    int currentRoll; // declare array for dice values

    currentRoll= 0;
    cout << "How many times will the dice be rolled?" << endl;
    cin >> remainingRolls;// user input # of dice rolls
    numberOfRolls = remainingRolls;// variable to hold number of rolls (for output)
    for (i=0; remainingRolls >0; remainingRolls--){// loop to count frequency of each value
        currentRoll = roller();// activate diceRoll function
        if (currentRoll == 8){
        eights++;
        }   
    }   
    percentage = (eights*100/numberOfRolls);
    cout << "The dice were rolled " << numberOfRolls << " times." << endl;
    cout << "The value 8 came up " << eights << " times or " << percentage << "% of the time." << endl;

    getch();
    return 0;   

}
最佳回答

您可以简单地删除数组。从不访问循环外的数组中的值,以便用本地变量替换。

开始创建变量 int 目前的 Roll , 以当前版本取代所有 diceValues [i] 的出现 。

问题回答

使用矢量, 让它增长到您需要的大小。 只要 < code> push_ back () 新的滚动即可 。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签