Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Arithmetic Mean by User
- * Implements a linked list that stores sequencial doubles inserted during reading
- * and calculates it's arithmetic mean.
- * The reading stops when the input is not a number.
- * The function returns a double containing the value of the mean.
- *
- * Requires #include <iostream> and <cstdlib>.
- */
- double Arithmetic_Mean_by_User()
- {
- int n=0;
- std::string input = std::string();
- struct intList{
- double num;
- struct intList *next;
- } *head, *loop;
- head = (struct intList*)malloc(sizeof(struct intList));
- head->next = NULL;
- loop = head;
- while(1)
- {
- std::cin >> input;
- if ((input.c_str())[0] < 48 || (input.c_str())[0] > 57)
- break;
- loop->num = atof(input.c_str());
- n++;
- loop->next = (struct intList*)malloc(sizeof(struct intList));
- loop->next->next = NULL;
- loop = loop->next;
- }
- if (n == 0)
- return 0;
- double mean=0;
- while (head != NULL)
- {
- mean += head->num;
- loop = head;
- head = head->next;
- free(loop);
- }
- return mean / n;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement