double Lerp(double x0,double y0,double x1,double y1,double x)
{
double dy = y1 - y0
if(dy == 0){
printf("除0错误!\n")
return 0
}
return x * (x1 - x0) / dy
}
int main()
{
double x0,x1,y1,y0,x,y
printf("Inptu x0 y0 x1 y1 x:")
scanf("%lf %lf %lf %lf %lf",&x0,&y0,&x1,&y1,&x)
y = Lerp(x0,y0,x1,y1,x)
printf("y = %lf\n",y)
return 0
}
一元线性回归的C语言程序是:利用最小二乘法来估计线性回归方程的参数,然后用这些参数来预测因变量的值1。例如,你可以参考下面的代码:#include <stdio.h>#include <math.h>//定义一个函数,计算一元线性回归方程的参数a和bvoid linear_regression(double x[], double y[], int n, double *a, double *b){//定义变量
double sum_x = 0//x的和
double sum_y = 0//y的和
double sum_xy = 0//xy的和
double sum_x2 = 0//x平方的和
//遍历数组,计算各项和
for (int i = 0i <ni++)
{
sum_x += x[i]
sum_y += y[i]
sum_xy += x[i] * y[i]
sum_x2 += x[i] * x[i]
}//根据最小二乘法公式,计算a和b
*a = (n * sum_xy - sum_x * sum_y) / (n * sum_x2 - pow(sum_x, 2))
*b = (sum_y - (*a) * sum_x) / n
}//主函数int main(){//定义一个自变量数组x,存放观测值
double x[] = {1.0, 2.0, 3.0, 4.0} //定义一个因变量数组y,存放观测值
double y[] = {3.1, 4.9, 7.2, 8.9} //定义数组长度n
int n = sizeof(x) / sizeof(x[0]) //定义两个指针变量a和b,用来存放线性回归方程的参数
double a double b
#include<stdio.h>#include<iostream.h>
#include<stdlib.h>
#defineOVERFLOW -2
#define OK 1
#define ERROR 0
#defineLIST_INIT_SIZE 100
#defineLISTINCREMENT 10
typedef intElemType
typedef intStatus
//定义顺序存储结构
typedef struct
{
ElemType *elem
int length
int listsize
}SqList
//初始化顺序表
StatusInitList_Sq(SqList &L)
{
L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType))
if(!L.elem ) exit(ERROR)
L.length =0
L.listsize =LIST_INIT_SIZE
return OK
}
//自定义创建顺序表
voidCreate_SqList(SqList &L)
{
int c,i=0
int *newBase
printf("请输入顺序表元素:\n")
while((scanf("%d",&c))!=EOF)
{
if(i>=L.listsize) //自定义顺序表大小超过初始化大小
{
newBase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType))
//为初始顺序表以LISTINCREMENT大小重新增加存储空间
if(!newBase)exit(OVERFLOW)
L.elem=newBase
L.listsize+=LISTINCREMENT
}
L.elem[i++]=c
}
L.length=i
printf("输入的顺序表元素:\n")
for(i=0i<L.lengthi++)
printf("%d ",L.elem[i])
printf("\n")
}
//在指定位置插入元素
StatusListInsert(SqList &L,int i,ElemType e)
{
ElemType *p,*q,*newbase
if(i<1||i>L.length+1)
{
printf("插入位置错误\n")
return(ERROR)
}
if(L.length>=L.listsize)
{
newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType))
if(!newbase) exit(OVERFLOW)
L.elem=newbase
L.listsize+=LISTINCREMENT
}
if(i==L.length) L.elem[i+1]=e
q=&(L.elem[i-1])
for(p=&(L.elem[L.length-1])p>=q--p)*(p+1)=*p
*q=e
++L.length
return OK
}
//在指定位置删除元素
StatusListDelete_Sq(SqList &L,int i,ElemType *e)
{
ElemType *p,*q
if(i<1||i>L.length+1)
return ERROR
p=&(L.elem[i-1])
*e=*p
q=L.elem+L.length-1
for(++pp<=q++p)
*(p-1)=*p
--L.length
return OK
}
void main()
{
SqList L
int m,n
int location,element
if(!InitList_Sq(L))
{
printf("初始化顺序表失败!\n")
exit(ERROR)
}
Create_SqList(L)
for(m=0m<3m++)
{
printf("输入插入位置:")
scanf("%d",&location)
while(location>L.length+1||location<1)
{
printf("输入位置错误,请重新输入!\n")
scanf("%d",&location)
}
printf("插入元素:")
scanf("%d",&element)
if(!ListInsert(L,location,element))
{
printf("顺序表插入失败!\n")
exit(ERROR)
}
printf("插入顺序表为:\n")
for(int i=0i<=L.length -1i++)
{
printf("%d ",L.elem[i])
}
printf("\n新顺序表一共有%d个元素。\n",L.length)
}
for(n=0n<3n++)
{
printf("输入删除位置:")
scanf("%d",&location)
while(location>L.length||location<1)
{
printf("输入位置错误,请重新输入!\n")
scanf("%d",&location)
}
if(!ListDelete_Sq(L,location,&element))
{
printf("删除错误!\n")
exit(ERROR)
}
printf("被删除的元素为:%d \n",element)
printf("被删除后的顺序表为:\n")
for(int j=0j<=L.length-1j++)
{
printf("%d ",L.elem[j])
}
printf("\n新顺序表一共有%d个元素。\n",L.length)
}
}
这个是我最近编写的 顺序表也是线性表的
这里还有链表的程序 用的话再传给你
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)