Understanding arrays with equations

I am trying to complete a homework assignment for my engineering programming applications course but I have been struggling to get understand and properly utilize the commands that we have been taught. This assignment we are using a single array where we are inputting the information from a .txt file and running it through equations that we create to come up with requested solutions. Currently I am struggling with finding the Geometric Mean and the Root Mean Squared. I think I'm not using the proper variables because I don't fully understand how they are used and reassigned through the equation, but I am hoping to gain some insight. My final is in 5 days so I need to get these concepts down before that. This is the code that I am currently running.

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
//Matthew Young HW9 Array;

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

using namespace std;

const int MAXVALUE=100;//Maximum amount of values we can pull from.

double i=0;
double loadArray(ifstream& infile, double data[], int MAXVALUE);
double findMax(double data[], int numValue);
double findMin (double data[], int numValue);
double findCalcMean (double data[], int numValue);
double findGeoMean (double data[], int numValue);
double findRmsAvg(double data[], int numValue);
 
int main(){

	double i=0;
	double numValue=0;
	double resist[MAXVALUE]={0};


	ifstream infile ("HW9resist.txt");
		if (!infile) {
			cout << "Error: can't open file.";
			infile.close();
			return 1;
		}
	numValue=loadArray(infile, resist, MAXVALUE);
	cout << "The maximum value is " << findMax(resist, numValue) << endl;
	cout << "The minimum value is " << findMin(resist, numValue) << endl;
	cout << "The calculated mean is " << findCalcMean(resist, numValue) << endl;
	cout << "The Geometric Mean is " << findGeoMean(resist, numValue) << endl;
	cout << "The Root Mean Average is " << findRmsAvg(resist, numValue) << endl;
	
	for(int i=0; i<numValue; i++){	
		cout << resist [i] << endl;
	}
	
	
	
	return 0;
}


double loadArray(ifstream& infile, double data[], int MAXVALUE){
	int i=0;
	while(i<MAXVALUE && infile >> data[i]){
		i++;
	}
	if(i>=MAXVALUE){
		cout << "Maximum size of array met." << endl;
	}
	if(!infile.eof()){
		cout << "Bad daa found." << endl;
	}
return i;
}

double findMax(double data[], int numValue){
	int index=0;
	double max=data[index];
		for (int i=1; i<numValue; i++){
			if (data[i]>max){
				index=i;
				max=data[i];
			}
		}
return max;
}


double findMin (double data[], int numValue){
		int index=0;
		double min=data[index];
			for (int i=1; i<numValue; i++){
				if (data[i]<min){
				index=i;
				min=data[i];
				}
			
			}
return min;
}			
			
double findCalcMean (double data[], int numValue){
	int index=0;
	double CalcMean=data[index];
	for (int i=1; i<numValue; i++){
		double sum=0;
		sum=data[i]*numValue;
		CalcMean=sum/numValue;		
		}				
return CalcMean;
}
			
double findGeoMean (double data[], int numValue){
	int index=0;
	double prod=1;
	double GeoMean=data[index];
	for (int i=1; i<numValue; i++){
		prod=prod*data[i];
		GeoMean=pow(prod, (1.0/i));
	}
return GeoMean;
}			

double findRmsAvg(double data[], int numValue){
	int index=0;
	double sumsq=1;
	double RmsAvg=data[index];
	for (int i=1; i<numValue; i++){
		sumsq=sumsq+pow(data[i],2);
		RmsAvg=sqrt(sumsq*(1.0/i));
	}
return RmsAvg;
}
Last edited on
Maybe go into more detail about the problems you're having specifically with some code to back it up. Explain why you believe your method does/doesn't work. I can't help you here but I can suggest making your post a bit more enjoyable to read and interact with haha.
Last edited on
Fundamentally, you are doing the same wrong thing(s) in all of findCalcMean, findGeoMean and findRmsAvg, so I'll just start with findCalcMean().

Imagine you have numbers 10, 20, 30, 40 (say). How do you find their mean?

Mean = ( 10 + 20 + 30 + 40 ) / 4;

How do you calculate that?

You start with a sum of 0.
Then you LOOP round, adding first 10, then 20, then 30, then 40.

AFTER you have FINISHED looping, you FINALLY divide by 4.
Is that what you do in your code? (That's a rhetorical question.)

------

It's just a little different with findGeomMean because you can't start with a product of 0 (or you'll end with a product of 0, too.)

Geo mean = ( 10 * 20 * 30 * 40 )^(1/4)

You start with a product of 1.
Then you LOOP round multiplying first by 10, then 20, then 30, then 40.
(Alternatively, as is closer to your code, you could start with a product of 10 - the first element - and just multiply by the remaining numbers. That's OK, too.)

AFTER you have FINISHED looping, you FINALLY take the power of 1.0/4.
Is that what you do in your code? (That's another rhetorical question.)


So, go through your functions line by line and ask yourself what they will do to the numbers 10, 20, 30, 40.

Are the initialisations correct? (currently, no - you're initialising the wrong variable).

Do you do the operation with the number of values AFTER finishing looping? (currently, no - you're doing it in the middle of the loop; you are also - for some reason known only to yourself - doing it with the loop counter i and not the number numValue).
Last edited on
Sorry if my code isn't very enjoyable lol. I am kind of focused on figuring out what I am doing wrong and I don't exactly know where I'm messing up so it is hard for me to explain. From what I have understood I am supposed to use "for" loops to get the equations to run properly. Maybe I am mistaken on that. I was also told to not hard code any of the values because my instructor is going to run our program through a different file and see if it still comes out with the correct results. The problems that I am having are that my equations are not coming out to the same value if I were to input them into a calculator, which is where I think 'lastchance' is finding some issues. Do I need to change these to while loops? Can I not do it with for loops? I didn't think that it would go to the next increment in the file until it found the first value through the equation and would just combine them as the equation cycled through. I am also having a difficult time with the initializations though as he pointed out.

I'm getting hung up figuring out what it means when I set "calc mean=data[index]' and then i is set to 1 (but I thought it started at 0) and then everything after that is just a blur cuz everything doesn't make sense to me.

If you need anything more specific let me know, I can't figure this stuff out. I've added two more equations and they are far from what they should be.

Sorry for the delayed response, we are also fighting some network virus so this is the first time I've been able to access internet from home in about two weeks. And I'm recovering from some GI tract infection, life is rough at the moment. I'm still not thinking clearly or have much energy but these last two weeks of class are a killer so no time for breaks other than the 40 hrs I just slept.
Last edited on
Please READ my post.

You don't need to add anything more, just move things round a bit.
So after taking some more time to try to figure out what you were saying lastchance, I'm interrupting what you're saying as I need to move the final calculation outside of the for loop so it isn't being run every time a new value is added correct? And that I am choosing an incorrect initializing value for some of my variables? I am getting the incorrect answer still but is this like what you were saying I needed to do? From how I understand it the data[] is going to pull the value from the file and numValue will pull the placement of the value and since there are only 10 values in the file it will stop after 10, is this all correct? From here I am trying to add up the values one by one and total them into the sum. At the end of this I want to take to total added together and divide by how many placements it pulled (numValue). Anything wrong with my way of thinking?

1
2
3
4
5
6
7
8
9
double findCalcMean (double data[], int numValue){
	int index=0;
	double sum=0;
	for (int i=1; i<numValue; i++){
		sum=sum+data[i];		
		}		
	double CalcMean=sum/numValue;
return CalcMean;
} 
What is this line doing? It isn't used.
int index=0;


You are correctly initialising sum now.


You are not quite adding up ALL the numbers. You are currently omitting data[0]. Change
for (int i=1; i<numValue; i++){
to
for (int i=0; i<numValue; i++){
in order that data[0] is the first number added to sum.


It's a minor detail, but you could actually replace the two lines
1
2
	double CalcMean=sum/numValue;
return CalcMean;

by the single line
return sum/numValue;
which has the happy benefit of (almost) ensuring that you only do the division by the number of pieces of data AFTER finishing looping.


There's also a posh way of writing sum=sum+data[i];, namely
sum+=data[i];


If you get this routine working, you should find that your other two routines have almost analogous lines. (But remember that a product needs initialising by 1, a sum by 0.)


You are almost there, so well done.
Last edited on
Awesome, I'm getting the correct value now! Thanks for the insight! And also for pointing out how to set up the i for products and sums, I probably would've over looked that. The whole reason I was using that int index=0; was because that was how he had set one of them up in class but I don't see why he was using them, I'm sure he does but I just want the correct outputs at this point and I can talk details with him later. I appreciate the help!

The last thing we have to do is to sort all the values, separate from our calculations, and I am getting a ton of errors with that but I think I am just calling it wrong or have my pass by reference set up incorrectly. I'll give this a few more good tries before I try to pick any of your brains with it.
Topic archived. No new replies allowed.