?? no.9.c
字號:
#include "stdio.h"
#include "conio.h"
bool curve(float* x, float* y, int n, float* pa, float* pb)
{
float sx = 0, sy = 0, sxy = 0, sx2 = 0;
int i;
if ( n < 1 || pa == NULL || pb == NULL ) return false;
for (i = 0; i < n; i++) {
sx = sx + x[i];
sy = sy + y[i];
sxy = sxy + x[i] * y[i];
sx2 = sx2 + x[i] * x[i];
}
*pa = ( n * sxy - sx * sy ) / ( n * sx2 - sx * sx );
*pb = ( sy * sx2 - sx * sxy ) / ( n * sx2 - sx * sx );
return true;
}
void print(float a, float b)
{
printf("y = (%.2f)x + (%.2f) \n", a, b);
}
int main()
{
float a, b;
float x1[5] = {1, 2, 3, 4, 5};
float y1[5] = {0, 2, 2, 5, 4};
float x2[10] = {1.04, 1.80, 1.90, 1.77, 1.47, 1.34, 1.50, 1.91, 2.04, 1.21};
float y2[10] = {100, 200, 210, 185, 155, 135, 170, 205, 235, 125};
if ( curve(x1, y1, 5, &a, &b) ) print(a, b);
if ( curve(x2, y2, 10, &a, &b) ) print(a, b);
getch();
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -