
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to implement t-test
In this tutorial, we will be discussing a program to implement t-test.
The t-test of the student’s T test is used to compare two means and tell if both of them are similar or different. Along with this, t-test also helps to determine how large the differences are to know the reason for the change.
Example
#include <bits/stdc++.h> using namespace std; //calculating mean float calc_mean(float arr[], int n){ float sum = 0; for (int i = 0; i < n; i++) sum = sum + arr[i]; return sum / n; } //calculating standard deviation float calc_deviation(float arr[], int n){ float sum = 0; for (int i = 0; i < n; i++) sum = sum + (arr[i] - calc_mean(arr, n)) * (arr[i] - calc_mean(arr, n)); return sqrt(sum / (n - 1)); } //finding t-test of two data float calc_ttest(float arr1[], int n, float arr2[], int m){ float mean1 = calc_mean(arr1, n); float mean2 = calc_mean(arr2, m); float sd1 = calc_deviation(arr1, n); float sd2 = calc_deviation(arr2, m); float t_test = (mean1 - mean2) / sqrt((sd1 * sd1) / n + (sd2 * sd2) / m); return t_test; } int main(){ float arr1[] = { 10, 20, 30, 40, 50 }; int n = sizeof(arr1) / sizeof(arr1[0]); float arr2[] = { 1, 29, 46, 78, 99 }; int m = sizeof(arr2) / sizeof(arr2[0]); cout << calc_ttest(arr1, n, arr2, m) << endl; return 0; }
Output
-1.09789
- Related Articles
- C++ Program to Implement the Solovay-Strassen Primality Test to Check if a Given Number is Prime
- C++ Program to Implement the Rabin-Miller Primality Test to Check if a Given Number is Prime
- How to find the power of t test in R?
- How to perform paired t test for multiple columns in R?
- How to extract the p-value from t test in R?
- How to find the sample size for t test in R?
- C++ Program to Implement Vector
- C# program to implement FizzBuzz
- C++ Program to Implement Trie
- C++ Program to Implement Stack
- C++ Program to Implement Treap
- C++ Program to Implement Dequeue
- C++ Program to Implement Queue
- Java Program to Implement LinkedList
- C program to implement CHECKSUM

Advertisements