Alignment problem

Can anyone teach me how to align the part on the numbers in relative frequency ?
My code go like this

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# include <iostream>
# include <ctime>
# include <cstdlib>
# include <iomanip>
using namespace std ;

int check() ;

int main (void)
{
	int dieone[6]={0} , y ,z , dietwo[6] = {0}  ;
	float counttwo = 0 , countthree = 0 , countforth = 0 , countfifth = 0 ,
		countsixth = 0 , countseventh = 0 , counteighth = 0 , countnineth = 0 , counttenth = 0 , counteleventh = 0 , counttwelveth = 0 , Relativefrequency  ;
	srand(time(0)) ;
	for (int x=0 ; x<= 8999 ; x++)
	{
		int sum = 0 ;
		y = rand()%6 + 1 ;
		dieone[y-1]=dieone[y-1] + 1 ;
		z = rand()%6 + 1 ;
		dietwo[z-1]=dietwo[z-1] + 1 ;
		sum = y + z ;
		if (sum == 2)
			counttwo ++ ; 
		if (sum == 3)
			countthree ++ ;
		if (sum == 4)
			countforth ++ ;
		if (sum == 5)
			countfifth ++ ;
		if (sum == 6)
			countsixth ++ ;
		if (sum == 7)
			countseventh ++ ;
		if (sum == 8)
			counteighth ++ ;
		if (sum == 9)
			countnineth ++ ;
		if (sum == 10)
			counttenth ++ ;
		if (sum == 11)
			counteleventh ++ ;
		if (sum == 12)
			counttwelveth ++ ;
		
	}
		cout <<	"---------------------------------------------------"
			 << endl 
			 << "Sum          "
			 <<"Frequency   " 
			 <<"       Relative Frequency" ;
	for (int i = 2 ; i <= 9 ; i++)
	{ 
		cout << endl 
			 <<	"---------------------------------------------------"
			 << endl 
			 << i 
			 <<"              "  ;
		if (i == 2)
		{
			cout << counttwo 
			   	 <<"              "  ;
			Relativefrequency = (counttwo/9000) * 100 ;
			cout << Relativefrequency ;
		}
		if (i == 3)
		{
			cout << countthree
				 <<"              "  ;	 
			Relativefrequency = (countthree/9000) * 100 ;
			cout << Relativefrequency ;
		}
		if (i == 4)
		{
			cout << countforth 
				 <<"              "  ;
			Relativefrequency = (countforth/9000) * 100 ;
			cout << Relativefrequency ;
		}
		if (i == 5)
		{
			cout << countfifth 
				 <<"              "  ;
			Relativefrequency = (countfifth/9000) * 100 ;
			cout << Relativefrequency ;
		}
		if (i == 6)
		{
			cout << countsixth 
				 <<"             "  ;
			Relativefrequency = (countsixth/9000) * 100 ;
			cout << Relativefrequency ;
		}
		if (i == 7)
		{
			cout << countseventh 
				 <<"             "  ;
			Relativefrequency = (countseventh/9000) * 100 ;
			cout << Relativefrequency ;
		}
		if (i == 8)
		{
			cout << counteighth 
				 <<"             "  ;
			Relativefrequency = (counteighth/9000) * 100 ;
			cout << Relativefrequency ;
		}
		if (i == 9)
		{
			cout << countnineth 
				 <<"              "  ;
			Relativefrequency = (countnineth/9000) * 100 ;
			cout << Relativefrequency ;
		}
	} 

	for (int i = 10 ; i <= 12 ; i++)
	{
		cout << endl 
			 <<	"---------------------------------------------------"
			 << endl 
			 << i 
			 <<"             "  ;
		if (i == 10)
		{
			cout << counttenth 
				 <<"              "  ;
			Relativefrequency = (counttenth/9000) * 100 ;
			cout << Relativefrequency ;
		}
		if (i == 11)
		{
			cout << counteleventh 
				 <<"              "  ;
			Relativefrequency = (counteleventh/9000) * 100 ;
			cout << Relativefrequency ;
		}
		if (i == 12)
		{
			cout << counttwelveth 
				 <<"              "  ;
			Relativefrequency = (counttwelveth/9000) * 100 ;
			cout << Relativefrequency ;
		}
	}
	cout << endl ;
system("pause") ;
return 0 ;
}
Last edited on
If you ever find yourself using numbered variables, it's probably time for an array!

To align the numbers in your output, use the manipulators setw(), setprecision(), and fixed.

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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;

int main()
{
	const int NUM_TRIALS = 9000;

	srand( time( 0 ));

	int dieone[ 6 ]={ 0 }, dietwo[ 6 ] = {0};
	int count[ 11 ] = { 0 }; // Index 0 holds 2's count, 1 holds 3's count, etc

	for( int i = 0; i < NUM_TRIALS; ++i )
	{
		int r1 = rand() % 6 + 1;
		++dieone[ r1 - 1 ];
		int r2 = rand() % 6 + 1;
		++dietwo[ r2 - 1 ];
		++count[ r1 + r2 - 2 ];
	}

	float Relativefrequency;

	cout << "===================================================\n";
	cout << "Sum          Frequency          Relative Frequency\n";
	cout << "===================================================\n";

	for( int i = 0; i <= 10; ++i )
	{
		cout << setw( 3 ) << i + 2;
		cout << setw( 19 ) << count[ i ];
		Relativefrequency = count[ i ] / (NUM_TRIALS / 100.0);
		cout << setw( 28 ) << fixed << setprecision( 3 ) << Relativefrequency;
		cout << "\n---------------------------------------------------\n";
	}
	cout << endl;

system("pause");
}

If you ever find yourself using an array, it's probably time for vector! :-)
Very good point, Pan!
Topic archived. No new replies allowed.