Advertisement
Zeroblade

linear regression for advanse

Feb 5th, 2012
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. float mean(float *x, int n);
  5. float summation(float *x, int n);
  6. float summation2(float *x, float *y, int n);
  7. void estimate(float intercept, float slope);
  8. int getN();
  9.  
  10. int main() {
  11.     int n = getN(); //number of programs
  12.     float x[n] = 0; //estimated LOC for program n
  13.     float y[n] = 0; //actual LOC for program n
  14.    
  15.     float sumX = 0;
  16.     float sumY = 0;
  17.     float sumXX = 0;
  18.     float sumXY = 0;
  19.    
  20.     float slope = 0;
  21.     float intercept = 0;
  22.    
  23.     //get the x and y
  24.     int i = 0;
  25.     for(i=0; i<n; i++) {
  26.         printf("Input estimated LOC for program %d: ", i+1);
  27.         scanf("%f", x[i]);
  28.         printf("Input actual LOC for program %d: ", i+1);
  29.         scanf("%f", y[i]);
  30.     }
  31.    
  32.     //calculation
  33.    
  34.     //summation(x, n);
  35.    
  36.     /* calculate summations (summation)
  37.      * calculate x*y summations (summation2)
  38.     */
  39.    
  40.     slope = ((n*sumXY) - (sumX*sumY)) / ((n*sumXX)-(sumX*sumX));
  41.    
  42.     intercept = ((sumY - (slope*sumX)) / n);
  43.    
  44.     printf("Regression equation is:\n  y = %f +%fx", intercept, slope);
  45.    
  46.     //ask user for programs to estimate
  47.     //make a new function for that
  48.     //continue to work until user types exit
  49.    
  50.     printf("Exiting program. Press any key to continue.");
  51.     getch();
  52. }
  53.  
  54.  
  55.  
  56. float mean(float *x, int n) {
  57.     float result = 0;
  58.     int i = 0;
  59.     for(i=0; i<n; i++) {
  60.         result = result + x[i]
  61.     }
  62.     result = result/n;
  63.     return result;
  64. }
  65.  
  66. float summation(float *x, int n) {
  67.     float result = 0;
  68.     int i = 0;
  69.     for(i=0; i<n; i++) {
  70.         result = result + x[i];
  71.     }    
  72.     return result;
  73. }
  74.  
  75. float summation2(float *x, float *y, int n){
  76.     float result = 0;
  77.     int i = 0;
  78.     for(i=0; i<n; i++) {
  79.         result = result + x[i]*y[i];
  80.     }    
  81.     return result;
  82. }
  83.  
  84. float estimate(float intercept, float slope) {
  85.     int exit = 0;
  86.    
  87.     while() {
  88.         printf("");
  89.         //compute
  90.     }
  91. }
  92.  
  93. int getN() {
  94.     int x = 0;
  95.     printf("Input number of programs: ");
  96.     scanf("%d", &x);
  97.     return x;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement