In this article, we will discuss how to find the variance of numbers in 2D array in C++. Before discussing its implementation, we must know about the 2D array in C++ with its syntax and example.
What is the 2D Array?
- In C++, the most primitive type of multi-dimensional array is a two-dimensional array. It can be visualized as an array of arrays.
- Another name for a two-dimensional array is a matrix. Depending on the initialization, it can have any type, including integer, character, float, etc.
- It consists of several Rows and Columns. The defined data value sequence stores every data value in a matrix format.
Syntax:
The syntax for declaring a 2D array is:
data_type array_name [x][y];
Where,
x = number of rows.
y = number of columns.
The data_type refers to the type of data that needs to be entered into the array.
2D array's elements are represented in arrx, where 'x' denotes the number of rows and 'y' denotes the number of columns of the array.
Example:
Let us take an example to illustrate the vector in C++.
#include <iostream>
using namespace std;
void printArray(int arr[][3], int row, int col) {
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}
int main() {
int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int rows = sizeof(arr) / sizeof(arr[0]);
int cols = sizeof(arr[0]) / sizeof(arr[0][0]);
printArray(arr, row, col);
return 0;
}
Output:
What is the Variance?
- A statistical evaluation of the variation between numbers in a data set is called a variance.
- Variance measures the extent to which each number in the set deviates from the mean, or average, and consequently from all other numbers in the set.
- The symbol σ2 is commonly used to represent variance.
We can use the following procedures in C++ to determine the variance of the elements in a 2D array:
- Determine the array's mean, or average, across all of its elements.
- Compute the total of the squared deviations between each element and the mean.
- To find the variance, divide the sum of the squared differences by the total number of elements.
Example 1:
Let us take an example to find the variance in a 2D array in C++.
#include <cmath>
#include <iostream>
using namespace std;
// Function to calculate the overall mean and variance of a
// 2D array
double calculateVariance(int rows, int cols,
int matrix[][3])
{
double sum = 0;
double squaredDiff = 0;
int totalCount = rows * cols;
// Calculate the sum of all elements
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
sum += matrix[i][j];
}
}
// Calculate the overall mean
double overallMean = sum / totalCount;
// Calculate the squared difference from the mean for
// each element
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
squaredDiff
+= pow(matrix[i][j] - overallMean, 2);
}
}
// Calculate variance
double variance = squaredDiff / totalCount;
return variance;
}
int main()
{
const int rows = 3;
const int cols = 3;
int matrix[rows][cols] = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
double result = calculateVariance(rows, cols, matrix);
cout << "Variance: " << result << endl;
return 0;
}
Output:
Complexity Analysis:
Time Complexity: O (N*M)
Space Complexity: O (1)
Example 2:
Let us take another example to find the variance in a 2D array in C++.
#include <iostream>
#include <cmath>
using namespace std;
double calculateMean(int arr[][4], int rows, int cols) {
double sum = 0;
int count = 0;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
sum += arr[i][j];
count++;
}
}
return sum/count;
}
double calculateVariance(int arr[][4], int rows, int cols) {
double mean = calculateMean(arr, rows, cols);
double sumSquaredDiff = 0;
int count = 0;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
double diff = arr[i][j] - mean;
sumSquaredDiff += diff * diff;
count++;
}
}
return sumSquaredDiff / count;
}
int main() {
int arr[2][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8}
};
int rows = sizeof(arr) / sizeof(arr[0]);
int cols = sizeof(arr[0]) / sizeof(arr[0][0]);
double variance = calculateVariance(arr, rows, cols);
cout << "Variance: " << variance << endl;
return 0;
}
Output:
Complexity Analysis:
Time Complexity: O (N*M)
Space Complexity: O (1)