首页 > 代码库 > 7.1练习,指针部分用法,算个平均数

7.1练习,指针部分用法,算个平均数

#define _STDC_WANT_LIB_EXT1_ 1
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define CAP_INCR 5

int main(void)
{
double *values =NULL;
int capacity = 0;
double *temp = NULL;
double sum = 0.0;
int count = 0;
char answer = n;

do
{
if(count == capacity)
{
capacity += CAP_INCR;
temp = realloc(values,capacity*sizeof(double));
if(!temp)
{
printf("bullshit\n");
exit(1);
}
values = temp;
temp = NULL;
}

printf("Enter number:\n");
if(EOF==scanf_s("%lf",values+count++))
{
break;
}

printf("Do you want to enter another(y or n)? ");
scanf(" %c", &answer);

}while(tolower(answer)==y);

for(int i = 0;i<count;i++)
{
sum+=*(values+i);
}

printf("the result is %lf\n",sum/count);
return 0;

}

 

7.1练习,指针部分用法,算个平均数