码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-12-16 11:50:09      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:平均数   return   result   main   type   sum   city   部分   capacity   

#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练习,指针部分用法,算个平均数

标签:平均数   return   result   main   type   sum   city   部分   capacity   

原文地址:http://www.cnblogs.com/fuxiaoqi/p/6186266.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!