
- 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
Program to find covariance in C++
In this tutorial, we will be discussing a program to find covariance.
For this we will be provided with two sets of random variables. Our task is to calculate their covariance i.e, the measure of how much those two values differ together.
Example
#include<bits/stdc++.h> using namespace std; //function to find mean float mean(float arr[], int n){ float sum = 0; for(int i = 0; i < n; i++) sum = sum + arr[i]; return sum / n; } //finding covariance float covariance(float arr1[], float arr2[], int n){ float sum = 0; for(int i = 0; i < n; i++) sum = sum + (arr1[i] - mean(arr1, n)) * (arr2[i] - mean(arr2, n)); return sum / (n - 1); } int main(){ float arr1[] = {65.21, 64.75, 65.26, 65.76, 65.96}; int n = sizeof(arr1) / sizeof(arr1[0]); float arr2[] = {67.25, 66.39, 66.12, 65.70, 66.64}; int m = sizeof(arr2) / sizeof(arr2[0]); if (m == n) cout << covariance(arr1, arr2, m); return 0; }
Output
-0.0580511
- Related Articles
- Covariance and Contravariance in C#
- How to find the covariance between two vectors in R?
- Write a program in Python to compute grouped data covariance and calculate grouped data covariance between two columns in a given dataframe
- How to find the covariance using the Series.cov() Method in Pandas?
- How to create a covariance matrix in R?
- Difference between Covariance and Correlation
- Program to find parity in C++
- How is Covariance and Correlation used in Portfolio Theory?
- Program to find compound interest in C++
- Program to find correlation coefficient in C++
- Program to find Cullen Number in C++
- Program to find HCF iteratively in C++
- Program to find Star number in C++
- C++ Program to Find Factorial
- C++ Program to Find GCD

Advertisements