Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include "count2l7.h"
  5.  
  6.  
  7.  
  8.  
  9. void print_array(double* a,int n)
  10. {
  11.     int j;
  12.     int NMAX=5;
  13.     for (j=0;j<n;j++)
  14.     {
  15.         if (j%NMAX==0) printf("\n");
  16.         printf("%lf ",a[j]);
  17.     }
  18. }
  19.  
  20.  
  21. void init_array(double* a,int n,int key)
  22. {
  23.     int k;
  24.     srand(key);
  25.     for (k=0;k<n;k++) a[k]=rand();
  26. }
  27.  
  28. int read_array(const char* name,double* a,int n)
  29. {
  30.     FILE *fp;
  31.     int i;
  32.     if (!(fp=fopen(name,"r")))
  33.     {
  34.         return -1;
  35.     }
  36.     for (i=0;i<n;i++)
  37.     {
  38.         if (fscanf(fp,"%lf",a+i)!=1)
  39.         {
  40.             if (!feof(fp))
  41.             {
  42.                 fclose(fp); return -2;
  43.             }
  44.             fclose(fp); return i;
  45.         }
  46.     }
  47.     fclose(fp); return i;
  48. }
  49. void combination(double* a, double* b, double* c, int n,int m)
  50. {
  51. int i,j,k,f;
  52. f==n+m;
  53. for(i=0, j=0,k=0; i<n - 1 && j<m - 1&&k<f - 1; i++, j++,k++)
  54. {if (a[i]<b[j]) {c[k]=a[i];
  55. i++;
  56. }
  57. else {c[k]=b[j];
  58. j++;
  59. }
  60. }
  61. }
  62.  
  63. int main(void)
  64. {
  65.     int s,n,key,f,m;
  66.     f==n+m;
  67.     double x;
  68.     double *a;
  69.     double *b;
  70.     double *c;
  71.     double t;
  72.     int res;
  73.     int res2=0;
  74.     const char* name="posledovat.txt";
  75.     const char* name2="posledovat2.txt";
  76.  
  77.     printf("Input n and key:");
  78.     if (scanf("%d%d",&n,&key)!=2)
  79.     {
  80.         printf("cannot read:\n");
  81.         return 1;
  82.     }
  83.     if (!(a=(double*)malloc(n*sizeof(double))))
  84.     {
  85.         printf("not enough memory\n");
  86.         return 2;
  87.     }
  88.     if (key==0)
  89.     {
  90.         res=read_array(name,a,n);
  91.         if (res<0)
  92.         {
  93.             switch(res)
  94.             {
  95.             case (-1):
  96.                 {
  97.                     printf("cannot open %s\n", name);
  98.                     break;
  99.                 }
  100.             case (-2):
  101.                 {
  102.                     printf("cannot read %s\n", name);
  103.                     break;
  104.                 }
  105.             default:
  106.                 {
  107.                     printf("unknown error %d:%s\n", res, name);
  108.                     break;
  109.                 }
  110.             free(a);
  111.             return 3;
  112.             }
  113.         }
  114.         n=res;
  115.  
  116.     }
  117. printf("Input m and key:");
  118.     if (scanf("%d%d",&m,&key)!=2)
  119.     {
  120.         printf("cannot read:\n");
  121.         return 1;
  122.     }
  123.     if (!(a=(double*)malloc(n*sizeof(double))))
  124.     {
  125.         printf("not enough memory\n");
  126.         return 2;
  127.     }
  128.     if (key==0)
  129.     {
  130.         res=read_array(name,b,m);
  131.         if (res<0)
  132.         {
  133.             switch(res)
  134.             {
  135.             case (-1):
  136.                 {
  137.                     printf("cannot open %s\n", name);
  138.                     break;
  139.                 }
  140.             case (-2):
  141.                 {
  142.                     printf("cannot read %s\n", name);
  143.                     break;
  144.                 }
  145.             default:
  146.                 {
  147.                     printf("unknown error %d:%s\n", res, name);
  148.                     break;
  149.                 }
  150.             free(b);
  151.             return 3;
  152.             }
  153.         }
  154.         m=res;
  155.  
  156.     }
  157.  
  158.     else
  159.     init_array(a,n,key);
  160.     init_array(b,m,key);
  161.     print_array(a,n); printf("\n");
  162.     combination(a,b,c,n,m);
  163.     t=clock();
  164.     t=(clock()-t)/CLOCKS_PER_SEC;
  165.     printf("\nelapsed=%.21f\n",t);
  166.     free(a);
  167.     return 0;
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement