Advertisement
trideceth12

PSP0 llist

Mar 29th, 2014
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.44 KB | None | 0 0
  1. /**
  2. @file
  3. @brief Mean and standard deviation calculator
  4.  
  5. This program calculates the mean and (population) standard deviation, of a
  6. sequence of real numbers, input to the program through stdin. The program
  7. accepts comma and newline separated values.
  8.  
  9. As per the specification, the program accepts input from a file - simply pipe
  10. the file into the program's stdin.
  11.  
  12. The specification suggested a linked list - a huge overengineering of this
  13. trivial problem. Luckily the specification also allowed "variables" and "other
  14. data structure(s)" to hold the data. I took that option and used stack'd
  15. variables to hold the data - as only the data from the nth element is relavant
  16. to the calculation.
  17.  
  18. @author Jake Sebastian-Jones
  19. */
  20. #include<stdio.h>
  21. #include<math.h>
  22.  
  23. /**
  24. Linked list with queue and stack operations.
  25.  
  26. Written by Jake Sebastian-Jones
  27. jake.sebastian-jones@linux.com
  28. Released under the WTFPL
  29. */
  30.  
  31.  
  32. #include<stdlib.h>
  33.  
  34. struct ll_node {
  35.     struct ll_node *prev;
  36.     struct ll_node *next;
  37.     void *value;
  38. };
  39.  
  40. struct llist{
  41.     struct ll_node *head;
  42.     struct ll_node *tail;
  43.     int len;
  44. };
  45.  
  46. inline static void ll_node_init(struct ll_node *n) {
  47.     n->prev = NULL;
  48.     n->next = NULL;
  49.     n->value = NULL;
  50. }
  51.  
  52. inline static void llist_init(struct llist *l) {
  53.     l->head = NULL;
  54.     l->tail = NULL;
  55.     l->len = 0;
  56. };
  57.  
  58. inline static void llist_enqueue(struct llist *l, struct ll_node *n) {
  59.     if(l->head == NULL) {
  60.         l->head = n;
  61.         l->tail = n;
  62.         l->len++;
  63.     }
  64.     else {
  65.         l->tail->next = n;
  66.         n->prev = l->tail;
  67.         l->tail = n;
  68.     }
  69. };
  70.  
  71. inline static void llist_append(struct llist *l, struct ll_node *n) {
  72.     llist_enqueue(l, n);
  73. }
  74.  
  75. inline static void llist_push(struct llist *l, struct ll_node *n) {
  76.     llist_enqueue(l, n);
  77. }
  78.  
  79. inline static struct ll_node* llist_dequeue(struct llist *l) {
  80.     struct ll_node *node = l->head;
  81.     if(l->head == l->tail) {
  82.         l->head = l->tail = NULL;
  83.     }
  84.     else if(l->head != NULL) {
  85.         l->head = l->head->next;
  86.     }
  87.     return node;
  88. }
  89.  
  90. inline static struct ll_node* llist_pop(struct llist *l) {
  91.     struct ll_node *node = l->tail;
  92.     if(l->head == l->tail) {
  93.         l->head = l->tail = NULL;
  94.     }
  95.     else if(l->tail != NULL) {
  96.         l->tail = l->tail->prev;
  97.         l->tail->next = NULL;
  98.     }
  99.     return node;
  100. }
  101.  
  102. inline static void ll_freelinks(struct llist *l) {
  103.     while(l->head != NULL) {
  104.         struct ll_node *node = llist_pop(l);
  105.         free(node);
  106.     }
  107. }
  108.  
  109. inline static void append_list(struct llist *from, struct llist *to) {
  110.  
  111.     struct ll_node *node = from->head;
  112.     while(node != NULL) {
  113.         struct ll_node *next = malloc(sizeof(*next));
  114.         ll_node_init(next);
  115.         next->value = node->value;
  116.         llist_append(to, next);
  117.         node = node->next;
  118.     }
  119.  
  120. }
  121.  
  122.  
  123.  
  124.  
  125. int main(void) {
  126.     double x, n, s2, sum, mean;
  127.  
  128.     fscanf(stdin, "%lf", &x);
  129.     sum = mean = x;
  130.     n = 1.0;
  131.  
  132.     struct llist list;
  133.     llist_init(&list);
  134.  
  135.     while( fscanf(stdin, "%lf", &x) == 1) {
  136.         sum += x;
  137.         n++;
  138.  
  139.         s2 = ( (n-2.0) / (n-1.0) * s2)  + ( 1.0 / n) * pow(x - mean, 2.0);
  140.        
  141.         struct ll_node node;
  142.         ll_node_init(&node);
  143.         node.value = malloc(4);
  144.         *((int*)(node.value)) = x;
  145.         llist_push(&list, &node);
  146.  
  147.         mean = sum / n;
  148.     }
  149.  
  150.     printf("mean = %.2lf\nσ = %.2lf\n", mean, sqrt(s2));
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement