Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include "f.h"
- int main (int argc, const char **argv)
- {
- FILE *in;
- double x0,*x,*y,res;
- int n,i;
- clock_t time;
- if(argc!=3)
- {
- fprintf(stderr,"Usage %s n input_file x0\n",argv[0]);
- return ERROR_READ;
- }
- if(sscanf(argv[1], "%d" ,&n)!=1 || n<2)
- {
- fprintf(stderr,"Can not read n!\n");
- return ERROR_READ;
- }
- if(!(x = (double *)malloc(n*sizeof(double))))
- {
- fprintf(stderr,"Can not allocate memory x[n]\n");
- return ERROR_MEMORY;
- }
- if(!(y = (double *)malloc(n*sizeof(double))))
- {
- fprintf(stderr,"Can nor allocate memory y[n]\n");
- return ERROR_MEMORY;
- }
- if(!(in = fopen(argv[2],"r")))
- {
- fprintf(stderr,"Can nor read %s\n",argv[2]);
- free(x);
- free(y);
- return ERROR_OPEN;
- }
- for(i = 0; i<(n);i++)
- {
- if(fscanf(in,"%lf",x + i)!=1)
- {
- fprintf(stderr,"Can nor read elments from %s\n",argv[2]);
- free(x);
- free(y);
- return ERROR_READ;
- }
- if(fscanf(in,"%lf",y + i)!=1)
- {
- fprintf(stderr,"Can nor read elments from %s\n",argv[2]);
- free(x);
- free(y);
- return ERROR_READ;
- }
- }
- fclose(in);
- printf("x0 = ");
- if( scanf("%lf", &x0)!=1 )
- {
- fprintf(stderr, "Error! Can not read x0!\n");
- free(x);
- free(y);
- return ERROR_READ;
- }
- /*if(sscanf(argv[4], "%lf", &x0)!=1)
- {
- fprintf(stderr,"Can not read %s\n",argv[4]);
- free(x);
- free(y);
- return ERROR_READ;
- }*/
- time = clock();
- res = fun(n,x0,x,y);
- printf("RESULT: %lf\n TIME: %lf\n",res,((double)(clock() - time) )/CLOCKS_PER_SEC);
- free(x);
- free(y);
- return 0;
- }
- #include<stdio.h>
- #include<stdlib.h>
- #include "f.h"
- double fun(int n,double x0,double*x,double*y)
- {
- int i,j;
- double res = y[0],k = 1;
- for(i=0;i<n-1;i++)
- {
- k *=(x0 - x[i]);
- for(j=0;j<(n-1-i);j++)
- y[j] = (y[j + 1] - y[j])/(x[j + i + 1] - x[j]);
- res += y[0]*k;
- }
- return res;
- }
- #ifndef F_H
- #define F_H
- double fun(int n,double x0, double *x, double *y);
- #define ERROR_READ 1
- #define ERROR_OPEN 2
- #define ERROR_MEMORY 3
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement