Lets Make Tech

Correlation Program

This program allows you to enter as many data sets as you like, then it finds the correlations between all of them. 

Here is a sample output:

undefined

Here is the code. This isn't a step by step tutorial, just a tutorial through example. If you have any question feel free to ask in the comments. 

#include "stdafx.h"
#include <stdlib.h>  
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

const int size = 1000;
int variables;

void get_variables();
void get_data();
void completedataset();
void get_mean(int i);
void a(int i);
void asqr(int i);
void correlate();
float a_times_b_sum(int i, int j);

class DATASET{
public:
	int id;
	string name;
	int size_of;
	float data[size];
	float mean;
	float a[size];
	float asqr[size];
	float asqrsum;
};
DATASET dataset[size];

int _tmain(int argc, _TCHAR* argv[])
{
	while (1)
	{
		get_variables();
		get_data();
		completedataset();
		correlate();
	}
	return 0;
}

void get_variables()
{
	string tempstring;
	cout << "Number of variables: ";
	cin >> tempstring;
	if (!(istringstream(tempstring) >> variables).fail())
	{

		for (int i = 0; i < variables; i++)
		{
			cout << "Enter Variable " << i +1 << " Name:" << endl << ":";
			cin >> dataset[i].name;
		}
	}
	else
	{
		cout << "Invalid"<<endl;
	}
}

void get_data()
{
	string read;
	bool next=false;
	for (int i = 0; i < variables;i++)
	{
		dataset[i].size_of = 0;
		next = false;
		cout << "Variable: " << dataset[i].name<<endl;
		cout << "Enter Data. Enter any letter to move to the next variable." << endl << ":";
		for (int j = 0; next == false; j++)
		{
			cin >> read;

			if (!(istringstream(read) >> dataset[i].data[j]).fail())
			{
				cout << ":";
				dataset[i].size_of++;
			}
			else
			{
				cout << "Moving Along..."<<endl;
				next = true;
			}
		}
	}
}

void completedataset()
{
	for (int i = 0; i < variables; i++)
	{
		get_mean(i);
		a(i);
		asqr(i);
	}
}

void get_mean(int i)
{
	float sum = 0;
	for (int j = 0; j < dataset[i].size_of; j++)
	{
		sum = sum + dataset[i].data[j];
	}
	dataset[i].mean = sum / (dataset[i].size_of);
}

void a(int i)
{
	for (int j = 0; j < dataset[i].size_of;j++)
	{
		dataset[i].a[j] = dataset[i].data[j] - dataset[i].mean;
	}
}

void asqr(int i)
{
	dataset[i].asqrsum = 0;
	for (int j = 0; j < dataset[i].size_of; j++)
	{
		dataset[i].asqr[j] = dataset[i].a[j] * dataset[i].a[j];
		dataset[i].asqrsum = dataset[i].asqrsum + dataset[i].asqr[j];
	}

}

void correlate()
{
	float p;
	for (int i = 0; i < variables; i++)
	{
		for (int j = 0; j < variables; j++)
		{
			p=(a_times_b_sum(i, j))/(sqrt(dataset[i].asqrsum*dataset[j].asqrsum));
			cout << dataset[i].name << " & " << dataset[j].name << ": P=" << p<<endl;
		}
	}
}



float a_times_b_sum(int i , int j)
{
	float sum=0;
	for (int k = 0; k < dataset[i].size_of || k < dataset[j].size_of; k++)
	{
		sum = sum + (dataset[i].a[k] * dataset[j].a[k]);
	}
	return sum;
}

 

 

 

 

Hello World C++

Writing code can seem intimidating when you are first starting out. 

1. Have Linux. If you are currently running Linux skip this step and go to step 2, other wise:

If you are not running Linux chances are you are running windows or mac. If you want you can use a virtual box to run Linux on top of your current OS or find a different tutorial for your own OS. To do this first download virtual box, then install what ever Linux distro you want. I recommend Ubuntu for beginners.

2.  Make sure you have your compiler installed. Press ctrl+t to open your terminal and enter the following (note this only works on distros that use apt, use what ever package manager your distro uses): 

sudo apt-get update && sudo apt-get install g++

undefined

3. Open up your text editor of choice, I recommend Sublime Text. You can always use the built in gui editor gedit or a terminal based editor such as vim or nano. For this tutorial I use nano. 

undefined

4. Include the libraries we need by typing this:

#include <iostream>

5. Type the following on the next line to make it so you don't have to keep typing std later on in your code.

using namespace std;

6.Now we are going to declare our main function. 

int main()
{


}

7. Now we put code inside of the brackets of the main function. Lets write Hello World! then make it go to the next line with \n or endl. 

cout<<"Hello World!\n";

or

cout<<"Hello World!"<<endl;

8. This is all we want, it is looking for the program to return something because main is declared as an int. We will return 0.

return 0;

9. Your code should look like the following if you used the \n method. Notice the indent in the code after the parentheses. While this is not necessary for the compiler to understand what you want it to do, it helps you to keep your code organized. 

#include <iostream>
usingnamespace std;
int main()
{
	cout<<"Hello World\n";
	return 0;
}

undefined

10. Comment your code! Right now you know what you mean, but later on you won't and will have no idea how your code works with out having to think about it. Comment your code using //!

 #include <iostream> //library we are using
usingnamespace std; //so we don't have to type std in front of everything
int main() //main function
{
cout<<"Hello World\n"; //print Hello World! to the terminal
return 0; //have main return 0 as a value ending the program
}

 

11. Save your program as helloworld.cpp

12. Compile with the g++ compiler we opened before by entering the following into your terminal. 

g++ helloworld.cpp -o helloworld

undefined

13. If no error messages came back it is time to run it type the following in your terminal:

./helloworld

undefined

 

This is where most C++ hello world tutorials end... Next you will want to learn how to create more functions and call them from your main function, variables, looping, then just throw some math in there and may be some objects or structs and you are well on your way. 

 

Home