鱼C论坛

 找回密码
 立即注册
查看: 2283|回复: 2

平均值

[复制链接]
发表于 2012-1-28 05:29:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
/* Exercise 7.1 Calculating a floating-point average using pointers */
/*********************************************************************
* In this solution I 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. I 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                       */
  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)              /* If memory was not allocated */
      {                      /* Output a message  and end   */
        printf("Memory allocation failed. Terminating program.");
        exit(1);
      }
      if(!values)            /* Are there any values?                  */
        values = temp;       /* No - so just copy address of new memory */
      else                   /* Yes - so copy data from old to new      */
      {
        for(int 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(int 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 */
  return 0;
}




                               
登录/注册后可看大图
该贴已经同步到 空手套小白狼的微博
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-1-28 19:47:04 | 显示全部楼层
学习了@!~~~
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-1-28 19:53:04 | 显示全部楼层
:funk:有必要吗,都是英文
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-11 00:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表