Please Help

I have to write a code that will read an array from a file and output the array along with the mean, median, standard deviation and what not. I can not get this program to work. When I debug it works fine beside one warning with deviation not returning a value or something and the program crashes on launch every time. I am in a very basic engineering c++ class with a teacher that cant even answer my questions or tell me how to do this. Bellow is a copy of my code. I can not express how much I would appreciate any help and thanks in advance for your time.

//This program will read a file and provide output and information based on the data

# include <iostream>
# include <cmath>
# include <fstream>
# include <iomanip>

using namespace std;

//Open the file under variable name Write
ofstream Write("temp.txt",ios::out);
ifstream Read("LabData.txt", ios::in);

void Header(); //Prototype for header function
int Maximum (int info[]); //Prototype for Maximum function
int Minimum (int num[]); //Prototype for Minimum function
void Distribution(int val[]); //Prototype for Distribution function
int Mean(int table[]); //Prototype for Mean function
double Deviation(int set[]); //Prototype for Standard Deviation function
void sort(int[]); //Prototype for Sort function


void main()
{
Header();

int data[400];

int number;
int lines = 1;

cout << "This program will read from a file, which is titled \"LabData.txt\"\n\n";
cout << "Then store the information to \"temp.txt\" along with\n\n";
cout << "The mean, minimum, maximum, distribution of dat and standard deviation\n\n";

for(int scale = 0; scale < 400; ++scale)
Read >> data[scale];

for(number =0; number < 400; ++number)
{
if(lines <= 20)
{
Write << data[number] << setw(4);

++lines;
}
else
{
lines = 1;

Write << "\n";
}
}

Write << "The Mean is " << Mean(data);

Write << "\n\nThe Minimum value is " << Minimum(data);

Write << "\n\nThe Maximum Value is " << Maximum(data);

Distribution(data);

Write << "\n\nThe Standard Deviation is " << Deviation(data);

sort(data);

for(number =0; number < 400; ++number)
{
if(lines <= 20)
{
Write << data[number] << setw(4);
}
else
{
lines = 1;

Write << "\n";
}
}
}






void Header() //Header Function
{ //Heading at top of output and sets precision to 2 decimal places

Write << "**************************************************************\n\n"
<< setw(5) <<"Luke Shaw and Cody Dickerman" << setw(36) << "Array\n\n"
<< setw(5) <<"Fall Semester" << setw(52) << "2012 \n\n"
<< setw(5) <<"Due Date" << setw(57) << "December 4,2012 \n\n"
<< setw(5) <<"Date Executed" << setw(52) << "November 27,2012 \n\n"
<< "**************************************************************\n\n";

} //End Header Function

int Maximum (int info[])
{
int Number;

Number = info[0];

for (int counter = 1; counter < 400; ++counter)
if (info[counter] > Number)
Number = info[counter];
return Number;
}

int Minimum (int num[])
{
int Value;

int count;

Value = num[399];

for(count = 399; count >= 0; --count);
if (num[count] < Value)
Value = num[count];

return Value;
}

void Distribution(int val[])
{
int val1 = 0;
int val2 = 0;
int val3 = 0;
int val4 = 0;
int val5 = 0;
int val6 = 0;
int val7 = 0;
int val8 = 0;
int val9 = 0;
int val10 = 0;

for( int var = 0; var < 400; ++var)
{
if (1 <= val[var] && val[var] <= 10)
++ val1;
else if (11 <= val[var] && val[var] <= 20)
++ val2;
else if (21 <= val[var] && val[var] <= 30)
++ val3;
else if (31 <= val[var] && val[var] <= 40)
++ val4;
else if (41 <= val[var] && val[var] <= 50)
++ val5;
else if (51 <= val[var] && val[var] <= 60)
++ val6;
else if (61 <= val[var] && val[var] <= 70)
++ val7;
else if (71 <= val[var] && val[var] <= 80)
++ val8;
else if (81 <= val[var] && val[var] <= 90)
++ val9;
else if (91 <= val[var] && val[var] <= 100)
++ val10;
}

Write << "\n\n1 <= " << val1 << " <= 10\n\n";
Write << "11 <= " << val2 << " <= 20\n\n";
Write << "21 <= " << val3 << " <= 30\n\n";
Write << "31 <= " << val4 << " <= 40\n\n";
Write << "41 <= " << val5 << " <= 50\n\n";
Write << "51 <= " << val6 << " <= 60\n\n";
Write << "61 <= " << val7 << " <= 70\n\n";
Write << "71 <= " << val8 << " <= 80\n\n";
Write << "81 <= " << val9 << " <= 90\n\n";
Write << "91 <= " << val10 << " <= 100\n\n";
}

int Mean(int table[])
{
int average;
double total = 0;
int sum = 0;

for (average = 0; average < 400; ++average)
sum += table[average];

return (sum / 400);
}

double Deviation(int set[])
{
double value = 0.;

for (int dev = 0; dev < 400; ++dev)
{
value = set[dev] - Mean(set);

value = pow(value,2.);

value = +value;

return sqrt((value / 400));
}
}

void sort(int data[])
{
int position;

int smallest;

for(int variable = 0; variable < 400; ++variable)
{
position = variable;

smallest = data[position];

for(int variable1 = variable + 1; variable1 < 401; ++variable)
{
if (data[variable1] < smallest)
{
position = variable1;

smallest = data[position];
}

data[position] = data[variable];

data[variable] = smallest;

}
}
}





Also if it helps, the LabData.txt file I am accessing is in the project folder and looks like this

14 4 78 45 23 99 56 54 7 19 37 71 20 82 33 59 53 77 55 1
71 72 25 54 61 78 53 45 60 56 54 70 76 60 69 32 45 48 68 36
16 37 58 44 37 42 53 65 13 54 16 30 43 63 62 37 75 59 23 67
41 42 55 34 60 62 21 72 42 36 53 66 47 46 56 44 73 86 49 63
64 70 68 24 45 49 60 57 55 70 45 35 41 37 44 74 45 60 67 85
12 48 69 53 11 44 59 29 22 52 93 42 58 74 66 84 61 35 42 41
41 63 79 53 54 69 33 36 54 44 64 52 44 33 7 51 59 61 54 27
62 70 56 52 34 61 94 33 28 61 34 41 10 58 53 43 51 56 69 68
51 80 62 68 71 42 46 52 50 33 59 59 66 45 69 22 57 68 58 74
60 69 36 57 74 36 82 60 43 70 32 68 51 45 73 69 23 49 51 48
51 26 49 52 65 41 34 58 43 5 92 56 83 52 40 58 88 47 60 60
43 30 57 54 61 79 65 57 78 46 73 59 56 56 34 44 31 41 87 38
54 61 80 52 41 67 39 52 55 51 21 24 32 45 40 64 39 59 68 67
52 48 6 38 53 42 60 40 39 15 65 57 63 50 66 32 47 75 43 75
2 66 83 29 50 64 62 51 77 43 33 72 19 56 88 67 60 26 67 66
49 62 53 54 27 56 57 40 54 58 45 64 43 39 47 66 76 31 65 80
91 38 71 53 47 47 35 52 71 46 56 57 68 42 31 92 58 77 41 37
50 39 53 55 72 39 94 76 62 32 64 46 51 78 80 25 63 75 79 70
50 60 58 84 40 59 51 82 14 63 52 61 53 45 91 42 81 44 76 78
6 45 63 38 85 31 61 93 58 46 49 64 83 79 28 86 77 69 87 37


Last edited on
try int main.
Testing it now, - I see that it crashes very quickly - perhaps a memory leak or a stack overflow - just started testing,

I would like to begin by making the array "Number[400] " a lot less, but it's hard coded throughout the entire program,

Maybe in the future you could initl'ize it to a single var -
as a static INT like this:

#define i_max_arry_size 400 // no semicolon afterwards.

Too, that 400 value is also all over the place,
that ought to be turned into a STATIC val as well,

STATIC INT i_arb_Cntr = 400;
==============

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

int val1 = 0;
int val2 = 0;
int val3 = 0;
int val4 = 0;
int val5 = 0;
int val6 = 0;
int val7 = 0;
int val8 = 0;
int val9 = 0;
int val10 = 0;

for( int var = 0; var < i_arb_cntr; ++var)
{
if (1 <= val[var] && val[var] <= 10)
++ val1;


While it isn't incorrect to always increment a var before it is tested, it is rather confusing, at least to me, that all of your vars are incremented first rather than afterwards.

This is why your program never checks the Zeroth element of your array.


Rather than ++val1
you should change all these to val1++ and the same with the other occurrences

Also, check your logic in your IF conditions.
For example:
 
if (1 <= val[var] && val[var] <= 10)

IF VAL1 is less than 1,
then it stands to reason that it is also less than 10 !
Therefore the IF statement is mostly (50%) useless,

You might try placing all your similar TYPE vars into a single line of code.
1
2
int val1=0, val2=0, val3=0, val4=0, val5=0, val6=0, val7=0, val8=0, val9=0, val10 = 0;








Last edited on
#define is a macro not an int
Re-posted below with a better format
Last edited on
Hey, thanks for the help so far but it does not seem to have fixed the program. Below is an updated version of my program. I tried to change everything that was suggested.

Also, in regards to the logic behind my if statement, I have to check for variables between 1 and 10 so it has to not only b less than 10 but also greater than one to fit into that category. The teacher wants a list of how many numbers are between 1 and 10, 11 and 20, and so forth.

Also if it helps, the LabData.txt file I am accessing is in the project folder and looks like this

14 4 78 45 23 99 56 54 7 19 37 71 20 82 33 59 53 77 55 1
71 72 25 54 61 78 53 45 60 56 54 70 76 60 69 32 45 48 68 36
16 37 58 44 37 42 53 65 13 54 16 30 43 63 62 37 75 59 23 67
41 42 55 34 60 62 21 72 42 36 53 66 47 46 56 44 73 86 49 63
64 70 68 24 45 49 60 57 55 70 45 35 41 37 44 74 45 60 67 85
12 48 69 53 11 44 59 29 22 52 93 42 58 74 66 84 61 35 42 41
41 63 79 53 54 69 33 36 54 44 64 52 44 33 7 51 59 61 54 27
62 70 56 52 34 61 94 33 28 61 34 41 10 58 53 43 51 56 69 68
51 80 62 68 71 42 46 52 50 33 59 59 66 45 69 22 57 68 58 74
60 69 36 57 74 36 82 60 43 70 32 68 51 45 73 69 23 49 51 48
51 26 49 52 65 41 34 58 43 5 92 56 83 52 40 58 88 47 60 60
43 30 57 54 61 79 65 57 78 46 73 59 56 56 34 44 31 41 87 38
54 61 80 52 41 67 39 52 55 51 21 24 32 45 40 64 39 59 68 67
52 48 6 38 53 42 60 40 39 15 65 57 63 50 66 32 47 75 43 75
2 66 83 29 50 64 62 51 77 43 33 72 19 56 88 67 60 26 67 66
49 62 53 54 27 56 57 40 54 58 45 64 43 39 47 66 76 31 65 80
91 38 71 53 47 47 35 52 71 46 56 57 68 42 31 92 58 77 41 37
50 39 53 55 72 39 94 76 62 32 64 46 51 78 80 25 63 75 79 70
50 60 58 84 40 59 51 82 14 63 52 61 53 45 91 42 81 44 76 78
6 45 63 38 85 31 61 93 58 46 49 64 83 79 28 86 77 69 87 37

P.s. just found out how to make the code appear as code, sorry for the double post

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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//This program will read a file and provide output and information based on the data

# include <iostream>
# include <cmath>
# include <fstream>
# include <iomanip>

using namespace std;

//Opens the file under variable name Write and Identifies the file that is being read from
ofstream Write("temp.txt",ios::out);
ifstream Read("LabData.txt", ios::in);

	void Header();				                //Prototype for header function      
	int Maximum (int info[], int);              //Prototype for Maximum function
	int Minimum (int num[],int);                //Prototype for Minimum function
	void Distribution(int val[],int);           //Prototype for Distribution function
	int Mean(int table[],int);                  //Prototype for Mean function
	double Deviation(int set[], int);           //Prototype for Standard Deviation function
	void sort(int[], int);                      //Prototype for Sort function
	const int Max = 400;                        //Size of the array


int main()
{
	Header();

	int data[Max];

	int number;
	int lines = 1;

	cout << "This program will read from a file, which is titled \"LabData.txt\"\n\n";
	cout << "Then store the information to \"temp.txt\" along with\n\n";
	cout << "The mean, minimum, maximum, distribution of dat and standard deviation\n\n";

	for(int scale = 0; scale < Max; scale++)
		Read >> data[scale];

	for(number =0; number < Max; number++)
	{
		if(lines <= 20)
		{
			Write << data[number] << setw(4);

			++lines;
		}
		else 
		{
			lines = 1;

			Write << "\n";
		}
	}

	Write << "The Mean is " << Mean(data, Max);

	Write << "\n\nThe Minimum value is " << Minimum(data, Max);

	Write << "\n\nThe Maximum Value is " << Maximum(data, Max);

	Distribution(data, Max);

	Write << "\n\nThe Standard Deviation is " << Deviation(data, Max);

	sort(data, Max);

	for(number =0; number < Max; number++)
	{
		if(lines <= 20)
		{
			Write << data[number] << setw(4);

			++lines;
		}
		else 
		{
			lines = 1;

			Write << "\n";
		}
	}

	return 0;
}
	





void Header()			//Header Function
{						
	//Heading at top of output and sets precision to 2 decimal places

Write << "**************************************************************\n\n"
	  << setw(5) <<"Luke Shaw and Cody Dickerman" << setw(36) << "Array\n\n"
	  << setw(5) <<"Fall Semester" << setw(52) << "2012 \n\n"
	  << setw(5) <<"Due Date" << setw(57) << "December 4,2012 \n\n"
	  << setw(5) <<"Date Executed" << setw(52) << "November 27,2012 \n\n"
	  << "**************************************************************\n\n";

} //End Header Function

int Maximum (int info[], int Max)
{
	int Number;

	Number = info[0];

	for (int counter = 1; counter < Max; counter++)
		if (info[counter] > Number)
			Number = info[counter];
	return Number;
}

int Minimum (int num[], int Max)
{
	int Value;

	int count;

	Value = num[399];

	for(count = (Max - 1); count >= 0; count--);
		if (num[count] < Value)
			Value = num[count];

	return Value;
}

void Distribution(int val[], int Max)
{
	int val1 = 0;
	int val2 = 0;
	int val3 = 0;
	int val4 = 0;
	int val5 = 0;
	int val6 = 0;
	int val7 = 0;
	int val8 = 0;
	int val9 = 0;
	int val10 = 0;

	for( int var = 0; var < Max; var++)
	{
		if (1 <= val[var] && val[var] <= 10)
			++ val1;
		else if (11 <= val[var] && val[var] <= 20)
			++ val2;
		else if (21 <= val[var] && val[var] <= 30)
			++ val3;
		else if (31 <= val[var] && val[var] <= 40)
			++ val4;
		else if (41 <= val[var] && val[var] <= 50)
			++ val5;
		else if (51 <= val[var] && val[var] <= 60)
			++ val6;
		else if (61 <= val[var] && val[var] <= 70)
			++ val7;
		else if (71 <= val[var] && val[var] <= 80)
			++ val8;
		else if (81 <= val[var] && val[var] <= 90)
			++ val9;
		else if (91 <= val[var] && val[var] <= 100)
			++ val10;
	}

	Write << "\n\n1 <= " << val1 << " <= 10\n\n";
	Write << "11 <= " << val2 << " <= 20\n\n";
	Write << "21 <= " << val3 << " <= 30\n\n";
	Write << "31 <= " << val4 << " <= 40\n\n";
	Write << "41 <= " << val5 << " <= 50\n\n";
	Write << "51 <= " << val6 << " <= 60\n\n";
	Write << "61 <= " << val7 << " <= 70\n\n";
	Write << "71 <= " << val8 << " <= 80\n\n";
	Write << "81 <= " << val9 << " <= 90\n\n";
	Write << "91 <= " << val10 << " <= 100\n\n";
}

int Mean(int table[], int Max)
{
	int average;
	double total = 0;
	int sum = 0;

	for (average = 0; average < Max; average++)
		sum += table[average];
	
	return (sum / Max);
}

double Deviation(int set[], int Max)
{
	double value = 0.;

	for (int dev = 0; dev < Max; dev++)
	{
		value = set[dev] - Mean(set, Max);

		value = pow(value,2.);

		value = +value;

		return sqrt((value / Max));
	}
}

void sort(int data[], int Max)
{
	int position;

	int smallest;

	for(int variable = 0; variable < Max; variable++)
	{
		position = variable;

		smallest = data[position];

		for(int variable1  = variable + 1; variable1 < (Max + 1) ; ++variable)
		{
			if (data[variable1] < smallest)
			{
				position = variable1;

				smallest = data[position];
			}

		data[position] = data[variable];

		data[variable] = smallest;

		}
	}
}
Last edited on
If you didn't know this, the problem is in void sort.
If you comment out the sort function the program runs.

It looks like the loop keeps trying to run after the max size of 400.

Below is what I used to debug. If you look at the output, and the loop order maybe you can figure it out.

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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//This program will read a file and provide output and information based on the data

# include <iostream>
# include <cmath>
# include <fstream>
# include <iomanip>
#include <conio.h>
using namespace std;

//Opens the file under variable name Write and Identifies the file that is being read from
ofstream Write("temp.txt",ios::out);
ifstream Read("LabData.txt", ios::in);

	void Header();				                //Prototype for header function      
	int Maximum (int info[], int);              //Prototype for Maximum function
	int Minimum (int num[],int);                //Prototype for Minimum function
	void Distribution(int val[],int);           //Prototype for Distribution function
	int Mean(int table[],int);                  //Prototype for Mean function
	double Deviation(int set[], int);           //Prototype for Standard Deviation function
	void sort(int[], int);                      //Prototype for Sort function
	const int Max = 400;                        //Size of the array


int main()
{
cout << "Test Main 1" << endl;
	Header();
	int data[Max];
	int number;
	int lines = 1;

	cout << "This program will read from a file, which is titled \"LabData.txt\"\n\n";
	cout << "Then store the information to \"temp.txt\" along with\n\n";
	cout << "The mean, minimum, maximum, distribution of dat and standard deviation\n\n";

cout << "Test Main 2" << endl;
//  getch();  // TEST
	for(int scale = 0; scale < Max; scale++)
    {

		Read >> data[scale];}

	for(number =0; number < Max; number++)
	{
cout << "Test Main 3" << endl;
//  getch();  // TEST
		if(lines <= 20)
		{
			Write << data[number] << setw(4);
			++lines;
//			  getch();  // TEST
		}
		else 
		{
cout << "Test Main 4" << endl;
//  getch();  // TEST
			lines = 1;
			Write << "\n";
//  getch();  // TEST
		}
	}
cout << "Test Main 5" << endl;
	Write << "The Mean is " << Mean(data, Max);
	Write << "\n\nThe Minimum value is " << Minimum(data, Max);
	Write << "\n\nThe Maximum Value is " << Maximum(data, Max);
	Distribution(data, Max);
	Write << "\n\nThe Standard Deviation is " << Deviation(data, Max);
cout << "Test Main 6" << endl;
	sort(data, Max);
cout << "Test Main 6.1" << endl;
//  getch();  // TEST
	for(number =0; number < Max; number++)
	{
cout << "Test Main 6.2" << endl;
		if(lines <= 20)
		{
cout << "Test Main 7" << endl;
//  getch();  // TEST
			Write << data[number] << setw(4);
			++lines;
		}
		else 
		{
cout << "Test Main 8" << endl;
//  getch();  // TEST
			lines = 1;
			Write << "\n";
		}
	}

	return 0;
}
	





void Header()			//Header Function
{						
cout << "Test Header" << endl;
	//Heading at top of output and sets precision to 2 decimal places

Write << "**************************************************************\n\n"
	  << setw(5) <<"Luke Shaw and Cody Dickerman" << setw(36) << "Array\n\n"
	  << setw(5) <<"Fall Semester" << setw(52) << "2012 \n\n"
	  << setw(5) <<"Due Date" << setw(57) << "December 4,2012 \n\n"
	  << setw(5) <<"Date Executed" << setw(52) << "November 27,2012 \n\n"
	  << "**************************************************************\n\n";

} //End Header Function

int Maximum (int info[], int Max)
{
cout << "Test Maximum" << endl;
	int Number;
	Number = info[0];
	for (int counter = 1; counter < Max; counter++)
		if (info[counter] > Number)
			Number = info[counter];
	return Number;
}

int Minimum (int num[], int Max)
{
cout << "Test Minimun" << endl;
	int Value;
	int count;
	Value = num[399];
	for(count = (Max - 1); count >= 0; count--);
		if (num[count] < Value)
			Value = num[count];
	return Value;
}

void Distribution(int val[], int Max)
{
cout << "Test Distribution" << endl;
	int val1 = 0;
	int val2 = 0;
	int val3 = 0;
	int val4 = 0;
	int val5 = 0;
	int val6 = 0;
	int val7 = 0;
	int val8 = 0;
	int val9 = 0;
	int val10 = 0;

	for( int var = 0; var < Max; var++)
	{
		if (1 <= val[var] && val[var] <= 10)
			++ val1;
		else if (11 <= val[var] && val[var] <= 20)
			++ val2;
		else if (21 <= val[var] && val[var] <= 30)
			++ val3;
		else if (31 <= val[var] && val[var] <= 40)
			++ val4;
		else if (41 <= val[var] && val[var] <= 50)
			++ val5;
		else if (51 <= val[var] && val[var] <= 60)
			++ val6;
		else if (61 <= val[var] && val[var] <= 70)
			++ val7;
		else if (71 <= val[var] && val[var] <= 80)
			++ val8;
		else if (81 <= val[var] && val[var] <= 90)
			++ val9;
		else if (91 <= val[var] && val[var] <= 100)
			++ val10;
	}

	Write << "\n\n1 <= " << val1 << " <= 10\n\n";
	Write << "11 <= " << val2 << " <= 20\n\n";
	Write << "21 <= " << val3 << " <= 30\n\n";
	Write << "31 <= " << val4 << " <= 40\n\n";
	Write << "41 <= " << val5 << " <= 50\n\n";
	Write << "51 <= " << val6 << " <= 60\n\n";
	Write << "61 <= " << val7 << " <= 70\n\n";
	Write << "71 <= " << val8 << " <= 80\n\n";
	Write << "81 <= " << val9 << " <= 90\n\n";
	Write << "91 <= " << val10 << " <= 100\n\n";
}

int Mean(int table[], int Max)
{
cout << "Test Mean" << endl;
	int average;
	double total = 0;
	int sum = 0;
	for (average = 0; average < Max; average++)
		sum += table[average];
	return (sum / Max);
}

double Deviation(int set[], int Max)
{
cout << "Test Deviation" << endl;
	double value = 0.;
	for (int dev = 0; dev < Max; dev++)
	{
		value = set[dev] - Mean(set, Max);
		value = pow(value,2.);
		value = +value;
		return sqrt((value / Max));
	}
}


void sort(int data[], int Max)
{
cout << "Test sort 0" << endl; // Test
	int position;
	int smallest;
cout << "Test sort 1" << endl; // Test	
	for(int variable = 0; variable < Max; variable++)
	{
cout << "Test sort 2" << endl; // Test	
		position = variable;
		smallest = data[position];
cout << "Test sort 3" << endl; // Test			
		for(int variable1  = variable + 1; variable1 < (Max + 1) ; ++variable)
		{
cout << "Test sort 4" << endl; // Test
cout << variable << " " << variable1 << " " << Max << " " << endl;
			if (data[variable1] < smallest)
			{
cout << "Test sort 5" << endl; // Test
				position = variable1;
				smallest = data[position];
			}
cout << "Test sort 6" << endl; // Test
		data[position] = data[variable];
cout << "Test sort 7" << endl; // Test
		data[variable] = smallest;
cout << "Test sort 8" << endl; // Test
		}
cout << "Test sort 9" << endl; // Test
	}
cout << "Test sort 10" << endl; // Test	
}



Last edited on
Hey, thanks a lot. I just finished taking the code apart function by function and isolated a couple of problems. The main one being the sort function as you have mentioned above. I'll mark this problem as solved and post the code for anyone that is curious. Thanks again everyone for all the help.
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//This program will read a file and provide output and information based on the data

# include <iostream>
# include <cmath>
# include <fstream>
# include <iomanip>

using namespace std;

//Opens the file under variable name Write and Identifies 
//The file that is being read from
ofstream Write("temp.txt",ios::out);
ifstream Read("LabData.txt", ios::in);

	void Header();				                //Prototype for header function  
	double Mean(int table[],double);            //Prototype for Mean function
	const int Max = 400;               //Constant used to represent the size of the array
	int Minimum (int num[],int);                //Prototype for Minimum function
	int Maximum (int info[], int);              //Prototype for Maximum function
	void Distribution(int val[],int);           //Prototype for Distribution function
	void sort(int[], int);                      //Prototype for Sort function
	double Deviation(int set[], int, double); //Prototype for Standard Deviation function


int main()
{
	//Displays programmer information to the user

	Header();

	int data[Max];   //Defines the arrat
	int number;      //Varialbe used to count 
	int lines = 1;   //Variable used to format the outputof the array 
	double meanb;    //Variable used to store the value of the mean

	//Message displayed to user letting them know where the file is stored and
	//What the name of the file it reads from is called

	cout << "This program will read from a file, which is titled \"LabData.txt\"\n\n";
	cout << "Then store the information to \"temp.txt\" along with\n\n";
	cout << "The mean, minimum, maximum, distribution of dat and standard deviation\n\n";

	//Loop to read the text document into an array

	for(int scale = 0; scale < Max; scale++)
		Read >> data[scale];

	//Loop to write the array with the proper formating

	for(number =0; number < Max; number++)
	{
		//User a varialbe to keep track of the output and format it correctly

		if(lines <= 20)
		{
			Write << setw(4) << data[number];

			++lines;
		}
		else 
		{
			lines = 2;

			Write << "\n" << setw(4) << data[number];
		}
	}

	//Writes the value of the mean, to a precision, to a file

	Write << "\n\nThe Mean is " << setprecision(4) << Mean(data, 400.0);

	//stores the value of the mean to a variable

	meanb = Mean(data, 400.0);

	//Writes the minimum value to a file

	Write << "\n\nThe Minimum value is " << Minimum(data, Max);

	//Writes the maximum value to a file

	Write << "\n\nThe Maximum Value is " << Maximum(data, Max);

	//Function that writes the distribution of the array to a file

	Distribution(data, Max);

	//Writes the standard deviation of the array to a file

	Write << "\n\nThe Standard Deviation is " << Deviation(data, Max, meanb) << "\n\n";

	//Sorts the array in order from the lowest integer to the highest integer

	sort(data, Max);

	//Reprints the now sorted array

	for(number =0; number < Max; number++)
	{
		//User a varialbe to keep track of the output and format it correctly

		if(lines <= 20)
		{
			Write << setw(4) << data[number];

			++lines;
		}
		else 
		{
			lines = 2;

			Write << "\n" << setw(4) << data[number];
		}
	}

	return 0; //Returns to end the int main()
} //End Main

void Header()			//Header Function
{						
	//Heading at top of output and sets precision to 2 decimal places

Write << "**************************************************************\n\n"
	  << setw(5) <<"Luke Shaw and Cody Dickerman" << setw(36) << "Array\n\n"
	  << setw(5) <<"Fall Semester" << setw(52) << "2012 \n\n"
	  << setw(5) <<"Due Date" << setw(57) << "December 4,2012 \n\n"
	  << setw(5) <<"Date Executed" << setw(52) << "November 27,2012 \n\n"
	  << "**************************************************************\n\n";

} //End Header Function

//Function for finding the mean

double Mean(int table[], double Max)
{
	int average;        //Variable used to count
	int sum = 0;        //Represents the sum of the array

	//Loop that sums the array

	for (average = 0; average < Max; average++)
		sum += table[average];

	//Returns the Mean of the array

	return (sum / Max);

} //End Mean Function

//Function for finding the maximum value

int Maximum (int info[], int Max)
{
	int Number;   //Variabled used to store the maximum number as the loop processes

	Number = info[0];

	//Loop the will find the maximum value and store it under number

	for (int counter = 1; counter < Max; counter++)
		if (info[counter] > Number)
			Number = info[counter];

	//Returns the maximum number

	return Number;
} //End Main

//Function for finding the minimum value

int Minimum (int info[], int Max)
{
	int Number;    //Variabled used to store the minimum number as the loop processes

	Number = info[0];

	//Loop the will find the minimum value and store it under number

	for (int counter = 1; counter < Max; counter++)
		if (info[counter] < Number)
			Number = info[counter];

	//Returns the minimum number

	return Number;
}

//Function for sorting the distribution of the array

void Distribution(int val[], int Max)
{
	int val1 = 0;     //Used to store a number of for a variable within a condition
	int val2 = 0;     //Used to store a number of for a variable within a condition
	int val3 = 0;     //Used to store a number of for a variable within a condition
	int val4 = 0;     //Used to store a number of for a variable within a condition
	int val5 = 0;     //Used to store a number of for a variable within a condition
	int val6 = 0;     //Used to store a number of for a variable within a condition
	int val7 = 0;     //Used to store a number of for a variable within a condition
	int val8 = 0;     //Used to store a number of for a variable within a condition
	int val9 = 0;     //Used to store a number of for a variable within a condition
	int val10 = 0;    //Used to store a number of for a variable within a condition

	//Loop to sort the array and count the number of 
	//Integers within the following conditions

	for( int var = 0; var < Max; var++)
	{
		//Checks which condition each array number falls under and increments a value

		if (1 <= val[var] && val[var] <= 10)
			++ val1;
		else if (11 <= val[var] && val[var] <= 20)
			++ val2;
		else if (21 <= val[var] && val[var] <= 30)
			++ val3;
		else if (31 <= val[var] && val[var] <= 40)
			++ val4;
		else if (41 <= val[var] && val[var] <= 50)
			++ val5;
		else if (51 <= val[var] && val[var] <= 60)
			++ val6;
		else if (61 <= val[var] && val[var] <= 70)
			++ val7;
		else if (71 <= val[var] && val[var] <= 80)
			++ val8;
		else if (81 <= val[var] && val[var] <= 90)
			++ val9;
		else if (91 <= val[var] && val[var] <= 100)
			++ val10;
	}

	//Writes the number of integers that fall within each condition to a file

	Write << "\n\n1 <= " << val1 << " <= 10\n\n";
	Write << "11 <= " << val2 << " <= 20\n\n";
	Write << "21 <= " << val3 << " <= 30\n\n";
	Write << "31 <= " << val4 << " <= 40\n\n";
	Write << "41 <= " << val5 << " <= 50\n\n";
	Write << "51 <= " << val6 << " <= 60\n\n";
	Write << "61 <= " << val7 << " <= 70\n\n";
	Write << "71 <= " << val8 << " <= 80\n\n";
	Write << "81 <= " << val9 << " <= 90\n\n";
	Write << "91 <= " << val10 << " <= 100\n\n";

} //End distribution function

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
//Function the finds the standard deviation

double Deviation(int set[], int Max, double Mean)
{
	double value = 0;     //Variable used store part of the standard deviation equation

	double deviation = 0; //Stores the standard deviation and is used to return a value

	//Loop that finds the sum of part of the standard deviation equation

	for (int dev = 0; dev < Max; dev++)
	
		value += (Mean-set[dev])*(Mean-set[dev]);

	//Finds the standard deviation of the array

	deviation = sqrt(value/(Max - 1));

	//Returns the standard deviation

	return deviation;
	
} //End standard deviation function

//Function that sorts the array

void sort( int list[], int size ) 
{
	int num = 1;  //Variable used in counting 

	//Loop that works as long as the count varaible is below the size of the array

	while ( num < size ) 
	{
		//Loop that checks and sorts each value

		for (int num1 = num; list[num1 - 1] > list[num1]; num1-- ) 
		{
			int val = list[num1];
			list[num1] = list[num1 - 1];
			list[num1 - 1] = val;
		}

		//Increments the variable that is counting

		num++;

	}

} //End sort function 
Topic archived. No new replies allowed.