Advertisement
Guest User

Untitled

a guest
May 26th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. void solve(double**, int, int);
  6. double ves(double*, int);
  7. void swapstr(double**, int, int, int);
  8. void mult(double**, int, int, FILE*);
  9.  
  10. void swapstr(double **a, int m, int i, int j)
  11. {
  12. int k; double tmp;
  13. double *pi = a[i], *pj = a[j];
  14. for (k=0;k<m;k++) {
  15. tmp = pi[k];
  16. pi[k] = pj[k];
  17. pj[k] = tmp;
  18. }
  19. }
  20.  
  21. double ves(double *str, int m)
  22. {
  23. int i; double v = 0;
  24. for (i=1;i<m;i++) {
  25. if (fabs(str[i]-str[i-1]) > v)
  26. v = fabs(str[i]-str[i-1]);
  27. }
  28. return v;
  29. }
  30.  
  31. void solve(double **a, int n, int m)
  32. {
  33. int i,j;
  34. for (i=0;i<n;i++) {
  35. for (j=0;j<n-1-i;j++) {
  36. if (ves(a[j],m) < ves(a[j+1],m))
  37. swapstr(a,m,j,j+1);
  38. }
  39. }
  40. }
  41.  
  42. void mult(double **a, int n, int m, FILE *out)
  43. {
  44. int i,j; double sum = 0;
  45. for(i=0;i<n;i++) {
  46. sum = 0;
  47. for(j=0;j<m;j++)
  48. sum += a[i][j]*a[n-1][j];
  49. fprintf(out,"%g ",sum);
  50. }
  51. }
  52.  
  53. int main(void)
  54. {
  55. double **a;
  56. int i,j,n,m;
  57. FILE *in, *out;
  58. in = fopen("input.txt","r");
  59. if (in == NULL)
  60. return -1;
  61. if (fscanf(in,"%d%d",&n,&m)!=2) {
  62. fclose(in);
  63. return -1;
  64. }
  65. if (m<1 || n<1) {
  66. fclose(in);
  67. return -1;
  68. }
  69. a = (double**)malloc(n*sizeof(double*));
  70. for (i=0;i<n;i++)
  71. a[i]=(double*)malloc(m*sizeof(double));
  72. for (i=0;i<n;i++) {
  73. for (j=0;j<m;j++) {
  74. if (fscanf(in,"%lf",&a[i][j])!=1) {
  75. fclose(in);
  76. for (i=0;i<n;i++) free(a[i]);
  77. free(a);
  78. return -1;
  79. }
  80. }
  81. }
  82. fclose(in);
  83. solve(a,n,m);
  84. out = fopen("output.txt","w");
  85. fprintf(out,"%d %d\n",n,m);
  86. for(i=0;i<n;i++) {
  87. for(j=0;j<m;j++)
  88. fprintf(out,"%g ",a[i][j]);
  89. fprintf(out,"\n");
  90. }
  91. fprintf(out,"\n");
  92. mult(a,n,m,out);
  93. fclose(out);
  94. for (i=0;i<n;i++)
  95. free(a[i]);
  96. free(a);
  97. return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement