Run Code  | API  | Code Wall  | Misc  | Feedback  | Login  | Theme  | Privacy  | Patreon 

NonparaU

//g++  7.4.0

//Nonparametric Test: The Mann-Whitney U Test
//code is created by Rezaul Hoque on January 26,2021
//please contact at jewelmrh@yahoo.com
 #include <iostream>
#include <cmath>
using namespace std;
//template sort function to sort the combined array
template <class T>
void sort(T* z, int n)
{

for(int i=1;i<n;i++)
{
T temp;
for(int j=0;j<n-i;j++)
if(z[j]>z[j+1]) {
temp = z[j];
z[j]=z[j+1];
z[j+1]= temp;}
}
}


int main()
{
int n=18, n1=8,n2=10;
float a[n1]={18.3,16.4,22.7,17.8,18.9,25.3,16.1,24.2};
float b[n2]={12.6,14.1,20.5,10.7,15.9,19.6,12.9,15.2,11.8,14.7};

float c[n]={18.3,16.4,22.7,17.8,18.9,25.3,16.1,24.2,12.6,14.1,20.5,10.7,15.9,19.6,12.9,15.2,11.8,14.7};
cout <<"Before sorting the two samples and the combined population look like:\n";
cout<<"First sample:\n";
for (int i=0;i<n1;i++){
cout <<" "<<a[i];
cout <<endl;
}
cout<<"Second sample:\n";
for (int i=0;i<n2;i++){
cout <<" "<<b[i];
cout <<endl;
}
cout<<"The combined population:\n";
for (int i=0;i<n;i++){
cout <<" "<<c[i];
cout <<endl;
}
sort(c,18);
cout <<"After sorting the population looks like:\n";
for (int i=0;i<n;i++){
cout <<"\n "<<c[i]<<" and it is ranked at "<<i+1;
cout <<endl;
}
float sam1R[n1]={12,10,16,11,13,18,9,17};
float sam2R[n2]={3,5,15,1,8,14,4,7,2,6};
float R1=0,R2=0;
for(int i=0;i<n1; i++){
R1 += sam1R[i];}
for(int i=0;i<n2; i++){
R2 += sam2R[i];}
cout<<"R1:\t"<<R1<<endl;
float U,  meanU, varU,sigU,zed;
U= n1*n2+(n1*(n1+1))/2-R1;
cout<<"U :\t"<<U<<endl;
meanU=(n1*n2)/2;
varU=(n1*n2*(n1+n2+1))/12;
sigU=sqrt(varU);
zed= (U-meanU)/sigU;
cout<<"The z score:\t"<<zed<<endl;
if(zed <= 1.96 && zed >= -1.96) cout<<"Accept null hypothesis , there is no difference between two samples at the 0.05 level.\n";
else   
       cout <<"Reject null hypothesis , there is difference between two samples at the 0.05 level.\n";

return 0;
}
 run  | edit  | history  | help 0