我正在努力完成打印" *"在各自的数量。另外,如果你看到我需要显示最小和最大数量的图片,我该怎么做?我正在考虑使用存储这些数字的数组来做这件事,但是当我创建数组并试图传递它们时,我得到了一个错误。
这里是图片:
#include <stdio.h>
#include <limits.h>
#include <math.h>
int f(int);
int main(void){
int i, t, funval,tempL,tempH;
int a;
// Make sure to change low and high when testing your program
int low=-3, high=11;
for (t=low; t<=high;t++){
printf("f(%2d)=%3d\n",t,f(t));
}
printf("\n");
printf(" ");
for (i=1; i<=31; i+=5)
printf("%3d ", i);
printf("\n");
printf(" ");
for (i=1; i<=31; i+=5)
printf(" | ");
printf("\n");
for (t=low; t<=high;t++){
printf("t=%2d\n",t);
}
printf("\n");
for(i=0;i<=sizeof(nums)/sizeof(int);i++){
if (nums[i] > max)
{
max = nums[i];
}
if (nums[i] < min)
{
min = nums[i];
}
}
printf("Min: %d\n", min);
printf("Max: %d\n", max);
printf("\n");
printf(" ");
for (i=min; i<=max; i+=5)
printf("%3d ", i);
printf("\n");
printf(" ");
for (i=min; i<=max; i+=5)
printf(" | ");
printf("\n");
for (t=low; t<=high;t++){
printf("t=%2d\n",t);
}
// Your code here...
return 0;
}
int f(int t){
// example 1
return (t*t-4*t+5);
// example 2
// return (-t*t+4*t-1);
// example 3
// return (sin(t)*10);
// example 4
// if (t>0)
// return t*2;
// else
// return t*8;
}
答案 0 :(得分:0)
#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;
}