Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include "count2l7.h"
- void print_array(double* a,int n)
- {
- int j;
- int NMAX=5;
- for (j=0;j<n;j++)
- {
- if (j%NMAX==0) printf("\n");
- printf("%lf ",a[j]);
- }
- }
- void init_array(double* a,int n,int key)
- {
- int k;
- srand(key);
- for (k=0;k<n;k++) a[k]=rand();
- }
- int read_array(const char* name,double* a,int n)
- {
- FILE *fp;
- int i;
- if (!(fp=fopen(name,"r")))
- {
- return -1;
- }
- for (i=0;i<n;i++)
- {
- if (fscanf(fp,"%lf",a+i)!=1)
- {
- if (!feof(fp))
- {
- fclose(fp); return -2;
- }
- fclose(fp); return i;
- }
- }
- fclose(fp); return i;
- }
- void combination(double* a, double* b, double* c, int n,int m)
- {
- int i,j,k,f;
- f==n+m;
- for(i=0, j=0,k=0; i<n - 1 && j<m - 1&&k<f - 1; i++, j++,k++)
- {if (a[i]<b[j]) {c[k]=a[i];
- i++;
- }
- else {c[k]=b[j];
- j++;
- }
- }
- }
- int main(void)
- {
- int s,n,key,f,m;
- f==n+m;
- double x;
- double *a;
- double *b;
- double *c;
- double t;
- int res;
- int res2=0;
- const char* name="posledovat.txt";
- const char* name2="posledovat2.txt";
- printf("Input n and key:");
- if (scanf("%d%d",&n,&key)!=2)
- {
- printf("cannot read:\n");
- return 1;
- }
- if (!(a=(double*)malloc(n*sizeof(double))))
- {
- printf("not enough memory\n");
- return 2;
- }
- if (key==0)
- {
- res=read_array(name,a,n);
- if (res<0)
- {
- switch(res)
- {
- case (-1):
- {
- printf("cannot open %s\n", name);
- break;
- }
- case (-2):
- {
- printf("cannot read %s\n", name);
- break;
- }
- default:
- {
- printf("unknown error %d:%s\n", res, name);
- break;
- }
- free(a);
- return 3;
- }
- }
- n=res;
- }
- printf("Input m and key:");
- if (scanf("%d%d",&m,&key)!=2)
- {
- printf("cannot read:\n");
- return 1;
- }
- if (!(a=(double*)malloc(n*sizeof(double))))
- {
- printf("not enough memory\n");
- return 2;
- }
- if (key==0)
- {
- res=read_array(name,b,m);
- if (res<0)
- {
- switch(res)
- {
- case (-1):
- {
- printf("cannot open %s\n", name);
- break;
- }
- case (-2):
- {
- printf("cannot read %s\n", name);
- break;
- }
- default:
- {
- printf("unknown error %d:%s\n", res, name);
- break;
- }
- free(b);
- return 3;
- }
- }
- m=res;
- }
- else
- init_array(a,n,key);
- init_array(b,m,key);
- print_array(a,n); printf("\n");
- combination(a,b,c,n,m);
- t=clock();
- t=(clock()-t)/CLOCKS_PER_SEC;
- printf("\nelapsed=%.21f\n",t);
- free(a);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement