Sorting/Outputting Array

Hi, sort of new to C++, first time taking a course in it and I'm having trouble with my first array program. I need to read a text file and input that data into an array, which is the easy part. I then have to do some calculations which is also pretty easy. I'm having trouble when I have to sort the array numerically then output it. It's giving me random very large negative numbers for some reason. Below is my code.

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
//Steve Osborne, My First Array

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

const int MAX=51;

void findMax(double resist[], int numValues, ofstream& outfile);
void findMin(double resist[], int numValues, ofstream& outfile);
void calcMean(double resist[], int numValues, ofstream& outfile);
void geomMean(double resist[], int numValues, ofstream& outfile);
void rmsAvg(double resist[], int numValues, ofstream& outfile);
void harmMean(double resist[], int numValues, ofstream& outfile);
void sortValues(double resist[],int n);

int main(){
	int t;
	int T;
	int i;				
	int numValues=0;	
	double resist[MAX];
	ifstream infile("C:\\Users\\Inara\\Desktop\\resist.txt");
	if (!infile){
		cout<<"Unable to read input file.\n";
			 return 0;
	}
	ofstream outfile("results.txt");
	if (!outfile){
		cout << "Unable to create output file.\n";
			 return 0;
	}
	for (i=1;i<MAX && !infile.eof();i++) {
		numValues++;
		infile>>resist[i];
	}
	cout<<"Steve Osborne, My First Array\n\n"
		<<"The output file created with the results can be found"
		<<"\nwhere your project folder is located.";
	outfile<<"Steve Osborne, Program 10\n"
		   <<"Sample Resistance Study\n";
	findMax(resist, numValues, outfile);
	findMin(resist, numValues, outfile);
	calcMean(resist, numValues, outfile);
	geomMean(resist, numValues, outfile);
	rmsAvg(resist, numValues, outfile);
	harmMean(resist, numValues, outfile);
	sortValues(resist,numValues);
	cout<<"\n";
	for(T=0;T<numValues;T++){
		cout<<"\n"<<fixed<<setprecision(2)<<resist[MAX];
	}
	cout<<"\n\n";
	infile.close();
	outfile.close();
	return 0;
}
void findMax(double resist[], int numValues, ofstream& outfile){
	int i=1;
	double value=resist[i];
	double max=resist[i];
	while(i<=numValues){
		if(value>max) max=value;
		value=resist[i++];
	}
	outfile<<"The maximum value is"
		   <<fixed<<setprecision(2)<<setw(18)<<max<<"\n";
}
void findMin(double resist[], int numValues, ofstream& outfile){
	int i=1;
	double value=resist[i];
	double min=resist[i];
	while(i<=numValues){
		if(value<min) min=value;
		value=resist[i++];
	}
	outfile<<"The minimum value is"
		   <<fixed<<setprecision(2)<<setw(18)<<min<<"\n";
}
void calcMean(double resist[], int numValues, ofstream& outfile){
	int i=1;
	double sum=0;
	while(i<=numValues){
		sum+=resist[i];
		i++;
	}
	outfile<<"The mean is"
		   <<fixed <<setprecision(6)<<setw(31) 
		   <<sum/numValues<< endl;
}
void geomMean(double resist[], int numValues, ofstream& outfile){
	int i=1;
	double geo=pow(resist[i],1./numValues);
	i++;
	while(i<=numValues){
		geo*=pow(resist[i],1./numValues);
		i++;
	}
	outfile<<"The geometric mean is"
		   <<fixed<<setprecision(6)<<setw(21) 
		   <<geo<<endl;
}
void rmsAvg(double resist[], int numValues, ofstream& outfile){
	int i=1;
	double sum=0;
	while(i<=numValues){
		sum+=resist[i]*resist[i];
		i++;
	}
	outfile<<"The harmonic mean is"
		   <<fixed<<setprecision(6)<<setw(22)
		   <<sqrt((1./numValues)*sum)<<"\n";
}
void harmMean(double resist[], int numValues, ofstream& outfile){
	int i=1;
	double sum=0;
	while(i<=numValues){
		sum+=1./resist[i];
		i++;
	}
	outfile<<"The rms average is"
		   <<fixed<<setprecision(6)<<setw(24)
		   <<numValues/sum<<"\n";
}
void sortValues(double resist[],int numValues){
	for(int I=0;I<numValues;I++){
		for(int II=0;II<numValues-1;II++){
			if(resist[II]>resist[II+1]){
				double temp=resist[II+1];
				resist[II+1]=resist[II];
				resist[II]=temp;
			}
		}
	}
} 
Last edited on
Please edit your post and put [code] tags around the source so that it is easier for others to read.

Why are you not using std::sort?
The professor never introduced us to that, he explained 15 minutes on character arrays and gave us this program. Not sure what :: is either.
Check out the reference section on this site: http://www.cplusplus.com/reference/algorithm/sort/

1
2
3
4
5
6
#include <algorithm>

void sortValues(double resist[],int numValues)
{
    std::sort(resist, resist + numValues);
}


The std:: is redundant were you to include this in your code because of your using directive. The :: operator is a scope resolution operator: http://en.wikipedia.org/wiki/Scope_resolution_operator#C.2B.2B

Thanks for the help on that, it certainly simplified things. But the sorted output from the program is--
-92559631349317830000000000000000000000000000000000000000000000.00
-92559631349317830000000000000000000000000000000000000000000000.00
-92559631349317830000000000000000000000000000000000000000000000.00
-92559631349317830000000000000000000000000000000000000000000000.00
-92559631349317830000000000000000000000000000000000000000000000.00
-92559631349317830000000000000000000000000000000000000000000000.00
-92559631349317830000000000000000000000000000000000000000000000.00
-92559631349317830000000000000000000000000000000000000000000000.00
-92559631349317830000000000000000000000000000000000000000000000.00
-92559631349317830000000000000000000000000000000000000000000000.00

Press any key to continue . . .


The text file only contains 10 values, ranging from 128-132. I'm not sure why it's giving me this. Any ideas?
Use ints Format it?
Well I figured out what I was doing wrong finally. I was originally trying to sort the entire array while using only 10 of the indices. This left me with 40 0's and it was messing it up. Thanks for the help guys!
You should be aware that professors are usually less impressed by your using std::sort() than by knowing how to sort the items yourself.

Write your own sort algorithm.
+1 Duoas

i bet your professor actually expects you to code your own algorithm to sort the data, but that's ok if he didn't say so..
As for choosing a sorting algorithm -- bubble sort or selection sort is probably the easiest, but mergesort is probably more efficient than either of them, and it's not that hard to implement either. Quicksort is probably more efficient, but mergesort's much easier to implement.

Wikipedia has excellent descriptions of the aglorithms and even pseudo-code examples which make it easy to understand the underlying algorithm.
Topic archived. No new replies allowed.