홍정모의 따라하며 배우는 C언어
나만은 연습문제 안습풀이
굉장히 더러운 코드이지만... 공유해봐요.
#include <stdio.h>
#define MONTHS 12
#define YEARS 3
void years_index(void)
{
int i;
printf("Year Index\t:");
i = 1;
while(i < 13)
{
printf("\t%d", i);
i++;
}
printf("\n");
}
void tem_data(int i, double *ptr)
{
int j;
j = 0;
printf("Yaer\t%d\t:", i);
while (j < MONTHS)
{
printf("\t%.1f", ptr[j]);
j++;
}
printf("\n");
}
void year_avg(int i, double *ptr)
{
int j;
double sum;
j = -1;
sum = 0;
printf("Year\t%d\t:\taverage temperature = ", i);
while (++j < MONTHS)
sum += ptr[j];
sum /= MONTHS;
printf("%.1f\n", sum);
}
void month_avg(double *a, double *b, double *c)
{
int i;
int j;
double avg_tem;
double *ptr[3];
i = -1;
ptr[0] = a;
ptr[1] = b;
ptr[2] = c;
printf("Avg temp\t:");
while (++i < MONTHS)
{
avg_tem = 0.0;
j = 0;
while (j < YEARS)
{
avg_tem += ptr[j++][i];
}
avg_tem /= (double)YEARS;
printf("\t%.1f", avg_tem);
}
printf("\n");
}
int main(void)
{
double year2016[MONTHS] = { -3.2, 0.2, 7.0, 14.1, 19.6, 23.6, 26.2, 28.0, 23.1, 16.1, 6.8, 1.2};
double year2017[MONTHS] = { -1.8, -0.2, 6.3, 13.9, 19.5, 23.3, 26.9, 25.9, 22.1, 16.4, 5.6, -1.9};
double year2018[MONTHS] = { -4.0, -1.6, 8.1, 13.0, 18.2, 23.1, 27.8, 28.8, 21.5, 13.1, 7.8, -0.6};
printf("[Temperature Data]\n");
years_index();
tem_data(0, year2016);
tem_data(1, year2017);
tem_data(2, year2018);
printf("\n");
printf("[Yearly average temperatures of 3 years]\n");
year_avg(0, year2016);
year_avg(1, year2017);
year_avg(2, year2018);
printf("\n");
printf("[Monthly average temperatures for 3 years]\n");
years_index();
month_avg(year2016, year2017, year2018);
return (0);
}