Calculating a floating-point average using pointers : Pointer Double « Pointer « C / ANSI-C






Calculating a floating-point average using pointers

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main()
{
  double *values = NULL;     
  double sum = 0.0;          
  int capacity = 5;          
  int i = 0;                 

  values= (double*)malloc((capacity)*sizeof(double));
  if(values == NULL){
     printf("Memory allocation failed. Terminating program.");
  }
  for(i=0;i<capacity;i++){
     values[i] = i;
     printf(" %.2lf",values[i]);
  }
  for(i = 0 ; i<capacity ; i++)
    sum += *(values+i);

  /* Output the average */
  printf("\n The average of the the values you entered is %.2lf.\n", sum/capacity);
  free(values);     /* We are done - so free the memory */
}


 

           
       








Related examples in the same category

1.A function to calculate an averageA function to calculate an average