C/Pointer/Pointer Double
A function to calculate an average
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
double average(double data[], int count)
{
int i = 0;
double sum = 0.0;
for( i = 0 ; i<count ; sum += data[i++])
;
return sum/count;
}
int main()
{
double data[3];
data[0] = 1;
data[1] = 2;
data[2] = 3;
printf("\nThe average of thew values you entered is %10.2lf\n", average(data, 3));
free(data);
}
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 */
}