Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct node {
- int val;
- struct node * next;
- } node;
- node add(node *list, int x){
- if(list == NULL){
- list = malloc(sizeof(node));
- list->next = x;
- }
- node *newNode = list;
- while (newNode->next != NULL) {
- newNode = newNode->next;
- }
- newNode->next = malloc(sizeof(node));
- newNode->next->val = x;
- newNode->next->next = NULL;
- }
- double mean(node *list){
- int count = 0;
- double sum = 0;
- while (list != NULL) {
- sum += list->val;
- count += 1;
- list = list->next;
- }
- return sum/count;
- }
- double smallest(node list){
- sortList(list);
- return list->val;
- }
- double largest(node list){
- sortList(list);
- node *newNode = list;
- while(newNode->next != NULL){
- newNode = newNode->next;
- }
- return newNode->val;
- }
- double median(node list){
- }
- void sortList(node *list) {
- node *tmpFirst = list;
- node *tmpNext = list->next;
- double tmp;
- while(tmpNext != NULL){
- while(tmpNext != tmpFirst){
- if(tmpNext->val < tmpFirst->val){
- tmp = tmpFirst->val;
- tmpFirst->val = tmpNext->val;
- tmpNext->val = tmp;
- }
- tmpFirst = tmpFirst->next;
- }
- tmpFirst = list;
- tmpNext = tmpNext->next;
- }
- }
- int main(int argc, char *argv[]){
- node *list = NULL;
- list = malloc(sizeof(node));
- if (list == NULL) {
- return 1;
- }
- double upperBound;
- double lowerBound;
- if(argc == 3){
- sscanf(argv[1], "%lf", &lowerBound);
- sscanf(argv[2], "%lf", &upperBound);
- printf("Lower Bounds: %f\n", lowerBound);
- printf("Upper Bounds: %f\n", upperBound);
- }else if(argc == 2){
- sscanf(argv[1], "%lf", &lowerBound);
- printf("Lower Bounds: %f\n", lowerBound);
- }else if(argc == 1){
- printf("No bounds\n");
- }else{
- printf("error\n");
- return 1;
- }
- double number;
- while(scanf("%lf\n", &number) == 1){
- if((argc == 3 && number >= lowerBound && number <= upperBound) || (argc == 2 && number >= lowerBound) || argc == 1){
- add(list, number);
- }
- }
- double small = smallest(list);
- double large = largest(list);
- double meanVal = mean(list);
- //int median = median(list);
- printf("Mean: %lf", mean);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement