728x90
반응형

 

#include <stdio.h>

int main(void)
{
	int score[10] = { 100, 90, 80, 82, 67, 88, 90, 95, 72, 71 };
	int time[10] = { 5, 4, 3, 3, 2, 2, 2, 3, 4, 6 };

	int size = sizeof(score) / sizeof(int);
	int scoreSum = 0;
	double scoreAvg;

	// score avg
	for (int i = 0; i < size; i++)
	{
		scoreSum += score[i];
	}
	scoreAvg = scoreSum / size;

	printf("Score sum = %d\n", scoreSum);			// 835
	printf("Score average = %.2lf\n", scoreAvg);	// 83.00
	printf("\n");

	// time avg

	int timeSum = 0;
	double timeAvg;

	for (int i = 0; i < size; i++)
	{
		timeSum += time[i];
	}
	timeAvg = timeSum / size;

	printf("Time sum = %d\n", timeSum);
	printf("Time average = %.2lf\n", timeAvg);

	double mse = 0;

	for (int i = 0; i < size; i++)
	{
		mse += (score[i] - scoreAvg) * (score[i] - scoreAvg) / size;
	}

	printf("MSE = %.2lf\n", mse);	// 108.70

	return 0;
}

100: 289
90: 49
80: 9
82: 1
67: 256
88: 25
90: 49
95: 144
72: 121
71: 144

1,087 sum


 

 

 

 

#include <stdio.h>

int main(void)
{
	int time[4] = {2, 4, 6, 8};
	int score[4] = {81, 93, 91, 97};

	double timeAvg;
	int timeSum = 0;
	double scoreAvg;
	int scoreSum = 0;

	int i;

	// Time Average
	for (i = 0; i < 4; i++)
	{
		timeSum += time[i];
		timeAvg = timeSum / 4;
	}

	printf("공부시간 (x) 평균 : %.2lf\n", timeAvg);

	// Score Average
	for (i = 0; i < 4; i++)
	{
		scoreSum += score[i];
		scoreAvg = scoreSum / 4;
	}

	printf("성적 (y) 평균 : %.2lf\n", scoreAvg);

	double incline;

	double aa = 0;
	double mse = 0;

	for (i = 0; i < 4; i++)
	{
		aa += (time[i] - timeAvg) * (score[i] - scoreAvg);
		mse += (time[i] - timeAvg) * (time[i] - timeAvg);
	}

	incline = aa / mse;

	printf("기울기 : %.2lf\n", incline);

	// y 절편의 값 b = y 의 평균

	double intercept;

	intercept =  scoreAvg - (timeAvg * incline);

	printf("y의 절편의 값 = %2.lf\n", intercept);

	// 예측값

	int y;
	int j;

	for (i = 0; i < 4; i++)
	{
		for (j = 0; j < 10;)
		{
			y = incline * j + intercept;
			j += 2;
		}
		printf("예측 값 : %lf\n", y);
	}

	return 0;
}
728x90
반응형

+ Recent posts