altvnv

Untitled

Apr 23rd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdio>
  4. #include "time.h"
  5.  
  6. using namespace std;
  7.  
  8. int main(){
  9. srand((unsigned)time(NULL));
  10.  
  11. fstream file("in.bin", ios::out | ios::binary), out("out.bin", ios::out | ios::binary);
  12.  
  13. if (!file.is_open()){
  14. cout << "[FILE] Error reading file" << endl;
  15. return 0;
  16. } else {
  17. cout << "[FILE] File successfuly opened" << endl;
  18. }
  19.  
  20. for (int i = 0; i<1010; i++){
  21. double data = (rand()%1000010*100.0)/10000;
  22. file.write(reinterpret_cast<char*> (&data), sizeof(double));
  23. }
  24.  
  25. file.close();
  26.  
  27. file.open("in.bin", ios::in | ios::binary);
  28.  
  29. double* array = new double[1011];
  30. int size = 0;
  31.  
  32. while (!file.eof()){
  33. double data;
  34. file.read(reinterpret_cast<char*> (&data), sizeof(double));
  35. array[size++] = data;
  36. }
  37.  
  38. double ans = -INT_MAX;
  39.  
  40. for (int i = 0; i<size; i++){
  41. if (i % 9 == 0 && i != 0){
  42. cout << "[LOG] Wrote maximum from previous 10 elements: " << ans * 1.0 << endl;
  43. out.write(reinterpret_cast<char*> (&ans), sizeof(double));
  44. ans = -INT_MAX;
  45. }
  46.  
  47. ans = max(ans, array[i]);
  48. }
  49.  
  50. out.close();
  51. file.close();
  52.  
  53. return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment