Posts

Showing posts from October, 2017

Object Oriented Programming C++ Simple Calculator

This is a beginner C++ OOP program. # include <iostream> # include <limits> # include <math.h> # include <cmath> // for log function using namespace std ; class calculator { public: double add(double a, double b){ return a+b; } double subtract (double a, double b){ return a-b; } double multiply (double a, double b){ if(a==0 && b==0){ cout<<"Error! Invalid operation!"<<endl; return std::numeric_limits<double>:: quiet_NaN (); } return a*b; } double divide (double a, double b){ if(b==0) { cout<<"Error! Cannot divide by 0!"<<endl; return std::numeric_limits<double>:: quiet_NaN (); } return a/b; } double power (double a, double b){ return pow(a,b); } double loggy (double a) { //can't be 0 or less

C++ Program for computing the Pearson Correlation Coefficient

Image
This C++ program computes the linear correlation between two variables X and Y using the Pearson correlation coefficient. The X and Y data vectors can be read from files or they can be inserted by hand. The program is largely self-explanatory and easy to understand. (see more details here: https://en.wikipedia.org/wiki/Pearson_correlation_coefficient) # include <cmath> # include <cstdio> # include <vector> # include <iostream> # include <algorithm> # include <iomanip> # include <fstream> using namespace std ; double suma (vector<double> a) { double s = 0; for (int i = 0; i < a. size (); i++) { s = s + a[i]; } return s; } double mean (vector<double> a) { return suma(a) / a. size (); } double sqsum (vector<double> a) { double s = 0;