У меня есть домашняя работа C ++, это вопрос
//Dietel & Dietel C Programming //Chapter 6 Arrays: Page 241 Exercise:
6.19 /* Write a program that simulates the rolling of two dice.
* The program should use rand to roll the first die, and
* should use rand again to roll the second die.
* The sum of the two values should then be calculated. (Note: Since each die
* can show an integer value from 1 to 6, then the sum of the two values will vary
* from 2 to 12 with 7 being the most freqent sum and 2 and 12 being the least frequent
* sums.) Figure 6.23 shows the 36 possible combinations of the two dice.
* Your program should
* roll the two dice 36,000 times. Use a single-scripted array to tally the numbers of times
* each possible sum appears.
* Print the results in a tabular format. Also, determine if the totals
* are resonable; i.e there are six ways to roll a 7, so approximately one sixth of all of the
* rolls should be 7.
*/
Я создал программу, но она дает мне вывод примерно так:
sum of Faces Frequency
2 0
3 4041
4 0
5 7922
6 0
7 12154
8 0
9 7936
10 0
11 3948
12 0
sum: 36001
Я не знаю, почему он дает 0 частот для всех четных чисел
Вот что я кодировал до сих пор:
#include <iostream>
#include<iomanip>
using namespace std;
int main()
{
const int arraysize = 13;
int counter[13], sum=0;
// init counter
for(int i=0; i<13; i++)
counter[i] = 0;
int die1;
int die2;
for ( int roll1 = 0; roll1 <=36000; roll1++ ) {
die1 = 1 + rand() % 6;
die2 = 1 + rand() % 6;
counter[die1+die2]++;
}
cout<<"sum of Faces"<<setw(13)<<"Frequency"<<endl;
for(int face=2; face<arraysize;face++)
{
cout<<setw(7)<<face<<setw(13)<<counter[face]<<endl;
sum += counter[face];
}
cout << "sum: " << sum;return 0;
}
Мне нужно добавить возможности для игры в кости, например:
1 + 1 = 2 : 1 possibility for sum to be 2
1 + 2 = 2 + 1 = 3 : 2 possibility for sum to be 3
1 + 3 = 2 + 2 = 3 + 1 = 4 : 3 possibility for sum to be 4
.
.
.
6 + 6 = 12 : 1 possibility for sum to be 12
Я просто копирую ваш код и запускаю его, вам, похоже, не хватает библиотеки для функции rand (понятия не имею, как вы ее запускаете). Я просто импортировал библиотеку для rand ….
#include <bits/stdc++.h>
using namespace std;
int main()
{
const int arraysize = 13;
int counter[13], sum=0;
// init counter
for(int i=0; i<13; i++)
counter[i] = 0;
int die1;
int die2;
for ( int roll1 = 0; roll1 <=36000; roll1++ ) {
die1 = 1 + rand() % 6;
die2 = 1 + rand() % 6;
counter[die1+die2]++;
}
cout<<"sum of Faces"<<setw(13)<<"Frequency"<<endl;
for(int face=2; face<arraysize;face++)
{
cout<<setw(7)<<face<<setw(13)<<counter[face]<<endl;
sum += counter[face];
}
cout << "sum: " << sum;return 0;
}
Он работает правильно на моей машине.
Вы, кажется, не заполняете случайную функцию, это может привести к странным результатам
Решение:
#include <ctime> // time()
#include <cstdlib> // srand(), rand()
...
srand(time(NULL)); // There are better ways to seed rand(), but this is simple
...
for ( int roll1 = 0; roll1 <=36000; roll1++ ) {
die1 = 1 + rand() % 6;
die2 = 1 + rand() % 6;
counter[die1+die2]++;
}
Кроме того, если вы хотите более похожее на C ++ решение (и имеете доступ к C ++ 11), Проверь это