Array Problems

I am trying to write a program that uses the randFloat function to generate two
sequences of 500 points. Each sequence should have a theoretical mean of 4, but one
sequence should have a variance of 0.5 and the other should have a variance of 2. When I check the computed means and variances and compare to the theoretical means and variances they don't make sense.
I posted all of my code in case I am missing something, but I am only having trouble with part 2
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
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <time.h>

using namespace std;


// Generates a double between a and b.
double randFloat (double a, double b)
{
 return a + (static_cast<double>(rand()) / RAND_MAX) * (b - a);
}

double rangeVariance(double a, double b)
{
    return pow(b - a, 2) / 12;
}

double rangeMean(double a, double b)
{
    return (a + b) / 2;
}

void generateSequence(double * seq, int len)
{
	for (int i = 0; i < len; ++i) {
		seq[i] = randFloat(4, 10);
	}
}

double calculateMean(double * seq, int len)
{
	double mean = 0;
	for (int i = 0; i < len; ++i) {
		mean += seq[i];
	}
	return mean / len;
}

double calculateVariance(double * seq, int len)
{
	double m = calculateMean(seq, len);
	double x = 0;
	for (int i = 0; i < len; ++i) {
		x += pow(seq[i] - m, 2);
	}
	return x / len;
}

void generateSequenceAndDiagnostics(double a, double b, double * seq, int len)
{
	generateSequence(seq, len);

	double theoryMean = rangeMean(a, b);
	double theoryVariance = rangeVariance(a, b);

	double actualMean = calculateMean(seq, len);
	double actualVariance = calculateVariance(seq, len);

	cout << theoryMean << "  " << actualMean << "  " << theoryVariance << "  " << actualVariance <<endl;
}

void part1()
{
	double seq100[100];
	double seq10000[10000];

	generateSequenceAndDiagnostics(4, 10, seq100, 100);
	generateSequenceAndDiagnostics(4, 10, seq10000, 10000);
}

double calculateA(double mean, double variance)
{
	return mean - sqrt(3 * variance);
}

double calculateB(double mean, double variance)
{
	return mean + sqrt(3 * variance);
}

void part2()
{
	double seq[500];

	double a = calculateA(4, 0.5);
	double b = calculateB(4, 0.5);
	generateSequenceAndDiagnostics(a, b, seq, 500);

	a = calculateA(4, 2);
	b = calculateB(4, 2);
	generateSequenceAndDiagnostics(a, b, seq, 500);
}

void part3()
{
	double seq[500];

	double a = calculateA(0, 3);
	double b = calculateB(0, 3);
	generateSequenceAndDiagnostics(a, b, seq, 500);

	a = calculateA(-4, 3);
	b = calculateB(-4, 3);
	generateSequenceAndDiagnostics(a, b, seq, 500);
}

double rand_mv(double mean, double variance)
{
	double a = calculateA(mean, variance);
	double b = calculateB(mean, variance);
	cout << a << " " << b <<endl;
	return randFloat(a, b);
}

void part4()
{
	cout << "Enter Mean: ";
	double mean;
	cin >> mean;

	cout << "Enter Variance ";
	double variance;
	cin >> variance;
	
	rand_mv(mean, variance);
}

int main()
{
	srand(time(0));

	part1();
	part2();
	part3();
	part4();

	return 0;
}
Topic archived. No new replies allowed.