Advertisement
Guest User

C Plotting Program

a guest
May 8th, 2014
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <grx20.h>
  5. #include <grxkeys.h>
  6.  
  7. int data_count, i;
  8. float sum_y, sum_x, sum_x_squared, sum_xy=0;
  9. float gradient, y_intercept, *x_values, *y_values;
  10. float x[100],y[100];
  11. FILE *store;
  12.  
  13. void file_save(void) {
  14.      store = fopen("j:/StoredValues.txt", "w");
  15.      for (i=0; i<data_count; i++) {
  16.      fprintf ( store, "%f,%f\n", x_values[i], y_values[i]);
  17.      }
  18.      fclose(store);
  19. }
  20.  
  21. void file_read(void) {
  22.      store = fopen ("j:/StoredValues.txt", "r");
  23.      if (store == NULL )
  24.               printf("\nError: Failed To Open Previous Data File - Program Will Continue Anyway\n");
  25.      else {
  26.               printf("\nSuccess: Data From Previous Run Imported\n");
  27.               i=0;
  28.               do {
  29.               fscanf ( store, "%f,%f\n", &x[i], &y[i]);
  30.               x_values = x;
  31.               y_values = y;
  32.               printf("%12f%12f\n", x_values[i], y_values[i]);
  33.               i=i+1;
  34.               } while (!feof(store));
  35.               fclose(store);
  36.      }
  37. }
  38.  
  39. void calculations(void) {
  40.      for (i=0; i<data_count; i++) {
  41.          sum_x = sum_x + x_values[i];
  42.          sum_x_squared = sum_x_squared + (x_values[i]*x_values[i]);
  43.          sum_y = sum_y + y_values[i];
  44.          sum_xy = sum_xy + (y_values[i]*x_values[i]);
  45.      }
  46.      if (data_count==1) { /*Prevents Attempting To Calculate A Gradient Of A Single Point*/
  47.          gradient=0;
  48.          y_intercept=0;
  49.      }
  50.      else {
  51.      gradient = (data_count*sum_xy - sum_x*sum_y)/(data_count*sum_x_squared - (sum_x*sum_x));
  52.      y_intercept = (sum_y - gradient*sum_x)/data_count;
  53.      }
  54. }
  55.    
  56. int main()
  57. {
  58.     int menu;
  59.    
  60.     printf("PROGRAM TO DRAW LINE OF BEST FIT\n");
  61.    
  62.     menu = 0;
  63.     file_read();
  64.     while (menu != 4) {
  65.           printf("\n1. Enter Data Points\n");
  66.           printf("2. View Data Points\n");
  67.           printf("3. Create Graph From Data\n");
  68.           printf("4. Exit Program\n");
  69.           printf("\nEnter An Option: ");
  70.           scanf("%d", &menu);
  71.          
  72.           switch(menu) {
  73.           case 1:
  74.                {
  75.                printf("\nPlease Enter How Many Data Points You Wish To Enter: \n");
  76.                scanf("%d", &data_count);
  77.                x_values=(float*)calloc(data_count,sizeof(*x_values)); /*Allows for dynamicly sized arrays*/
  78.                y_values=(float*)calloc(data_count,sizeof(*y_values));
  79.                if (x_values==NULL) {
  80.                    printf("Error! Memory Could Not Be Allocated. ");
  81.                    exit(0);
  82.                }
  83.                printf("\nPlease Enter Your X Values: \n");
  84.                for(i=0;i<data_count;i++) {
  85.                printf("\nx<%d>: ", i);
  86.                scanf("%f", x_values+i);
  87.                }
  88.                printf("\nPlese Enter Your Y Values: \n");
  89.                for(i=0;i<data_count;i++) {
  90.                printf("\ny<%d>: ", i);
  91.                scanf("%f",y_values+i);
  92.                }
  93.                calculations();
  94.                file_save();
  95.                break;
  96.                }
  97.                
  98.           case 2:
  99.                {
  100.                if (x_values==NULL) {
  101.                printf("\n\nError! Please Enter Data First\n\n");
  102.                break;
  103.                }
  104.                printf("\nEntered Values: \n");
  105.                printf("\n%12s%12s\n\n", "x", "y");
  106.                for(i=0;i<data_count;i++) {
  107.                printf("%12f%12f\n", x_values[i], y_values[i]);
  108.                }
  109.                printf("\nSum Of X Values: %f\n", sum_x);
  110.                printf("Sum Of Y Values: %f\n", sum_y);
  111.                printf("Gradient Of The Line: %f\n", gradient);
  112.                printf("Y Intercept Of The Line: %f\n", y_intercept);
  113.                break;                  
  114.                }
  115.           case 3:
  116.           case 4:
  117.                default:
  118.                  {
  119.                    printf("\n\nERROR invalid\n\n");
  120.                  }
  121.                  }
  122.                  }
  123.                  
  124.     free(x_values);
  125.     free(y_values);
  126.     return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement