[ create a new paste ] login | about

Link: http://codepad.org/CsPdJwDX    [ raw code | output | fork ]

C++, pasted on Apr 15:
#include <iostream>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <cstdlib>
#include <ctime>

class RealRand {
    double top;
public:
    RealRand(double t) : top(t) {}
    double operator () () { return top * fabs(sin(rand())); }
};

int main(){
    const double TOP_VALUE(10.0);
    const double DEF_VALUE(0.5);
    
    srand(time(0));
    
    const size_t N(10);
    double a[N];
    std::generate_n(a, N, RealRand(TOP_VALUE));
    std::copy(a, a + N, std::ostream_iterator<double>(std::cout, " "));
    std::cout << std::endl;
    double * pMaxA = (double*)std::max_element(a, a + N);
    std::fill_n(pMaxA + 1, N - (pMaxA - a + 1), DEF_VALUE);
    std::copy(a, a + N, std::ostream_iterator<double>(std::cout, " "));
    std::cout << std::endl;
    
    std::cout << std::endl;
    
    const size_t M(8);
    double b[M];
    std::generate_n(b, M, RealRand(TOP_VALUE));
    std::copy(b, b + M, std::ostream_iterator<double>(std::cout, " "));
    std::cout << std::endl;
    double * pMaxB = (double*)std::max_element(b, b + M);
    std::fill_n(pMaxB + 1, M - (pMaxB - b + 1), DEF_VALUE);
    std::copy(b, b + M, std::ostream_iterator<double>(std::cout, " "));
    std::cout << std::endl;
    
    return 0;
}


Output:
1
2
3
4
5
1.24191 7.37201 8.75 0.151733 6.81053 7.61797 5.31498 8.12619 9.93915 9.67783 
1.24191 7.37201 8.75 0.151733 6.81053 7.61797 5.31498 8.12619 9.93915 0.5 

7.66852 7.74806 6.0759 8.19559 9.69423 6.20987 5.28028 0.0350438 
7.66852 7.74806 6.0759 8.19559 9.69423 0.5 0.5 0.5 


Create a new paste based on this one


Comments: