Skip to content

Instantly share code, notes, and snippets.

@potikanond
Created April 2, 2019 14:56
Show Gist options
  • Save potikanond/b1ec19553a5e8dc1be9446f727f637af to your computer and use it in GitHub Desktop.
Save potikanond/b1ec19553a5e8dc1be9446f727f637af to your computer and use it in GitHub Desktop.
ENGR201 - Sample c++ program for calculating SD using function
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
void myFunc1(int[], int);
double myFunc2(int[], int);
double myFunc3(int[], int, double);
int main() {
int N=5,data[N] = {1,3,5,7,9};
myFunc1(data,N);
double x1 = myFunc2(data,N);
double x2 = myFunc3(data,N,x1);
printf("Avg: %.3f, SD: %.3f", x1, x2);
return 0;
}
void myFunc1(int x[], int s) {
for (int i=0; i<s; i++)
cout << x[i] << " ";
cout << endl;
}
double myFunc2(int x[], int s) {
double sum=0;
for (int i=0; i<s; i++)
sum+=x[i];
return sum/s;
}
double myFunc3(int x[], int s, double x_bar) {
double sum_sqr=0;
for (int i=0; i<s; i++)
sum_sqr += pow(x[i]-x_bar, 2);
return sqrt(sum_sqr/s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment