SlideShare a Scribd company logo
/* Write C program to implement the linear regression algorithm. */



#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<string.h>



float mean(float *a, int n);

void deviation(float *a, float mean, int n, float *d, float *S);



void main()

{

    float a[20],b[20],dx[20],dy[20];

    float sy=0,sx=0,mean_x=0,mean_y=0,sum_xy=0;

    float corr_coff=0,reg_coff_xy=0, reg_coff_yx=0;

    char type_coff[7];

    int n=0,i=0;



    clrscr();



    printf("Enter the value of n: ");

    scanf("%d",&n);

    printf("Enter the values of x and y:n");

    for(i=0;i<n;i++)
scanf("%f%f",&a[i],&b[i]);

mean_x=mean(a,n);

mean_y=mean(b,n);

deviation(a,mean_x,n,dx,&sx);

deviation(b,mean_y,n,dy,&sy);



for(i=0;i<n;i++)

    sum_xy=sum_xy+dx[i]*dy[i];

corr_coff=sum_xy/(n*sx*sy);

printf("Enter the type of regression coefficient as 'x on y' or 'y on x': ");

fflush(stdin);

gets(type_coff);



if(strcmp(type_coff,"x on y")==1)

{

        reg_coff_xy=corr_coff*(sx/sy);

        printf("nThe value of linear regression coefficient is %f",reg_coff_xy);

}

else if(strcmp(type_coff,"y on x")==1)

{

        reg_coff_yx=corr_coff*(sy/sx);

        printf("nThe value of linear regression coefficient is %f",reg_coff_yx);

}

else
printf("nEnter the correct type of regression coefficient.");

    getch();

}




float mean(float *a, int n)

{

    float sum=0, i=0;

    for(i=0;i<n;i++)

        sum=sum+a[i];

    sum=sum/n;

    return (sum);

}



void deviation(float *a, float mean, int n, float *d, float *s)

{

    float sum=0,t=0;

    int i=0;

    for(i=0;i<n;i++)

    {

        d[i]=a[i]-mean;

        t=d[i]*d[i];

        sum=sum+t;

    }
sum=sum/n;

    *s=sqrt(sum);

}

More Related Content

DOCX
Maple Code for Steepest Descent
DOCX
programa suma y multiplica
PDF
Creative Coding 1 - 2 Variables
DOC
Palindrome number program c
PDF
Bcsl 033 data and file structures lab s1-1
Maple Code for Steepest Descent
programa suma y multiplica
Creative Coding 1 - 2 Variables
Palindrome number program c
Bcsl 033 data and file structures lab s1-1

What's hot (20)

PPTX
Computer programing w
DOCX
Metnum
DOCX
One dimensional operation of Array in C- language
PDF
Program to reflecta triangle
PPTX
Wcbpijwbpij new
DOCX
Project filter matlab
PDF
Odd number
PDF
Switch
PDF
Bcsl 033 data and file structures lab s5-3
DOCX
Euler method in c
PDF
Program in ‘C’ language to implement linear search using pointers
DOC
Add digits of number in c
PDF
Implement a queue using two stacks.
PDF
Bcsl 033 data and file structures lab s5-2
PDF
Bcsl 033 data and file structures lab s1-4
PPTX
C Programming Example
Computer programing w
Metnum
One dimensional operation of Array in C- language
Program to reflecta triangle
Wcbpijwbpij new
Project filter matlab
Odd number
Switch
Bcsl 033 data and file structures lab s5-3
Euler method in c
Program in ‘C’ language to implement linear search using pointers
Add digits of number in c
Implement a queue using two stacks.
Bcsl 033 data and file structures lab s5-2
Bcsl 033 data and file structures lab s1-4
C Programming Example
Ad

Viewers also liked (11)

DOC
QT085.Doc
PPT
Javascript Debugging
PPS
Imagen
PPTX
Intro to Web Sockets
DOC
Energy risk_nov2010
PPT
BrainFingerprintingpresentation
PPT
DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
QT085.Doc
Javascript Debugging
Imagen
Intro to Web Sockets
Energy risk_nov2010
BrainFingerprintingpresentation
DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
Ad

Similar to week-23x (20)

PPTX
Regression
PDF
PPT
15 regression basics
PDF
C programs
DOCX
#include PA3Header.h#include stdafx.h Function r.docx
PPTX
Presentation on Regression Analysis
PPTX
ML_Regression.pptx
PPTX
Regression
PPTX
Different Types of Machine Learning Algorithms
PDF
Please write it in C not python .pdf
PPTX
Regression Analysis.pptx
PPTX
Regression Analysis Techniques.pptx
PPTX
Unit 7b Regression Analyss.pptxbhjjjjjjk
PDF
Here is The code in C language .pdf
PPTX
โปรแกรมย่อยและฟังชันก์มาตรฐาน
PDF
Equation of linear regression straight line
PDF
Modify the code in C Please .pdf
PDF
C programs Set 2
PPTX
Program presentation
PDF
Bio-L8- Correlation and Regression Analysis.pdf
Regression
15 regression basics
C programs
#include PA3Header.h#include stdafx.h Function r.docx
Presentation on Regression Analysis
ML_Regression.pptx
Regression
Different Types of Machine Learning Algorithms
Please write it in C not python .pdf
Regression Analysis.pptx
Regression Analysis Techniques.pptx
Unit 7b Regression Analyss.pptxbhjjjjjjk
Here is The code in C language .pdf
โปรแกรมย่อยและฟังชันก์มาตรฐาน
Equation of linear regression straight line
Modify the code in C Please .pdf
C programs Set 2
Program presentation
Bio-L8- Correlation and Regression Analysis.pdf

Recently uploaded (20)

PDF
01-Introduction-to-Information-Management.pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Pharma ospi slides which help in ospi learning
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
RMMM.pdf make it easy to upload and study
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
GDM (1) (1).pptx small presentation for students
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
master seminar digital applications in india
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
01-Introduction-to-Information-Management.pdf
Chinmaya Tiranga quiz Grand Finale.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Microbial disease of the cardiovascular and lymphatic systems
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Pharma ospi slides which help in ospi learning
FourierSeries-QuestionsWithAnswers(Part-A).pdf
human mycosis Human fungal infections are called human mycosis..pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
RMMM.pdf make it easy to upload and study
O5-L3 Freight Transport Ops (International) V1.pdf
Cell Structure & Organelles in detailed.
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
GDM (1) (1).pptx small presentation for students
O7-L3 Supply Chain Operations - ICLT Program
master seminar digital applications in india
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
VCE English Exam - Section C Student Revision Booklet
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE

week-23x

  • 1. /* Write C program to implement the linear regression algorithm. */ #include<stdio.h> #include<conio.h> #include<math.h> #include<string.h> float mean(float *a, int n); void deviation(float *a, float mean, int n, float *d, float *S); void main() { float a[20],b[20],dx[20],dy[20]; float sy=0,sx=0,mean_x=0,mean_y=0,sum_xy=0; float corr_coff=0,reg_coff_xy=0, reg_coff_yx=0; char type_coff[7]; int n=0,i=0; clrscr(); printf("Enter the value of n: "); scanf("%d",&n); printf("Enter the values of x and y:n"); for(i=0;i<n;i++)
  • 2. scanf("%f%f",&a[i],&b[i]); mean_x=mean(a,n); mean_y=mean(b,n); deviation(a,mean_x,n,dx,&sx); deviation(b,mean_y,n,dy,&sy); for(i=0;i<n;i++) sum_xy=sum_xy+dx[i]*dy[i]; corr_coff=sum_xy/(n*sx*sy); printf("Enter the type of regression coefficient as 'x on y' or 'y on x': "); fflush(stdin); gets(type_coff); if(strcmp(type_coff,"x on y")==1) { reg_coff_xy=corr_coff*(sx/sy); printf("nThe value of linear regression coefficient is %f",reg_coff_xy); } else if(strcmp(type_coff,"y on x")==1) { reg_coff_yx=corr_coff*(sy/sx); printf("nThe value of linear regression coefficient is %f",reg_coff_yx); } else
  • 3. printf("nEnter the correct type of regression coefficient."); getch(); } float mean(float *a, int n) { float sum=0, i=0; for(i=0;i<n;i++) sum=sum+a[i]; sum=sum/n; return (sum); } void deviation(float *a, float mean, int n, float *d, float *s) { float sum=0,t=0; int i=0; for(i=0;i<n;i++) { d[i]=a[i]-mean; t=d[i]*d[i]; sum=sum+t; }
  • 4. sum=sum/n; *s=sqrt(sum); }