All pastes #245039 Raw Edit

Miscellany

public cpp v1 · immutable
#245039 ·published 2006-11-10 15:30 UTC
rendered paste body
#include <iostream>#include <utility>#include <string>#include <vector>#include "sample.h"using namespace std;// returns the square of a doubledouble square(double x) {	return x * x;}// sample class member functions after this point// no. of elements in the sampleint sample::size() { return samples.size(); }// value at an element n, in the sampledouble sample::at(int n) { return samples.at(n); }// return the vector itselfconst vector<double> &sample::getdata() { return samples; }// sets the data vector itselfvoid sample::set_data(const vector<double> &v) {	samples = v;}// returns the lowest value in a sorted sampledouble sample::minimum() {	std::sort(samples.begin(), samples.end());	return samples.at(0);}// returns the highest value in a sorted sampledouble sample::maximum() {	std::sort(samples.begin(), samples.end());	return samples.at(samples.size() - 1);}// returns the difference between the max and min valuesdouble sample::range() { return (maximum() - minimum()); }// returns halfway between the max and min valuesdouble sample::midrange() { return (maximum() + minimum()) / 2; }// returns the mean of the sampledouble sample::mean() {	double total = sum_elements();	int total_elements = samples.size();				return total/total_elements;}// the variance of the sampledouble sample::variance() {	// sum of x	double sumx = sum_elements();		// sum of (x squared)	double sumxsquared = 0;	for (int i = 0; i <= (signed)samples.size() - 1; i++)		sumxsquared += square(samples[i]);		// no. of elements	int n = (signed)samples.size();		return (sumxsquared - ((square(sumx))/n)) / n;	}// the s.d. of the sampledouble sample::std_deviation() {	return sqrt(variance());}// the median of the sample after sortingdouble sample::median() {	std::sort(samples.begin(), samples.end());		bool n_is_odd = samples.size() % 2;		// odd no. of elements	if (n_is_odd) {		cout << "odd no. elements\n";		return samples[((samples.size() + 1) / 2)];	}		// even number of elements	else {		double j = samples[samples.size() / 2];		double k = samples[(samples.size() / 2) + 1];				return (j + k) / 2;	}		}// the sum of the value of the elements in the sampledouble sample::sum_elements() {	double sumx = 0;	for (int i = 0; i <= (signed)samples.size() - 1; i++)		sumx += samples[i];		return sumx;}// independent functions after this point----------------------// operator for writing sampleostream& operator<<(ostream &s, sample p) {	s << "<" << p.size() << ": ";		for (int i = 0; i <= p.size() - 1; i++)		s << p.at(i) << " ";		s << ">";}istream& operator>>(istream &s, sample &p) {	int x;	double currdouble;	char larrow, semicolon, rarrow;		if ((s >> larrow) && (larrow == '<')) {		if ((s >> x >> semicolon) && (semicolon == ':')) { 			vector<double> vi(x);						for (int i = 0; i <= (signed)vi.size() - 1; i++) {				s >> currdouble;				vi[i] = currdouble;			}							if ((s >> rarrow) && (rarrow == '>')) {				p = sample(vi);				// cout << "Sample added.\n";			}		}					else			s.setstate(ios::badbit); // read failed	}	return s;}