![]() |
|
#1
|
|||
|
|||
Linear RegressionCPP / C++ / C Code:
Now I need to calculate m and b.... m = ![]() and b= ![]() How can I do that? |
|||
|
#2
|
|||
|
|||
Re: Linear RegressionQuote:
It's done with loops. I'll get you started: CPP / C++ / C Code:
Here's a run: Code:
You probably want to use floating point variables for your data points even if the inputs all have integer values since you will be dividing by stuff that may make the results to be non-integers. Unless every calculation in your program always has an integer result, I'm thinking it might be better to use floating point data throughout. Gratuitous use of global variables is generally frowned on, so I made the declarations inside main(). There are lots of reasons for this even though it may not be apparent just now. My point is that acquiring "good" habits from the beginning may be less stressful than trying to break "bad" habits later. Regards, Dave |
|
#3
|
|||
|
|||
Re: Linear RegressionThanks, that helped a lot...
but I'm having problem with that code CPP / C++ / C Code:
Please enter values of (x,y) Enter a value for x_1: 3 Enter a value for y_1: 6 Enter a value for x_2: 9 Enter a value for y_2: 12 12 18 126 90 However the output should be like that 12 18 99 729 |
|
#4
|
|||
|
|||
Re: Linear RegressionQuote:
I would probably make the print statements a little more verbose. Maybe something like CPP / C++ / C Code:
Then the output might look like Code:
If there is any question about what numbers were used to arrive at those numbers, I might print out the numbers themselves. Now: are these the correct values for the sums? Hmmm... According to my abacus: Sum of x values = 3+9 = 12 (check) Sum of y values = 6+12 = 18 (check) Sum of x*y values = 3*6+9*12 = 18+108 = 126 (check) Sum of x*x values = 3*3+9*9 = 9+81 = 90 (check) So I think the printout shows the correct values. However... There is some undefined behavior with your program. See footnote. CPP / C++ / C Code:
This statement guarantees that the initial value of sumsqx is equal to zero, but does not initialize the others. Apparently your compiler helpfully sets them to zero, but that is not guaranteed for other compilers (or, maybe not even for other programs using this statement with the same compiler or with later versions of the same compiler). To guarantee the results, Initialize each one separately. You can do it like this: CPP / C++ / C Code:
Or, some people prefer to do each one on its own line since humans who want to check the code might be less likely to overlook a mistake like yours; CPP / C++ / C Code:
Now, as a matter of style, arrays in C and C++ start with index zero, not 1. I mean there's nothing "wrong" with using x[1], x[2], y[1] and y[2], but I think it might be better to get used to the zero-based arrays. Wasting an element in each array (not using x[0] or y[0]) might not seem important for a trivial program like this, but it's just not the usual style. So the loops might look like CPP / C++ / C Code:
The fact that arrays are zero-based is probably the reason that the most common idiom in C and C++ programming for doing something "n" times is CPP / C++ / C Code:
Regards, Dave One kind of undefined behavior is using variables that weren't initialized by the program. Some compilers always initialize variables to zero, some do not. Bottom line: don't count on it! (A little digital pun. Get it? Don't count... No? Oh, well) The main implication: Because of the possibility of undefined behavior: In general, you can't prove that a program is correct by testing alone. |
Recent GIDBlog
Problems with the Navy (Chiefs) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| MPI with C (Solving Linear Equation using Gauss-Seidel Iteration) | trainz13 | C Programming Language | 7 | 01-May-2007 23:38 |
| Linear Search | eccoflame | C Programming Language | 3 | 19-Apr-2005 09:36 |
| C++ Linear Regression with Outliers | bartb | C++ Forum | 1 | 06-Mar-2005 09:27 |
| Ineed some help with thos code - Linear Linked List | sosy2001 | C Programming Language | 6 | 11-Nov-2004 11:23 |
| Linear search on a multidimensional array. | anignna | C++ Forum | 4 | 07-Mar-2004 21:07 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The