Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. #include "time.h"
  4. float func(float **a,int k,int m)
  5. {
  6. int j,s=0;
  7. float b=1;
  8. for(j=0;j<m;j++)
  9. {
  10. if(a[k][j]>=-1.5&&a[k][j]<=-1||a[k][j]>=1&&a[k][j]<=1.5)
  11. {
  12. b=b*a[k][j]*a[k][j];
  13. s=s+1;
  14. }
  15. }
  16. if(s==0) b=0;
  17. return b;
  18. }
  19.  
  20. void main()
  21. {
  22. srand(time(0));
  23. FILE *f_in=NULL;
  24. FILE *f_out=NULL;
  25.  
  26. f_in=fopen("in.txt","r");
  27. f_out=fopen("out.txt","w");
  28.  
  29. int m,n,i,k,j,key,ans;
  30. float res;
  31.  
  32. printf("Choose input. From console-1, from file-2\n");
  33. scanf("%d",&i);
  34.  
  35. if(i==1)
  36. {
  37. printf("Enter amount of lines and columns\nLines:");
  38. scanf("%d",&n);
  39. printf("Columns:");
  40. scanf("%d",&m);
  41. printf("\n");
  42. }
  43.  
  44. if(i==2)
  45. {
  46. if(f_in!=NULL)
  47. {
  48. fscanf(f_in,"%d %d",&n,&m);
  49. }
  50. }
  51.  
  52. float** a=(float**) malloc(n*sizeof(float*));
  53. for(k=0;k<n;k++)
  54. {
  55. a[k]=(float*)malloc(m*sizeof(float));
  56. }
  57. if(a==NULL) exit(2);
  58.  
  59. if(i==1)
  60. {
  61. for(k=0;k<n;k++)
  62. {
  63. for(j=0;j<m;j++)
  64. {
  65. a[k][j]=(0.1)*(-15+rand()%31);
  66. printf("%f ",a[k][j]);
  67. }
  68. printf("\n");
  69. }
  70. printf("\n");
  71. }
  72.  
  73. if(i==2)
  74. {
  75. for(k=0;k<n;k++)
  76. {
  77. for(j=0;j<m;j++)
  78. {
  79. fscanf(f_in,"%f",&a[k][j]);
  80. }
  81. }
  82. close(f_in);
  83. }
  84.  
  85. printf("Choose output. To console-1, to file-2\n");
  86. scanf("%d",&key);
  87. printf("\n");
  88. if(key==1)
  89. {
  90. for(k=0;k<n;k++)
  91. {
  92. res=func(a,k,m);
  93. printf("b[%d] = %f\n",k+1,res);
  94. }
  95. }
  96. if(key==2)
  97. {
  98. if(f_out!=NULL)
  99. {
  100. for(k=0;k<n;k++)
  101. {
  102. res=func(a,k,m);
  103. fprintf(f_out,"b[%d] = %f\n",k+1,res);
  104. }
  105.  
  106. close(f_out);
  107. }
  108. }
  109.  
  110. for(k=0;k<n;k++)
  111. {
  112. free(a[k]);
  113. }
  114. free(a);
  115.  
  116. if(i==1){
  117. printf("\nDo you want to continue work? Yes-1,No-0\n");
  118. scanf("%d",&ans);
  119. if(ans==1) main();
  120. }
  121. return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement