Advertisement
Guest User

Untitled

a guest
Sep 8th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node {
  5.     int val;
  6.     struct node * next;
  7. } node;
  8.  
  9. node add(node *list, int x){
  10.   if(list == NULL){
  11.     list = malloc(sizeof(node));
  12.     list->next = x;
  13.   }
  14.   node *newNode = list;
  15.   while (newNode->next != NULL) {
  16.       newNode = newNode->next;
  17.   }
  18.   newNode->next = malloc(sizeof(node));
  19.   newNode->next->val = x;
  20.   newNode->next->next = NULL;
  21. }
  22.  
  23. double mean(node *list){
  24.    int count = 0;
  25.    double sum = 0;
  26.   while (list != NULL) {
  27.     sum += list->val;
  28.     count += 1;
  29.     list = list->next;
  30.   }
  31.   return sum/count;
  32. }
  33.  
  34. double smallest(node list){
  35.   sortList(list);
  36.   return list->val;
  37. }
  38.  
  39. double largest(node list){
  40.   sortList(list);
  41.   node *newNode = list;
  42.   while(newNode->next != NULL){
  43.         newNode = newNode->next;
  44.   }
  45.   return newNode->val;
  46. }
  47.  
  48. double median(node list){
  49.  
  50. }
  51.  
  52. void sortList(node *list) {
  53.     node *tmpFirst = list;
  54.     node *tmpNext = list->next;
  55.     double tmp;
  56.     while(tmpNext != NULL){
  57.          while(tmpNext != tmpFirst){
  58.               if(tmpNext->val < tmpFirst->val){
  59.                       tmp = tmpFirst->val;
  60.                       tmpFirst->val = tmpNext->val;
  61.                       tmpNext->val = tmp;
  62.               }
  63.               tmpFirst = tmpFirst->next;
  64.           }
  65.         tmpFirst = list;
  66.         tmpNext = tmpNext->next;
  67.     }
  68. }
  69.  
  70. int main(int argc, char *argv[]){
  71.  
  72.   node *list = NULL;
  73.   list = malloc(sizeof(node));
  74.   if (list == NULL) {
  75.       return 1;
  76.   }
  77.  
  78.   double upperBound;
  79.   double lowerBound;
  80.  
  81.   if(argc == 3){
  82.     sscanf(argv[1], "%lf", &lowerBound);
  83.     sscanf(argv[2], "%lf", &upperBound);
  84.     printf("Lower Bounds: %f\n", lowerBound);
  85.     printf("Upper Bounds: %f\n", upperBound);
  86.   }else if(argc == 2){
  87.     sscanf(argv[1], "%lf", &lowerBound);
  88.     printf("Lower Bounds: %f\n", lowerBound);
  89.   }else if(argc == 1){
  90.     printf("No bounds\n");
  91.   }else{
  92.     printf("error\n");
  93.     return 1;
  94.   }
  95.  
  96.   double number;
  97.  
  98.   while(scanf("%lf\n", &number) == 1){
  99.     if((argc == 3 && number >= lowerBound && number <= upperBound) || (argc == 2 && number >= lowerBound) || argc == 1){
  100.       add(list, number);
  101.     }
  102.   }
  103.  
  104.   double small = smallest(list);
  105.   double large = largest(list);
  106.   double meanVal = mean(list);
  107.   //int median = median(list);
  108.  
  109.   printf("Mean: %lf", mean);
  110.  
  111.   return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement