Calculation problem

hi it's musa again.now i have a new problem.i write some code using rand() but it can't calculate DEVIATION correctely like it show 0% when it will be 0.39%
.plz help me

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using std::cout;
using std::cin;
using std::endl;

#include<iomanip>

using std::setw;

#include<cstdlib>
#include<ctime>
#include<cmath>

int main()

{

const int arraysize=7;
int frequency[arraysize]={0};
const int array2=6;
double deviation[array2]={0};


int turn;
int fresum=0;
double mean_fre;
double expect;
cout<<"Enter number of roll:";
cin>>turn;
expect=turn/6;

srand(time(0) );

for (int i=1;i<=turn;i++)
 
 {
   ++frequency[1 + rand()%6];
   
                 }
              
 for (int j=0;j<=6;j++)
 {     
                 

         deviation[j]= (expect - frequency[j])/expect *100;
         
         }
              

cout<<"FACE"<<setw(13)<<"FREQUENCY"<<setw(13)<<"DEVIATION"<<endl;


cout << fixed << setprecision(3);

for(int face=1;face<=6;face++)

 {
    cout<<setw(4)<<face<<setw(14)<<frequency[face]<<setw(13)<<deviation[face]<<"%"<<endl;

      }

return main();
}
Change all your ints (not the for indices) to doubles.

EDIT: Oh, also, I just noticed a bug on line 44.
Hint: Make arraysize equal 6 and go from there.
Last edited on
Topic archived. No new replies allowed.