Rodex_

LowerSum_Kollege_Liste

Feb 26th, 2017
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.29 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node{
  5.     double element;
  6.     struct node *next;
  7. }node_t;
  8.  
  9. node_t *head = NULL;
  10.  
  11. node_t *add_first(double value, node_t *l){
  12.     node_t *newNode = (node_t*)malloc(sizeof(node_t));
  13.     newNode->element =value;
  14.     newNode->next=l;
  15.     return newNode;
  16. }
  17.  
  18. int getSize(node_t *l){
  19.     int size=0;
  20.     for(node_t *i=head; i!=NULL; i=i->next){
  21.         ++size;
  22.     }
  23.     return size;
  24. }
  25. double getSum(node_t *l){
  26.     double value =0;
  27.     for(node_t *i=head; i!=NULL; i=i->next){
  28.         value += i->element;
  29.     }
  30.     return value;
  31. }
  32. double pop() {
  33.     return head->element;
  34. }
  35. node_t *remove_first(node_t *l){
  36.     node_t *newNode = l->next;
  37.     free(l);
  38.     return newNode;
  39. }
  40. int main()
  41. {
  42.     double input;
  43.     double durchschnitt;
  44.     double temp;
  45.     while(input > 0){
  46.  
  47.         scanf("%lf", &input);
  48.         head = add_first(input,head);
  49.     }
  50.  
  51.     //remove negative numbers
  52.     head = remove_first(head);
  53.  
  54.     durchschnitt = getSum(head)/getSize(head);
  55.  
  56.     for(node_t *i=head; i!=NULL; i=i->next){
  57.         if(i->element < durchschnitt){
  58.             temp += i->element;
  59.         }
  60.     }
  61.     printf("%.2f", temp);
  62.     for(node_t *i=head; i!=NULL; i=i->next){
  63.         head=remove_first(head);
  64.     }
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment