#include <stdio.h>
#include <stdlib.h>
///Add the struct for my list which i need to put in input values.
struct list
{
double number;
struct list *next;
};
typedef struct list LIST;
LIST *top = NULL;
LIST *tmp = NULL;
///Function to add the new Input Value and the List and link them together.
///Allocates Memory for the new Nodes in the List.
void add_element(double number)
{
if(top == NULL)
{
top = (LIST *)malloc(sizeof(LIST));
top->number = number;
top->next = NULL;
}
else
{
tmp = (LIST *)malloc(sizeof(LIST));
tmp->next = top;
tmp->number = number;
top = tmp;
}
}
///Count the single Positions in the List and return the Number of Values inside.
int size(LIST *i)
{
int size = 0;
for(i = top; i != NULL; i = i->next)
{
size ++;
}
return size;
}
///Calculate all Values in the List and return the sum of all of them.
double sum(LIST *i)
{
double value = 0;
for(i = top; i != NULL; i = i->next)
{
value = i->number+value;
}
return value;
}
///Clears the memory of the last value in the list and link the list again to the previous value.
void clear_list()
{
if(top->next == NULL)
{
free(top);
top = NULL;
}
else
{
LIST *n = top->next;
free(top);
top = n;
}
}
int main()
{
double input;
double temp;
double durchschnitt;
///Scan the Value the User want to calculate until the Input is <= 0.
while(input > 0.0000)
{
scanf("%lf", &input);
add_element(input);
}
///Cleared the last value in the List with which the User canceled the Input.
clear_list();
///Calculate the Average of the Input Numbers.
durchschnitt = sum(top)/size(top);
///Find out which Numbers are smaller than average and write the sum on an double int.
for(LIST *i = top; i != NULL; i = i->next)
{
if(i->number < durchschnitt)
{
temp = temp + i->number;
}
}
printf("%.2f", temp);
///Free the memory which was allocated for the Input list.
for(LIST *i = top; i != NULL; i = i->next)
{
clear_list();
}
return 0;
}
Comments