Rodex_

LoverSum_MyVersion

Feb 26th, 2017
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. ///Add the struct for my list which i need to put in input values.
  5. struct list
  6. {
  7.     double number;
  8.     struct list *next;
  9. };
  10.  
  11. typedef struct list LIST;
  12.  
  13. LIST *top = NULL;
  14. LIST *tmp = NULL;
  15.  
  16.  
  17. ///Function to add the new Input Value and the List and link them together.
  18. ///Allocates Memory for the new Nodes in the List.
  19. void add_element(double number)
  20. {
  21.     if(top == NULL)
  22.     {
  23.         top = (LIST *)malloc(sizeof(LIST));
  24.         top->number = number;
  25.         top->next = NULL;
  26.     }
  27.     else
  28.     {
  29.         tmp = (LIST *)malloc(sizeof(LIST));
  30.         tmp->next = top;
  31.         tmp->number = number;
  32.         top = tmp;
  33.     }
  34. }
  35.  
  36.  
  37. ///Count the single Positions in the List and return the Number of Values inside.
  38. int size(LIST *i)
  39. {
  40.     int size = 0;
  41.     for(i = top; i != NULL; i = i->next)
  42.     {
  43.         size ++;
  44.     }
  45.     return size;
  46. }
  47.  
  48.  
  49. ///Calculate all Values in the List and return the sum of all of them.
  50. double sum(LIST *i)
  51. {
  52.     double value = 0;
  53.     for(i = top; i != NULL; i = i->next)
  54.     {
  55.         value = i->number+value;
  56.     }
  57.     return value;
  58. }
  59.  
  60.  
  61. ///Clears the memory of the last value in the list and link the list again to the previous value.
  62. void clear_list()
  63. {
  64.     if(top->next == NULL)
  65.     {
  66.         free(top);
  67.         top = NULL;
  68.     }
  69.     else
  70.     {
  71.         LIST *n = top->next;
  72.         free(top);
  73.         top = n;
  74.     }
  75.  
  76. }
  77.  
  78. int main()
  79. {
  80.     double input;
  81.     double temp;
  82.     double durchschnitt;
  83.  
  84.     ///Scan the Value the User want to calculate until the Input is <= 0.
  85.     while(input > 0.0000)
  86.     {
  87.         scanf("%lf", &input);
  88.         add_element(input);
  89.     }
  90.  
  91.     ///Cleared the last value in the List with which the User canceled the Input.
  92.     clear_list();
  93.  
  94.     ///Calculate the Average of the Input Numbers.
  95.     durchschnitt = sum(top)/size(top);
  96.  
  97.     ///Find out which Numbers are smaller than average and write the sum on an double int.
  98.     for(LIST *i = top; i != NULL; i = i->next)
  99.     {
  100.         if(i->number < durchschnitt)
  101.         {
  102.             temp = temp + i->number;
  103.         }
  104.     }
  105.  
  106.     printf("%.2f", temp);
  107.  
  108.     ///Free the memory which was allocated for the Input list.
  109.     for(LIST *i = top; i != NULL; i = i->next)
  110.     {
  111.         clear_list();
  112.     }
  113.  
  114.     return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment