[C]非常郁闷的一个问题
- From: "Thomas X. Iverson" <txi@xxxxxxxxxxxxx>
- To: ghostunix@xxxxxxxxxxxxx
- Date: Thu, 29 May 2008 12:39:41 +0800
在做一道指针的习题,要求输入任意多个浮点数进行平均数的计算,动态分配内存
书上的代码如下:
/* Exercise 7.1 Calculating a floating-point average using pointers */
/*********************************************************************
* In this solution we allocate a some memory and when it is full *
* allocate a new, larger amount of memory and copy the contents of *
* the old memory to the new. We then free the old memory. This *
* process repeats as often as necessary. *
*********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(void)
{
double *values = NULL; /* Pointer to memory holding data values */
double *temp = NULL; /* Pointer to newly allocated memory */
double sum = 0.0; /* Sum of values */
int capacity = 0; /* Maximum number of values that can be stored */
int increment = 5; /* Capacity increment for dynamic allocation */
int count = 0; /* Number of values read */
int i = 0; /* Index to array */
char answer = 'n';
do
{
if(count == capacity) /* Check if there is spare memory */
{
capacity += increment; /* Increase the capacity of memory by increment */
temp = (double*)malloc((capacity)*sizeof(double)); /* and allocate it */
if(temp == NULL) /* If memory was not allocated */
{ /* Output a message and end */
printf("Memory allocation failed. Terminating program.");
exit(1);
}
if(values == NULL) /* Are there any values? */
values = temp; /* No - so just copy address of new memory */
else /* Yes - so copy data from old to new */
{
for(i = 0 ; i<count ; i++)
*(temp+i) = *(values+i);
free(values); /* Free the old memory */
values = temp; /* Copy address of new */
}
temp = NULL; /* Reset pointer */
}
printf("Enter a value: ");
scanf("%lf", values+count++);
printf("Do you want to enter another(y or n)? ");
scanf(" %c", &answer);
}while(tolower(answer) == 'y');
/* Now sum the values */
for(i = 0 ; i<count ; i++)
sum += *(values+i);
/* Output the average */
printf("\n The average of the the values you entered is %.2lf.\n", sum/count);
free(values); /* We are done - so free the memory */
}
在理解了这个算法后,我自己重新实现了一遍(算是默写性质的复习):
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int main(void)
{
double *temp=NULL;
double *value=NULL;
double sum=0.0;
int i;
int count=0;
int capa=0;
int incre=5;
char answer='n';
do
{
if(count==capa)
{
capa+=incre;
temp=(double *)malloc((capa)*sizeof(double));
if(temp==NULL)
{
printf("Failed\n");
return 1;
}
if(value==NULL)
value=temp;
else
{
for(i=0;i<count;i++)
{
*(temp+i)=*(value+i);
}
free(value);
value=temp;
}
temp=NULL;
}
printf("Enter a value:");
scanf("%lf",value+count++);
printf("Enter another?:");
scanf(" %c",&answer);
}while(tolower(answer)=='y');
for(i=0;i<count;i++);
{
sum+=*(value+i);
}
printf("%.2lf",sum/count);
free(value);
}
问题出现了,这两段代码我怎么看都没有觉得有问题,可是就是有两个问题:
1.我自己写的这个代码无法很好的处理scanf输入缓冲问题
2.我自己写的这个代码计算出的平均值总是0,我后来加了个printf查看sum和count的值,sum总是为0,count没有问题,不知道为什么
--
Keep It Simple Stupid
http://blog.ghostunix.org
ghosTM55
- Follow-Ups:
- Re: [C]非常郁闷的一个问题
- From: 李磊
- Re: [C]非常郁闷的一个问题
- From: 李磊
Other related posts:
- » [ghosTunix.org] 欢迎大家加入
- » [ghosTunix.org] 再试验
- » [ghosTunix.org] 给大家一个建议
- » [ghosTunix.org] 大家晚安
- » [ghosTunix.org] 刚才改了一下模式
- » 建议在邮件标题加上邮件列表名
- » Re: 建议在邮件标题加上邮件列表名
- » Re: 建议在邮件标题加上邮件列表名
- » Re: 建议在邮件标题加上邮件列表名
- » Re: 建议在邮件标题加上邮件列表名
- » Re: 建议在邮件标题加上邮件列表名
- » Re: 建议在邮件标题加上邮件列表名
- » Re: 建议在邮件标题加上邮件列表名
- » Re: 建议在邮件标题加上邮件列表名
- » Re: 建议在邮件标题加上邮件列表名
- » [ghosTunix.org] 现在应该有前缀了
- » [ghosTunix.org] Re: 建议在邮件标题加上邮件列表名
- » [ghosTunix.org] Re: 建议在邮件标题加上邮件列表名
- » [ghosTunix.org] 文件系统与红颜祸水...
- » [ghosTunix.org] 邮件列表的摘要信息太难看了
- » [ghosTunix.org] 我已被这个邮件列表的摘要信息搞晕了
- » [ghosTunix.org] 我不想出版这本书了
- » [ghosTunix.org] Re: 我不想出版这本书了
- » [ghosTunix.org] Re: 我不想出版这本书了
- » [ghosTunix.org] 反正弦函数怎么实现?
- » [ghosTunix.org] 我不行了,摘要全是乱码,改模式了,邮件多就多吧
- » [T] 通告:修改了主题标签
- » [T] 基本的几个条目已经弄好了
- » [概念]计算机最大能支持的硬盘空间与哪些因素有关?
- » Re: [概念]计算机最大能支持的硬盘空间与哪些因素有关?
- » Re: [概念]计算机最大能支持的硬盘空间与哪些因素有关?
- » Re: [概念]计算机最大能支持的硬盘空间与哪些因素有关?
- » Re: [概念]计算机最大能支持的硬盘空间与哪些因素有关?
- » Re: [概念]计算机最大能支持的硬盘空间与哪些因素有关?
- » Re: [概念]计算机最大能支持的硬盘空间与哪些因素有关?
- » Re: [概念]计算机最大能支持的硬盘空间与哪些因素有关?
- » Re: [概念]计算机最大能支持的硬盘空间与哪些因素有关?
- » Re: [概念]计算机最大能支持的硬盘空间与哪些因素有关?
- » Re: [概念]计算机最大能支持的硬盘空间与哪些因素有关?
- » Re: [概念]计算机最大能支持的硬盘空间与哪些因素有关?
- » Re: [概念]计算机最大能支持的硬盘空间与哪些因素有关?
- » Re: [概念]计算机最大能支持的硬盘空间与哪些因素有关?
- » Re: [概念]计算机最大能支持的硬盘空间与哪些因素有关?
- » Re: [概念]计算机最大能支持的硬盘空间与哪些因素有关?
- » Re: [概念]计算机最大能支持的硬盘空间与哪些因素有关?
- » Re: [概念]计算机最大能支持的硬盘空间与哪些因素有关?
- » Re: [概念]计算机最大能支持的硬盘空间与哪些因素有关?
- » Re: [概念]计算机最大能支持的硬盘空间与哪些因素有关?
- » Re: [概念]计算机最大能支持的硬盘空间与哪些因素有关?
- » Re: [概念]计算机最大能支持的硬盘空间与哪些因素有关?
- » Re: [概念]计算机最大能支持的硬盘空间与哪些因素有关?
- » Re: [概念]计算机最大能支持的硬盘空间与哪些因素有关?
- » Re: [概念]计算机最大能支持的硬盘空间与哪些因素有关?
- » Re: [概念]计算机最大能支持的硬盘空间与哪些因素有关?
- » Re: [概念]计算机最大能支持的硬盘空间与哪些因素有关?
- » Re: [概念]计算机最大能支持的硬盘空间与哪些因素有关?
- » 到新学校了
- » Re: 到新学校了
- » Re: 到新学校了
- » Re: 到新学校了
- » [离散数学]双蕴含与合取的区别
- » Re: [离散数学]双蕴含与合取的区别
- » Re: [离散数学]双蕴含与合取的区别
- » Re: [离散数学]双蕴含与合取的区别
- » [C]逻辑移位与算术移位的问题
- » [no subject]
- » 大家能说一下自己的职业么?
- » Re: 大家能说一下自己的职业么?
- » Re: 大家能说一下自己的职业么?
- » Re: 大家能说一下自己的职业么?
- » Re: 大家能说一下自己的职业么?
- » Re: 大家能说一下自己的职业么?
- » Re: 大家能说一下自己的职业么?
- » Re: 大家能说一下自己的职业么?
- » Re: 大家能说一下自己的职业么?
- » Re: 大家能说一下自己的职业么?
- » Re: 大家能说一下自己的职业么?
- » Re: 大家能说一下自己的职业么?
- » Re: 大家能说一下自己的职业么?
- » [C]关于指针的问题
- » 一个笑话。。。
- » Re: 大家能说一下自己的职业么?
- » Re: 大家能说一下自己的职业么?
- » Re: 大家能说一下自己的职业么?
- » Re: 大家能说一下自己的职业么?
- » Re: 大家能说一下自己的职业么?
- » Re: 大家能说一下自己的职业么?
- » Re: 大家能说一下自己的职业么?
- » Re: 大家能说一下自己的职业么?
- » Re: 大家能说一下自己的职业么?
- » 试点
- » Re: 试点
- » Re: 试点
- » 再试点
- » [C]非常郁闷的一个问题
- » 美国对民主的定义-----开源界的最佳描述
- » Re: 美国对民主的定义-----开源界的最佳描述
- » Re: 美国对民主的定义-----开源界的最佳描述
- » Re: 美国对民主的定义-----开源界的最佳描述
- » Re: 美国对民主的定义-----开源界的最佳描述
- » Re: 美国对民主的定义-----开源界的最佳描述
- » 奇怪
- » Re: 奇怪
- » Re: 奇怪
- » Re: 奇怪
- » Re:奇怪
- » 中国人可以开发属于自己的脚本语言
- » Re: 中国人可以开发属于自己的脚本语言
- » Re: 中国人可以开发属于自己的脚本语言
- » Re: 中国人可以开发属于自己的脚本语言
- » 回复: 中国人可以开发属于自己的脚本语言
- » Re: 回复: 中国人可以开发属于自己的脚本语言
- » 很长时间没有消息了 - Lyper Lai
- » Re: 很长时间没有消息了 - Ben Ben
- » Re: 很长时间没有消息了 - 李磊
- » Re: 很长时间没有消息了 - Thomas X. Iverson
- » Re: 很长时间没有消息了 - Ken . xu . SourceForge
- » Re: 很长时间没有消息了 - hodrag
- » Re: 很长时间没有消息了 - Thomas X. Iverson
- » Re: 很长时间没有消息了 - Lyper Lai
- » Re: 很长时间没有消息了 - Thomas X. Iverson
- » Re: 很长时间没有消息了 - Ken . xu . SourceForge
- » RE: 很长时间没有消息了 - bruce
- » Re: 很长时间没有消息了 - Ken . xu . SourceForge
- » RE: 很长时间没有消息了 - bruce
- » Re: 很长时间没有消息了 - Thomas X. Iverson
- » Re: 很长时间没有消息了 - vvoody
- » Re: 很长时间没有消息了 - Lyper Lai
- » Re: 很长时间没有消息了 - Thomas X. Iverson
- » Re: 很长时间没有消息了 - Thomas X. Iverson
- » Re: 很长时间没有消息了 - Ken . xu . SourceForge
- » Re: 很长时间没有消息了 - tan dake
- » Re: 很长时间没有消息了 - Thomas X. Iverson
- » Re: 很长时间没有消息了 - Lyper Lai
- » Re: 很长时间没有消息了 - David Cheng
- » Re: 很长时间没有消息了 - tan dake
- » Re: 很长时间没有消息了 - ai1010
- » Re: 很长时间没有消息了 - David Cheng
- » Re: 很长时间没有消息了 - Ken . xu . SourceForge
- » Re: 很长时间没有消息了 - 俞奕
- » Re: 很长时间没有消息了 - Ken . xu . SourceForge
- » Re: 很长时间没有消息了 - 俞奕
- » Re: 很长时间没有消息了 - Thomas X. Iverson
- » Re: 很长时间没有消息了 - ai1010
- » Re: 很长时间没有消息了 - Ben Ben
- » 新的一年,我们的邮件列表搬家了 - Thomas X. Iverson
- » Re: 新的一年,我们的邮件列表搬家了 - vvoody
- » Re: 新的一年,我们的邮件列表搬家了 - imagelife
- » Re: 新的一年,我们的邮件列表搬家了 - Thomas X. Iverson
- » Re: 新的一年,我们的邮件列表搬家了 - vvoody
- » Re: 新的一年,我们的邮件列表搬家了 - nihui
- » Re: 新的一年,我们的邮件列表搬家了 - Thomas X. Iverson
- » Re: 新的一年,我们的邮件列表搬家了 - nihui
- » Re: 新的一年,我们的邮件列表搬家了 - Thomas X. Iverson
- » Re: 新的一年,我们的邮件列表搬家了 - jim qiu
- » Re: 新的一年,我们的邮件列表搬家了 - LI Daobing (李道兵)
- » Re: 新的一年,我们的邮件列表搬家了 - Thomas X. Iverson
- » Re: 新的一年,我们的邮件列表搬家了 - vvoody
- » Re: 新的一年,我们的邮件列表搬家了 - iNutshell
- » Re: 新的一年,我们的邮件列表搬家了 - iNutshell
- » Re: 新的一年,我们的邮件列表搬家了 - Thomas X. Iverson
- » Re: 新的一年,我们的邮件列表搬家了 - Lyper Lai
- » ???核的?? - hodrag
- Re: [C]非常郁闷的一个问题
- From: 李磊
- Re: [C]非常郁闷的一个问题
- From: 李磊