Advertisement
sheyshya1

chi square

Mar 29th, 2022
768
-1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 1
  1. #include<iostream>
  2.  
  3.  
  4. using namespace std;
  5.  
  6. class chisquare
  7. {
  8.     private:
  9.         int n, observed[20],i,N,Expected;
  10.         float Calculation[20],final,critical;
  11.  
  12.     public:
  13.         chisquare() //constructor to initialize values
  14.         {
  15.             N=0;
  16.             final=0.00;
  17.         }
  18.  
  19.         void getdata() //getting observed frequency
  20.         {
  21.             cout<<"How many numbers:?"<<endl;
  22.             cin>>n;
  23.             cout<<"Enter the observed frequency:"<<endl;
  24.             for(i=0;i<n;i++)
  25.             {
  26.                 cout<<"Enter "<<i+1<<"th number: "<<endl;
  27.                 cin>>observed[i];
  28.             }
  29.         }
  30.  
  31.  
  32.         void calculation() //calculation of N(total frequency), Calculation(((Oi-Ei)^2/Ei)),final(?)
  33.         {
  34.             for(i=0;i<n;i++)
  35.             {
  36.                 N=N+observed[i];
  37.             }
  38.             Expected=N/n;
  39.             for(i=0;i<n;i++)
  40.             {
  41.                 Calculation[i]=(float)((observed[i]-Expected)*(observed[i]-Expected))/Expected;
  42.                 final=final+Calculation[i];
  43.             }
  44.         }
  45.  
  46.         void display() //display in tabulated format
  47.         {
  48.             cout<<"S.No"<<"\t";
  49.             cout<<"Oi"<<"\t";
  50.             cout<<"Ei"<<"\t";
  51.             cout<<"((Oi-Ei)*(Oi-Ei))/Ei"<<endl;
  52.             for(i=0;i<n;i++)
  53.             {
  54.                 cout<<i+1<<"\t";
  55.                 cout<<observed[i]<<"\t";
  56.                 cout<<Expected<<"\t";
  57.                 cout<<Calculation[i]<<endl;
  58.             }
  59.             cout<<"-------------------------------------------------------"<<endl;
  60.             cout<<"Total sum of Oi:"<<N;
  61.             cout<<"calculation is: "<<final;
  62.             cout<<endl;
  63.         }
  64.  
  65.  
  66.         void conclusion() //compare tabulated and calculated value and conclude if Ho is accepted.
  67.         {
  68.             cout<<endl;
  69.             cout<<"Enter the critical value:"<<endl;
  70.             cin>>critical;
  71.             if(final<critical)
  72.             {
  73.                 cout<<"The test is accepted"<<endl;
  74.             }
  75.             else
  76.             {
  77.                 cout<<"The test is rejected"<<endl;
  78.             }
  79.         }
  80.  
  81. };
  82. int main()
  83. {
  84.     chisquare c;
  85.     c.getdata();
  86.     c.calculation();
  87.     c.display();
  88.     c.conclusion();
  89.  
  90.     return(0);
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement