Quick histogram array assignment

Here is the assignment.
https://wiki.ittc.ku.edu/ittc/EECS168:Homework4

My problem i my output is only the header. It's like the program stops at my while loop that outputs all the calculations and the histogram.

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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
using namespace std;

double calc_mean(int input_array[], int subject_number);
double calc_median(int input_array[], int subject_number);
double calc_mode(int histogram_array[], int subject_number);
double calc_min(int input_array[]);
double calc_max(int array[], int subject_number);
double calc_std_dev(int input_array[], int subject_number);
void fill_histogram_array(int subject_number, int input_array[], int histogram_array[]);
void draw_histogram(double mode_value, int histogram_array[]);
void sort(int a[], int number_used);
void swap_values(int& first_number, int& second_number);
int index_of_smallest(const int a[], int start_index, int number_used);

int main()
{
	char c;
	int drug_id, subject_number, count, x, number;
	double mean_value, median_value, mode_value, min_value, max_value, std_dev_value;
	int histogram_array[11] = {0,0,0,0,0,0,0,0,0,0,0};
	int input_array[100];
	ifstream infile;
	ofstream outfile;
	infile.open("drugtrial.dat");
	outfile.open("results.txt");
	infile >> c;
     while(infile >> c)
     {
                           if(c == '@')
                           break;
     }
     
     infile >> drug_id;
     outfile << "Drug ID: " << drug_id << endl;
     
     infile >> c;
     
     infile >> subject_number;
     outfile << "FDA Drug Trials" << endl << "Subjects: " << subject_number << endl << endl;

	while (infile >> c)
	{
		infile.get(c);
		if (c == '#')
		{
			count++;
			infile.ignore(1000, '\n');		

			for (x = 0; x <subject_number; x++)
			{
				infile >> number;
				input_array[x] = number;
			}

			mean_value = calc_mean(input_array, subject_number);
			median_value = calc_median(input_array, subject_number);
			mode_value = calc_mode(histogram_array, subject_number);
			min_value = calc_min(input_array);
			max_value = calc_max(input_array, subject_number);
			std_dev_value = calc_std_dev(input_array, subject_number);
			
			outfile << "|Side Effect " << count << "|" << endl;
			outfile << "Mean: " << mean_value << endl;
			outfile << "Median: " << median_value << endl;
			outfile << "Mode: " << mode_value << endl;
			outfile << "Minimum: " << min_value << endl;
			outfile << "Maximum: " << max_value << endl;
			outfile << "Standard Deviation: " << std_dev_value << endl;
			fill_histogram_array(subject_number, input_array, histogram_array);
			draw_histogram(mode_value, histogram_array);
			outfile << "[00]  [01]  [02]  [03]  [04]  [05]  [06]  [07]  [08]  [09]  [10]" << endl << endl;
			outfile << "** = One Subject" << endl;
			
		}
	}
	
	return 0;
}

double calc_mean(int input_array[], int subject_number)
{
	double top;
	for (int x = 0; x < subject_number; x++)
		top += input_array[x];
	return (top / subject_number);
}

double calc_median(int input_array[], int subject_number)
{
	sort(input_array, subject_number);
	return(input_array[subject_number/2]);
}
	

double calc_mode(int histogram_array[], int subject_number)
{
	int a[11];
	for (int x = 0; x < 11; x++)
		a[x] = histogram_array[x];
	return(calc_max(a, subject_number));
}

double calc_min(int input_array[])
{
	return(input_array[0]);
}

double calc_max(int array[], int subject_number)
{
	return(array[subject_number]);
}

double calc_std_dev(int input_array[], int subject_number)
{
	double y = subject_number;
	double sum = 0;
	double running_sum =0;
	for (int x = 0; x < subject_number; x++)
		sum += input_array[x];
	double z = sum/y;
	for (int i = 0; i < subject_number; i++)
		running_sum += (pow((input_array[i] - z),2));
	return (sqrt(running_sum/subject_number));
}

void fill_histogram_array(int subject_number, int input_array[], int histogram_array[])
{
	for (int x = 0; x <subject_number; x++)
	{
		if (input_array[x] == 0)
			histogram_array[0]++;
		if (input_array[x] == 1)
			histogram_array[1]++;
		if (input_array[x] == 2)
			histogram_array[2]++;
		if (input_array[x] == 3)
			histogram_array[3]++;
		if (input_array[x] == 4)
			histogram_array[4]++;
		if (input_array[x] == 5)
			histogram_array[5]++;
		if (input_array[x] == 6)
			histogram_array[6]++;
		if (input_array[x] == 7)
			histogram_array[7]++;
		if (input_array[x] == 8)
			histogram_array[8]++;
		if (input_array[x] == 9)
			histogram_array[9]++;
		if (input_array[x] == 10)
			histogram_array[10]++;
	}
}

			

void draw_histogram(double mode_value, int histogram_array[])
{
	ofstream outfile;
	outfile.open("results.txt");
	for (int x = mode_value; x = 0; x--)
	{
		for (int y = 0; y < 11; y++)
		{
			if (histogram_array[y] = x)
				outfile << "**";
			else 
				outfile << "  ";
			outfile << "    ";
		}
		outfile << endl;
	}
	outfile.close();
}

void sort(int a[], int number_used)
{
	int index_of_next_smallest;
	for (int index = 0; index < number_used - 1; index++)
	{
		index_of_next_smallest = index_of_smallest(a, index, number_used);
		swap_values(a[index], a[index_of_next_smallest]);
	}
}

//Function to swap values if the number is smaller
void swap_values (int& first_number, int& second_number)
{
	int temp;
	temp = first_number;
	first_number = second_number;
	second_number = temp;
}

int index_of_smallest(const int a[], int start_index, int number_used)
{
	int min = a[start_index], index_of_min = start_index;

	for (int index = start_index + 1; index < number_used; index++)
		if (a[index] < min)
		{
			min = a[index];
			index_of_min = index;
		}

	return index_of_min;

}
Topic archived. No new replies allowed.