SHARE
TWEET

Median Vs. Average Arduino

sspence65 Jun 30th, 2017 (edited) 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void swap(int *p,int *q) {
  2.    int t;
  3.    
  4.    t=*p;
  5.    *p=*q;
  6.    *q=t;
  7. }
  8.  
  9. void sort(int a[],int n) {
  10.    int i,j,temp;
  11.  
  12.    for(i=0;i<n-1;i++) {
  13.       for(j=0;j<n-i-1;j++) {
  14.          if(a[j]>a[j+1])
  15.             swap(&a[j],&a[j+1]);
  16.       }
  17.    }
  18. }
  19.  
  20. void setup() {
  21.   // put your setup code here, to run once:
  22. Serial.begin(9600);
  23. }
  24.  
  25.  
  26.  
  27. void loop() {
  28.    int a[] = {150,200,0};
  29.    int n = 3;
  30.    int sum,i;
  31.  
  32.    sort(a,n);
  33.    
  34.    n = (n+1) / 2 - 1;  // -1 as array indexing in C starts from 0
  35.    int avg = (a[0]+a[1]+a[2])/3;
  36.    Serial.print("Median = ");
  37.    Serial.println(a[n]);
  38.    Serial.print("Average = ");
  39.    Serial.println(avg);
  40.    delay(5000);
  41.    
  42. }
RAW Paste Data
Top