I'm stuck on the very last part of a problem.

You've bought a square plot of land on ebay and you now want to plan how to develop this land. The trouble is, you've never been there. All you have is the engineer's terrain report which is an NxN matrix where each value indicates the height of that 1m2 cell. The format of the file provided to you is as follows:

10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 12 12 12
1 2 3 4 5 6 7 12 12 12
1 2 3 4 5 6 7 12 12 12

This matrix reflects a piece of land that slopes evenly from East to West, with a flat, elevated area in the south-easterly corner. The first value in the file is N, which tells you the size of the matrix (NxN).

Your program should output the following:

* the coordinates of the highest point in the site;
* the mean (average) height across the terrain;
* the standard deviation across the heights (the square root of [the difference between the mean of the squares of the heights and the square of the mean of the heights]).

And the following is my code:

#include "Plot.h"
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;

Plot::Plot() {

}
	void Plot::loadFromfile(){

	ifstream inputfile;

	inputfile.open("terrain200.dat");
	inputfile>>area;

	for(int i=0; i<area; i++){
		for(int j=0;j<area; j++){
			inputfile>>array[i][j];
		}
	}

	inputfile.close();
	}
	void Plot::print(ostream &x){


		int height=array[0][0];
		int a,b;
		double m,sum=0,ms,hs,diff,standDev;	//ms = mean squared, hs = heights squared

		for(int i=0; i<area; i++){
				for(int j=0;j<area; j++){

					sum+=array[i][j];

					if(array[i][j]>height){
						height=array[i][j];
						a=i;
						b=j;
					}
				}
		}

		hs=(sum*sum);

		m=sum/(area*area);

		ms=(m*m);

		//diff=

		standDev=sqrt(diff);

		x << "Highest point is " << height << " at the coordinates (" << a << "," << b << "). \n" ;
		x << "The total sum of heights is " << sum << ". \n";
		x << "The mean of the heights is " << m << ". \n";
		x << "the square of the mean of the heights is " << ms << ". \n";
		x << "The mean of the squares of the heights is " <<  << ". \n";
		x << "The standard deviation across the heights is " << standDev << ". \n";

	}

I think I'm really close to getting it but I'm just at the stage where I can't think anymore!

Folks might find it helpful if you'd post "Plot.h".

Sorry, probably would help alright.

#include <string>

using namespace std;

#ifndef PLOT_H_
#define PLOT_H_

class Plot {
public:
	Plot();


	void loadFromfile( );

	void print(ostream &x);


private:

		int area;

		int array[200][200];


};




#endif /* PLOT_H_ */

There are a few ways of calculating the standard deviation, some more efficient than others. Say you have five points:

1,2,3,4,5

Calculate the mean, which is 3. Now go through the points one at at time and get the differences:

1 - 3 = -2
2 - 3 = -1
3 - 3 = 0
4 - 3 = 1
5 - 3 = 2

Square these values.

4
1
0
1
4

Add 'em up.

4+1+0+1+4 = 10

You had 5 points, so divide by 5 (some people might say you should divide by 5 - 1 = 4 here, but since we're talking about land and the average is known to be EXACTLY the value of the mean as opposed to the mean being an ESTIMATE of some unknown average, you shouldn't subtract 1 here. Or ask your professor whether he/she intends for you to subract 1 or not. The professor is always right. Anyway, the formula listed shows that you're not supposed to subtract 1).

Ten divided by five is 2. The standard deviation would be the square root of 2.


So in your program, there are 100 points, not five, but the process is the same. Figure out the mean, then figure out the deviations from the mean, square them, and add those squared values up. Then divide by 100 and take the square root. This isn't the only way, but it's one way.

Thanks! :)

Any suggestions how to code it?

Just a quick correction, in Plot.h it should read

int array[10][10]

Just an error from testing it with a different input file.

Thanks! :)

Any suggestions how to code it?

Take it step by step, turn the process I posted in English into code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.