#include <stdio.h>
#include <stdlib.h>
int parabola1(int);
int *calc(int low, int high, int (*f)(int), int *size, int *min, int *max){
/*
#input
low, high : range {x| low <= x <= high}
f : function
#output
*size : Size of array
*min : Minimum value of f(x)
*max : Maximum value
return : pointer to first element of int num[*size]
NULL if this can not be ensured.
*/
int i, x, *nums;
*size = high - low + 1;
*max = *min = f(low);//value of the provisional
if(NULL==(nums=malloc(*size*sizeof(*nums)))){
return NULL;//max and min are unavailable
}
for(i = 0, x = low; x <= high; ++x, ++i){
nums[i] = f(x);
if(nums[i] > *max)
*max = nums[i];
if(nums[i] < *min)
*min = nums[i];
}
return nums;
}
int main(void){
int i, t;
int *nums, size;
int low=-3, high=9, min, max;
nums = calc(low, high, parabola1, &size, &min, &max);
for (i=0; i<size;i++){
printf("f(%2d)=%3d\n", low+i, nums[i]);
}
printf("--\n");
printf("min=%3d\n", min);
printf("max=%3d\n", max);
printf("--\n");
t = -2;
printf("t=%2d%*s%c\n", t, nums[t-low]-min, "", '*');
free(nums);
return 0;
}
int parabola1(int t){
return t*(t-4)+5;
}
solved printing * in respective number using printf in C