0% found this document useful (0 votes)
50 views5 pages

Programming Fundamentals

The document is a C++ program that performs linear regression on data points to calculate the slope and y-intercept of the regression line. The program prompts the user to enter the number of data points and then the x and y coordinates for each point. It calculates the sum, mean, and products of the x and y values. The slope and y-intercept are calculated using the appropriate formulas. The user is then prompted to choose whether to interpolate new y values for given x values using the regression line equation.

Uploaded by

Haroon Afzal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views5 pages

Programming Fundamentals

The document is a C++ program that performs linear regression on data points to calculate the slope and y-intercept of the regression line. The program prompts the user to enter the number of data points and then the x and y coordinates for each point. It calculates the sum, mean, and products of the x and y values. The slope and y-intercept are calculated using the appropriate formulas. The user is then prompted to choose whether to interpolate new y values for given x values using the regression line equation.

Uploaded by

Haroon Afzal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Programming Fundamentals (COMP 111)

Assignment# 02 CLO# 02
Due Date: November 30, 2022 NAME:Haroon Afzal

Program:
#include<iostream>
using namespace std;
#include<iomanip>
int main ()
{ int n, Sqr_of_W;

double W, Z, b, Sn_W, Sn_Z, Mul_of_Sn_WZ;//Sn means sum of all----

double Avg_of_Sn_W, Avg_of_Sn_Z, Slope;

cout<<"How many data points do you have(1-12): ";

cin>>n;//Getting value from user to find the intercept

if(n==0)

goto exit;

cout<<"\nEnter "<<n<<" 'w' and 'z' coordinates for each point (seperate with space):
"<<endl;

for(int i=1;i<=n;i++)//loop for given equations upto the number of points that user give

{
cout<<setw(10)<<i<<": ";

cin>>W>>Z;

Sn_W+=W;//summing up all w values

Sn_Z+=Z;//summing up all z values

Sqr_of_W+=(W*W);//square of all w values

Mul_of_Sn_WZ+=(W*Z);//Mul of Wand Z values

Avg_of_Sn_W=Sn_W/n;//Average of all w values

Avg_of_Sn_Z=Sn_Z/n;//Average of all Z values

Slope=((n*Mul_of_Sn_WZ)-(Sn_W*Sn_Z))/((n*Sqr_of_W)-(Sn_W*Sn_Z));//finding slope
using formula

b=Avg_of_Sn_Z-(Slope*Avg_of_Sn_W);

cout<<"The equation of regression line is: Z="<<fixed<<setprecision(3)<<Slope<<"w +


"<<b<<endl;

True_case://

char ch;
cout<<endl;

cout<<"Do you want to interpolate? (z/n): ";

cin>>ch;

if(ch=='Z'|| ch=='z')

cout<<"Enter 'w' values,one per line.Terminate the program any time with(ctrl+d):
"<<endl;

while(cin>>W)

Y=(Slope*W)+b;

if(Z>0)

cout<<setw(10)<<"z= +"<<setprecision(2)<<Z<<endl<<endl;

else

cout<<setw(10)<<"Z= "<<setprecision(2)<<Z<<endl<<endl;

}
}

else if(ch=='N'||ch=='n')//else if statement to check the condition

cout<<"\tGood Bye";

else if((ch!='N'||'n')||(ch!='z'||'Z'))//else if statement to check the condition

cout<<"\n\tInvalid....Try again?\n";

goto True_case;//goto function is used when user enter invalid choice

exit:

return 0;

OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy