Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <conio.h>
  5. void saveInFile(float *X,int S)
  6. {
  7. int i;
  8. FILE *cfPtr;
  9. if((cfPtr=fopen("arrayInF.txt","w"))==NULL)
  10. printf("File could not be opened\n");
  11. else
  12. {
  13. fprintf(cfPtr,"%d\n",S);
  14. for (i=0;i<S;i++)
  15. {
  16. fprintf(cfPtr,"%f\n",X[i],i);
  17.  
  18. }
  19. }
  20. fclose(cfPtr);
  21. }
  22. void readF(float *X, int R)
  23. {
  24. int i;
  25. FILE *fp;
  26. X=malloc(sizeof(int)*(R+1));
  27.  
  28. if((fp=fopen("arrayInF.txt","rt"))==NULL)
  29. printf("File could not be open\n");
  30. else
  31. {
  32. fscanf(fp,"%d",&R);
  33. printf("%d\n",R);
  34. for(i=0;i<R;i++)
  35. {
  36. fscanf(fp,"%f",&X[i]);
  37. printf("%f\n",X[i]);
  38. }
  39. }
  40. }
  41.  
  42. float minp(float x[],int size)
  43. {
  44. float min;
  45. int i;
  46.  
  47. min=x[0];
  48. for(i=0;i<size;i++)
  49. {
  50. if (((x[i]<min) || (min<0)) && (x[i]>=0)) min=x[i];
  51. }
  52. return min;
  53. }
  54.  
  55. void input_array(float x[], int size)
  56. {
  57. int i;
  58. printf("Input %d float numbers.\n",size);
  59. for(i=0;i<size;i++)
  60. {
  61. printf("x[%d]=",i+1);
  62. scanf("%f",&x[i]);
  63. }
  64. }
  65. int main(void)
  66. {
  67. float *x,min;
  68. int s;
  69.  
  70. srand(time(NULL));
  71. s=10-rand()%5;
  72. x=(float *) malloc(sizeof(float)*s);
  73. input_array(x,s);
  74. saveInFile(x,s);
  75. readF(x,s);
  76. min=minp(x,s);
  77. if (min>=0) printf("Min=%f\n",min);
  78. else
  79. {
  80. printf("Vse chisla otricatel`nie\n");
  81. }
  82. free(x);
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement