Skip to content

Commit

Permalink
Add C++ example.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hendrik Borchert committed Aug 5, 2024
1 parent b41722b commit 628d47c
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ All functions for higher moments (e.g., kurtosis) will also compute all lower mo
and reduces computational overhead, making it efficient to obtain all necessary statistical
measures with minimal passes through the data.

**Example**
**Example Usage in C**
```C
#include <stdio.h>
#include "rstats.h"
Expand Down Expand Up @@ -122,6 +122,38 @@ int main() {
return 0;
}
```
**Example Usage in C++**
```CXX
#include <iostream> // Use C++ iostream for printing

// Include the C header file with extern "C"
extern "C" {
#include "rstats.h"
}

int main() {
double buffer[5] = {0}; // Initialize buffer for mean, variance, skewness, kurtosis
double data[20] = {5.0, 3.2, 4.1, 2.9, 6.5, 4.3, 5.1, 3.8, 4.6, 2.7,
3.3, 5.7, 4.9, 2.4, 3.6, 5.8, 4.0, 6.2, 3.4, 5.3};
double w = 1.0;

// Update statistics with new values
for (int i = 0; i < 20; ++i) {
rstats_kurtosis(data[i], w, buffer); // This will also update mean, variance, and skewness
}

// Finalize and get results
double results[4];
rstats_kurtosis_finalize(results, buffer);

std::cout << "Mean: " << results[0] << std::endl;
std::cout << "Variance: " << results[1] << std::endl;
std::cout << "Skewness: " << results[2] << std::endl;
std::cout << "Kurtosis: " << results[3] << std::endl;

return 0;
}
```

## License

Expand Down

0 comments on commit 628d47c

Please sign in to comment.