I had the following problem and got the following answer from an anonymous user,
ID: 3722300 • Letter: I
Question
I had the following problem and got the following answer from an anonymous user, could someone please help me with a code that would work on a fortran compiler
The program below gives the desired output.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
program.cpp
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include<iostream>
#include<conio.h>
#include<fstream>
#include<stdlib.h>
#include <sstream>
using namespace std;
int main() {
const int max = 100;
float x[max]= {1,2,3,4,5,6,7,8,9,10};
float y[max]= {1,2,3,4,5,6,7,8,9,10};
float tempx[max] = {};
float tempy[max] = {};
int n,xLine = 10,yLine = 10;
float xavg=0,yavg=0,m=0,temp = 0,b= 0;
char fname[10];
system("cls");
cout<<"The values of a x and y are : "<<endl;
cout<<"Data point"<<" "<<"x value"<<" "<<"y value"<<endl;
for(int i =0;i<xLine;i++){
cout<<i+1<<" "<<x[i]<<" "<<y[i]<<endl;
}
cout<<"Choose a point to delete or 0 to skip this data : ";
cin>>n;
while(n!=0){
if(n>=0 && n<=xLine){
cout<<"Data point"<<" "<<"x value"<<" "<<"y value"<<endl;
int ctr = 1;
for(int i =0;i<xLine;i++){
if((i-1)==n){
continue;
}
else{
tempx[i] = x[i];
tempy[i] = y[i];
cout<<ctr++<<" "<<x[i]<<" "<<y[i]<<endl;
}
}
xLine = xLine-1;
for(int i=0;i<xLine;i++){
x[i] = tempx[i];
y[i] = tempy[i];
}
cout<<"Choose a point to delete or 0 to skip this data : ";
cin>>n;
}
}
//calculate x avg
// calculate y avg
for(int i=0;i<xLine;i++){
xavg = xavg + x[i];
yavg = yavg + y[i];
}
xavg = xavg/xLine;
yavg = yavg/yLine;
//calculate m
for(int i=0;i<xLine;i++){
m = m + ((x[i] - xavg) * (y[i] - yavg));
temp = temp + ((x[i] - xavg) * (x[i] - xavg));
}
m = m/temp;
//calculate b
b = yavg - (m*xavg);
//display b= y avg - m x avg
cout<<"We find the equation of the line to be: y = "<< xavg<<"*x + "<<b<<endl;
getch();
}
Least Squares Line Fit When given a set of data that looks like a line, engineers often want to find the y mx+b line that best fits the data. In other words, when given a list of xvalues and a list of corresponding y values, we want to find the slope m and intercept b of the line that comes closest to the x and y data points. This is actually a straightforward process via the method of "least squares", based on minimizing the error between the actual data points and the line one gets using certain values of m and b. We will not cover the derivation here, but the results we will use are below Assume that each of our x values is called x and each of our y values is called y, so that a single data point is (x, y) igoes from 1 to n, which is the number of data points we have. We define average x and y values: Xi Then we find the best-fit slope to be 2 (x-x.vg) and the best-fit intercept to be Project Overview: Our project will be to get a file name from the user, read x and y data from file, display the data at the console, allow the user to remove points from the data, then find the best-fit slope and intercept and send them to the console. In more detail, the project program you write will do the following: Prompt the user for a file name, which will be stored in a string variable. Use the ADVANCE-"no in the write statement for the prompt to get the prompt and the user's response on the same line. Read the x and y data from the file and store it in two dynamic, double-precision real arrays. This will require counting the lines in the file to see how many data lines there are and then allocatingExplanation / Answer
As my best of Knoledge and working Experience the below is the desired code
Feel free to comment if any queries
dont forget to rate possitive which will make me do more in chegg
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.