Advertisement
Guest User

Pseudoslucajni

a guest
Feb 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4. #include <ctime>
  5. using namespace std;
  6.  
  7. int main(){
  8.     srand(time(NULL));
  9.     int N;
  10.     do{
  11.         cout << "Unesite prirodan broj N: ";
  12.         cin >> N;
  13.     }while(N < 0);
  14.     fstream dat;
  15.     dat.open("datoteka.dat", ios::out | ios::binary);
  16.     int broj1 = rand();
  17.     int broj2;
  18.     dat.write((char*)&broj1, sizeof(int));
  19.     for(int i=0;  i<N-1; i++){
  20.         do{
  21.             broj2 = rand();
  22.         }while(broj2 > broj1);
  23.         dat.write((char*)&broj2, sizeof(int));
  24.         broj1 = broj2;
  25.     }
  26.     dat.close();
  27.     dat.clear();
  28.     dat.open("datoteka.dat", ios::in | ios::binary);
  29.     while(1){
  30.         dat.read((char*)&broj1, sizeof(broj1));
  31.         if(dat.eof()) break;
  32.         cout << broj1 << " ";
  33.     }
  34.     dat.close();
  35.     dat.clear();
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement