1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
double SumArray ( double [ ], int );
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define SQUARE(x) ((x)*(x))
// Defing a specific format for the listing instead of using
// multiple tabs
#define W "15"
#define Z "14"
#define U "%"Z"s"
#define S "%"W"s"
#define F1 "%"W".1f"
#define F2 "%"W".2f"
/* SumArray is a fonction that calculates the sum of any
array when it is called */
#include "rent.h"
double SumArray (double a[], int n)
{
int c;
double sum = 0;
for (c = 0; c < n; c++)
{
sum += a[c];
}
return sum;
}
#include "rent.h"
int main (void)
{
// Two arrays that are the ones provided from the file
double filearray1[14], filearray2[14],
// Three arrays to store in them further calculations
filearray3[14], filearray4[14],
filearray5[14];
// Setting counter to 0
size_t r = 0;
FILE *fp;
// File that we will read from
char fname[] = "rent2.dat";
if ((fp = fopen (fname, "r")) == NULL)
{
printf ("Unable to open %s.\n", fname);
exit (1);
}
printf (S S "\n", "Rent","Income");
printf (U U U U U "\n", "X","Y","X^2","Y^2","XY");
while (r < (sizeof filearray1/sizeof *filearray1 )
&& fscanf (fp, "%lf%lf\n", &filearray1[r], &filearray2[r]) != EOF)
{
// The three arrays which contain calculations:
// Squaring the first array read and storing it in another one
filearray3[r] = SQUARE (filearray1[r]);
// Doing the same thing with the second one
filearray4[r] = SQUARE (filearray2[r]);
// Product of the first two arrays
filearray5[r] = filearray1[r] * filearray2[r];
// Priting everything
printf (F1 F1 F2 F2 F2 "\n",
filearray1[r], filearray2[r], filearray3[r],
filearray4[r], filearray5[r]);
r++;
}
// Summing all the arrays and storing results
// in new double variables so that further
// will be easier
double SumX = SumArray (filearray1,r),
SumY = SumArray (filearray2,r),
SumXX = SumArray (filearray3,r),
SumYY = SumArray (filearray4,r),
SumXY = SumArray (filearray5,r);
printf (S S S S S "\n", "------","--------","---------","-------------","-----------");
// Printing the total at the end of each array
printf (F1 F1 F1 F1 F1"\n", SumX, SumY, SumXX, SumYY, SumXY);
printf("\n\n\t\t\t Press ENTER to Continue");
getchar();
/* Coefficient of correlation calculations */
// Number of elements contained in each array
int N = 14;
// Variable of the coefficient
double coef;
// Declaring two others so that the formula is easier
double coef1, coef2;
// Calculation of the coefficient using formula provided
coef1 = N*SumXY - (SumX)*(SumY);
coef2 = sqrt((N*SumXX - SumX*SumX)*(N*SumYY - SumY*SumY));
coef = coef1/coef2;
printf ("\n\n\t +=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+ ");
printf ("\n\t | Coefficient of correlation is: | ");
printf ("\n\t | r = + %.2lf | ", coef);
printf ("\n\t +=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+ \n");
/* Regression equations calculation */
// Variables needed for the equation
double a, b, b1, b2, avgX, avgY;
// Averaging X and Y
avgX = SumX/N;
avgY = SumY/N;
// Calculating b
b1 = SumXY - N*avgX*avgY;
b2 = SumXX - N*avgX*avgX;
b = b1/b2;
// Calculating a
a = avgY - b*avgX;
// Plugging a and b and priting the equation
printf("\n\t Regression equation is: \n");
printf("\t =-=-=-=-=-=-=-=-=-=-=-= \n");
printf("\n\t Yc = a + bX = %.2lf + %.2lfX\n", a, b);
printf("\n\t( a = %.6lf , b = %.6lf )\n\n", a, b);
/* Stand error of estimate */
double Sy;
Sy = (SumYY - (a*SumY + b*SumXY))/N;
Sy = sqrt (Sy);
printf("\t +=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=+ ");
printf("\n\t | The Standard error of estimate: |");
printf("\n\t | Sy = +/- $%.2lf |", Sy);
printf("\n\t +=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=+\n");
printf("\n\t\t\t Press ENTER to continue \n");
getchar();
// Variables needed for prediction of the income
double rent, choice, income, lower, upper;
do
{
printf("\n\nEnter a rent amount (to quit enter 0) : ");
scanf("%lf", &rent);
if(rent > 0)
{
income = a + (b*rent);
lower = income - Sy;
upper = income + Sy;
printf("\n\n\t Rent \t\t Lower Limit \t Predicted Income \t Upper Limit \n");
printf("\n\t$ %.2lf \t %.2lf \t %.2lf \t\t %.2lf \n", rent, lower, income, upper);
}
else if ( rent < 0 )
{
printf("\nError! Entry not valid, try again.\n");
}
}
while (rent != 0);
return 0;
} |
Partager