⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex7_04.c

📁 [C语言入门经典(第4版)]整本书的源码!值得推荐!全部是最简单的源码!
💻 C
字号:
/* Exercise 7.4 Read and process daily temperatures */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define DAYS             2        /* Initial days allowed for */
#define READINGS_PER_DAY 6        /* Initial days allowed for */

int main(void)
{
   double **data = NULL;     /* Pointer to array of pointers to arrays of temperatures */
   double **newdata = NULL;  /* Pointer to array of pointers to arrays of temperatures */
   double *averages = NULL;  /* Pointer to array of averages */

   int day = 0;
   int max_days = 0;
   char answer = 'n';

   do
   {
     if(day == max_days)
     {
       max_days += DAYS;
       /* Create new array of pointers */
       newdata = (double**)malloc(max_days*sizeof(double*));
       /* Create an array of values of type double for each new day and store the address */
       for(int i = day ; i<max_days ; i++)
         *(newdata+i) = (double*)malloc(READINGS_PER_DAY*sizeof(double));
       if(data != NULL)
       {
         /* Copy the addresses of the existing arrays of temperatures */
         for(int i = 0 ; i<day ; i++)
           *(newdata +i) = *(data+i);
         free(data);          /* Free memory for the old array of pointers */
       }
       data = newdata;        /* copy the address of the new array of pointers */
       newdata = NULL;        /* Reset the pointer                             */
     }

     printf("Enter the %d readings for a day:\n", READINGS_PER_DAY);
     for(int i = 0 ; i<READINGS_PER_DAY ; i++)
       scanf(" %lf", *(data+day)+i);
     ++day;
     printf("Do you want to enter another set(y or n)? ");
     scanf(" %c", &answer);
   } while(tolower(answer) != 'n');

   /* If there is unused memory for temperature values - release it */
   for(int i = day ; i<max_days ; i++)
     free(*(data+i));

   /* Allocate space for the averages */
   averages = (double*)malloc(day*sizeof(double));

   /*Calculate the averages */
   for(int i = 0 ; i<day ; i++)
   {
     *(averages+i) = 0.0;
     for(int j = 0 ; j<READINGS_PER_DAY ; j++)
       *(averages+i) += *(*(data+i)+j);
     *(averages+i) /= READINGS_PER_DAY;
   }
   printf("\nThe daily temperatures and averages are:\n");
   for(int i = 0 ; i<day ; i++)
   {
     for(int j = 0 ; j<READINGS_PER_DAY ; j++)
       printf("%10.1lf", *(*(data+i)+j));
     printf("%10.1lf\n", *(averages+i));
   }
   /* Finally - release all the memory */
   free(averages);
   for(int i = 0 ; i<day ; i++)
     free(*(data+i));
   free(data);
   return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -