Hogy-Bear icon

Q3 final

Hogy-Bear | PRO | 04/05/18 05:02:31 PM UTC | 0 ⭐ | 251 👁️ | Never ⏰ | []
C |

3.19 KB

|

None

|

0 👍

/

0 👎

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
static int readNum(char *);
 
int main(int argc, char const *argv[]) {
 
  char type; //type of numbers
  int amt_nums, i; // amount of numbers
  double average; //aveage
 
  //get type and number of elements
  do {
    fpurge(stdin);
    printf("Would you like floats(f) or ints(i) (Enter f or i): ");
    scanf("%c", &type);
  } while( (type!='f') && (type!='i') );
  amt_nums = readNum("How many numbers would you like to store in the array");
 
  if (type == 'i'){ //ints
    //create int array of user entered size
    int total = 0, *arr = (int *)calloc( amt_nums, sizeof(*arr) );
 
    for (i = 0; i < amt_nums; i++) {//loop and get numbers
      printf("(%d)please enter an int: ", i+1);
      scanf("%d", &arr[(int)i]);
      total += arr[(int)i]; //add to total
    }
 
    average = total / amt_nums; //average
    printf("the average is %lf\n", average);
 
    do {
      //ask user if they want to add extra numbers and how many
      fpurge(stdin);
      printf("Would you like to add more numbers to list (y or n) ");
      scanf("%c", &type);
    } while( (type!='y') && (type!='n') );
 
    if(type == 'y'){
      int addition = readNum("How many new numbers would you like to store in the array");
      arr = (int *) realloc(arr, ((amt_nums+addition) * sizeof(*arr)) );
 
      for (i = amt_nums; i < amt_nums+addition; i++) { //loop and get the additional numbers
        printf("please enter int number %d: ", i+1);
        scanf("%d", &arr[i]);
        total += arr[i]; //add to total
      }
 
      average = ( total / (amt_nums+addition) ); //average
      printf("The new average is now %lf\n", average);
    }
 
 
  }else{ //floats
    //create float array of user entered size
    float total = 0, *arr = (float *)calloc( amt_nums, sizeof(*arr) );
 
    for (i = 0; i < amt_nums; i++) { //loop and get floats
      printf("please enter float number %d: ", i+1);
      scanf("%f", &arr[i]);
      total += arr[i]; //add to total
    }
 
    average = total / amt_nums; //average
    printf("the average is %lf\n", average);
 
    do {
      //ask user if they would like to add more space to the float arrtay
      fpurge(stdin);
      printf("Would you like to add more numbers to list (y or n) ");
      scanf("%c", &type);
    } while( (type!='y') && (type!='n') );
    int addition = readNum("How many new numbers would you like to store in the array");
 
    if (type == 'y') {
      //add extra space in memory for the float array
      arr = (float *) realloc(arr, ((amt_nums+addition) * sizeof(*arr)) );
 
      for (i = amt_nums; i < amt_nums+addition; i++) {
        //get additional floats
        printf("please enter float number %d: ", i+1);
        scanf("%f", &arr[i]);
        total += arr[i]; //add to total
      }
 
      average = ( total / (amt_nums+addition) ); //average
      printf("The new average is now %lf\n", average);
    }
 
  }
 
  return 0;
}
 
static int readNum(char *sen){
  /*
    get a positive integer
  */
  int input;
  printf("%s\n", sen);
  do {
    printf("%s", "enter a number : ");
    scanf("%d", &input);
  } while(input <= 0);
  return input;
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎