Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <grx20.h>
- #include <grxkeys.h>
- int data_count, i;
- float sum_y, sum_x, sum_x_squared, sum_xy=0;
- float gradient, y_intercept, *x_values, *y_values;
- float x[100],y[100];
- FILE *store;
- void file_save(void) {
- store = fopen("j:/StoredValues.txt", "w");
- for (i=0; i<data_count; i++) {
- fprintf ( store, "%f,%f\n", x_values[i], y_values[i]);
- }
- fclose(store);
- }
- void file_read(void) {
- store = fopen ("j:/StoredValues.txt", "r");
- if (store == NULL )
- printf("\nError: Failed To Open Previous Data File - Program Will Continue Anyway\n");
- else {
- printf("\nSuccess: Data From Previous Run Imported\n");
- i=0;
- do {
- fscanf ( store, "%f,%f\n", &x[i], &y[i]);
- x_values = x;
- y_values = y;
- printf("%12f%12f\n", x_values[i], y_values[i]);
- i=i+1;
- } while (!feof(store));
- fclose(store);
- }
- }
- void calculations(void) {
- for (i=0; i<data_count; i++) {
- sum_x = sum_x + x_values[i];
- sum_x_squared = sum_x_squared + (x_values[i]*x_values[i]);
- sum_y = sum_y + y_values[i];
- sum_xy = sum_xy + (y_values[i]*x_values[i]);
- }
- if (data_count==1) { /*Prevents Attempting To Calculate A Gradient Of A Single Point*/
- gradient=0;
- y_intercept=0;
- }
- else {
- gradient = (data_count*sum_xy - sum_x*sum_y)/(data_count*sum_x_squared - (sum_x*sum_x));
- y_intercept = (sum_y - gradient*sum_x)/data_count;
- }
- }
- int main()
- {
- int menu;
- printf("PROGRAM TO DRAW LINE OF BEST FIT\n");
- menu = 0;
- file_read();
- while (menu != 4) {
- printf("\n1. Enter Data Points\n");
- printf("2. View Data Points\n");
- printf("3. Create Graph From Data\n");
- printf("4. Exit Program\n");
- printf("\nEnter An Option: ");
- scanf("%d", &menu);
- switch(menu) {
- case 1:
- {
- printf("\nPlease Enter How Many Data Points You Wish To Enter: \n");
- scanf("%d", &data_count);
- x_values=(float*)calloc(data_count,sizeof(*x_values)); /*Allows for dynamicly sized arrays*/
- y_values=(float*)calloc(data_count,sizeof(*y_values));
- if (x_values==NULL) {
- printf("Error! Memory Could Not Be Allocated. ");
- exit(0);
- }
- printf("\nPlease Enter Your X Values: \n");
- for(i=0;i<data_count;i++) {
- printf("\nx<%d>: ", i);
- scanf("%f", x_values+i);
- }
- printf("\nPlese Enter Your Y Values: \n");
- for(i=0;i<data_count;i++) {
- printf("\ny<%d>: ", i);
- scanf("%f",y_values+i);
- }
- calculations();
- file_save();
- break;
- }
- case 2:
- {
- if (x_values==NULL) {
- printf("\n\nError! Please Enter Data First\n\n");
- break;
- }
- printf("\nEntered Values: \n");
- printf("\n%12s%12s\n\n", "x", "y");
- for(i=0;i<data_count;i++) {
- printf("%12f%12f\n", x_values[i], y_values[i]);
- }
- printf("\nSum Of X Values: %f\n", sum_x);
- printf("Sum Of Y Values: %f\n", sum_y);
- printf("Gradient Of The Line: %f\n", gradient);
- printf("Y Intercept Of The Line: %f\n", y_intercept);
- break;
- }
- case 3:
- case 4:
- default:
- {
- printf("\n\nERROR invalid\n\n");
- }
- }
- }
- free(x_values);
- free(y_values);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement