Hogy-Bear

Q3 final

Apr 5th, 2018
308
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. static int readNum(char *);
  6.  
  7. int main(int argc, char const *argv[]) {
  8.  
  9.   char type; //type of numbers
  10.   int amt_nums, i; // amount of numbers
  11.   double average; //aveage
  12.  
  13.   //get type and number of elements
  14.   do {
  15.     fpurge(stdin);
  16.     printf("Would you like floats(f) or ints(i) (Enter f or i): ");
  17.     scanf("%c", &type);
  18.   } while( (type!='f') && (type!='i') );
  19.   amt_nums = readNum("How many numbers would you like to store in the array");
  20.  
  21.   if (type == 'i'){ //ints
  22.     //create int array of user entered size
  23.     int total = 0, *arr = (int *)calloc( amt_nums, sizeof(*arr) );
  24.  
  25.     for (i = 0; i < amt_nums; i++) {//loop and get numbers
  26.       printf("(%d)please enter an int: ", i+1);
  27.       scanf("%d", &arr[(int)i]);
  28.       total += arr[(int)i]; //add to total
  29.     }
  30.  
  31.     average = total / amt_nums; //average
  32.     printf("the average is %lf\n", average);
  33.  
  34.     do {
  35.       //ask user if they want to add extra numbers and how many
  36.       fpurge(stdin);
  37.       printf("Would you like to add more numbers to list (y or n) ");
  38.       scanf("%c", &type);
  39.     } while( (type!='y') && (type!='n') );
  40.  
  41.     if(type == 'y'){
  42.       int addition = readNum("How many new numbers would you like to store in the array");
  43.       arr = (int *) realloc(arr, ((amt_nums+addition) * sizeof(*arr)) );
  44.  
  45.       for (i = amt_nums; i < amt_nums+addition; i++) { //loop and get the additional numbers
  46.         printf("please enter int number %d: ", i+1);
  47.         scanf("%d", &arr[i]);
  48.         total += arr[i]; //add to total
  49.       }
  50.  
  51.       average = ( total / (amt_nums+addition) ); //average
  52.       printf("The new average is now %lf\n", average);
  53.     }
  54.  
  55.  
  56.   }else{ //floats
  57.     //create float array of user entered size
  58.     float total = 0, *arr = (float *)calloc( amt_nums, sizeof(*arr) );
  59.  
  60.     for (i = 0; i < amt_nums; i++) { //loop and get floats
  61.       printf("please enter float number %d: ", i+1);
  62.       scanf("%f", &arr[i]);
  63.       total += arr[i]; //add to total
  64.     }
  65.  
  66.     average = total / amt_nums; //average
  67.     printf("the average is %lf\n", average);
  68.  
  69.     do {
  70.       //ask user if they would like to add more space to the float arrtay
  71.       fpurge(stdin);
  72.       printf("Would you like to add more numbers to list (y or n) ");
  73.       scanf("%c", &type);
  74.     } while( (type!='y') && (type!='n') );
  75.     int addition = readNum("How many new numbers would you like to store in the array");
  76.  
  77.     if (type == 'y') {
  78.       //add extra space in memory for the float array
  79.       arr = (float *) realloc(arr, ((amt_nums+addition) * sizeof(*arr)) );
  80.  
  81.       for (i = amt_nums; i < amt_nums+addition; i++) {
  82.         //get additional floats
  83.         printf("please enter float number %d: ", i+1);
  84.         scanf("%f", &arr[i]);
  85.         total += arr[i]; //add to total
  86.       }
  87.  
  88.       average = ( total / (amt_nums+addition) ); //average
  89.       printf("The new average is now %lf\n", average);
  90.     }
  91.  
  92.   }
  93.  
  94.   return 0;
  95. }
  96.  
  97. static int readNum(char *sen){
  98.   /*
  99.     get a positive integer
  100.   */
  101.   int input;
  102.   printf("%s\n", sen);
  103.   do {
  104.     printf("%s", "enter a number : ");
  105.     scanf("%d", &input);
  106.   } while(input <= 0);
  107.   return input;
  108. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment