niedziela, 14 sierpnia 2016

sqrt and random

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#define max 20
#define m_first 20
#define value 5
using namespace std;

void init_random()
{
    srand(time(NULL));
}
struct Point
{
    double x;
    double y;
    double z;
    double v;
   
};
double unresult_a(double x,double y)
{
    return (1./x)/(1./y);
}
double unresult_b(double x, double y)
{
   
    return (x/unresult_a(x,y));
}
double unresult_c(double x, double y)
{
    return (x/unresult_b(x ,y));
}
double unresult_d(double x, double y)
{
    return (x/unresult_c(x,y));
}
double over_zero(double x, double y)
{
    if(x<y)
     return (y-x);
    else
     return (x-y);
}
bool is_first(int x)
{
    int i,sum;
    sum=0;
    for(i=1;i<x+1;i++)
     if(x%i==0)
      ++sum;
    if(sum==2)
     return true;
    else
     return false;
}
int main(int argc, char **argv)
{
    double *sqrt_table=new double[m_first];
    double *sum_pointer=new double[value];
    double *random_sum_pointer=new double[value];
    double *my_result=new double[value];
    struct Point *Pointer=new struct Point[max];
    struct Point *random_Pointer=new struct Point[max];
   
    init_random();
    int i,j;
    i=0;
    j=1;
    do
    {
        if(is_first(j))
        {
            sqrt_table[i]=sqrt(1.0*j);
            ++i;
        }
        ++j;
    }while(i<m_first);
    for(i=0;i<value;i++)
    {
        sum_pointer[i]=0.0;
        random_sum_pointer[i]=0.0;
        my_result[i]=0.0;
    }
    for(i=0;i<max;i++)
    {
        Pointer[i].x=unresult_a(sqrt_table[i],sqrt_table[max-(i+1)]);
        sum_pointer[0]+=Pointer[i].x;
        random_Pointer[i].x=unresult_a(sqrt_table[rand()%max],sqrt_table[rand()%max]);
        random_sum_pointer[0]+=random_Pointer[i].x;
        my_result[0]+=over_zero(sum_pointer[0],random_sum_pointer[0]);
        Pointer[i].y=unresult_b(sqrt_table[i],sqrt_table[max-(i+1)]);
        sum_pointer[1]+=Pointer[i].y;
        random_Pointer[i].y=unresult_b(sqrt_table[rand()%max],sqrt_table[rand()%max]);
        random_sum_pointer[1]+=random_Pointer[i].y;
        my_result[1]+=over_zero(sum_pointer[1],random_sum_pointer[1]);
        Pointer[i].z=unresult_c(sqrt_table[i],sqrt_table[max-(i+1)]);
        sum_pointer[2]+=Pointer[i].z;
        random_Pointer[i].z=unresult_c(sqrt_table[rand()%max],sqrt_table[rand()%max]);
        random_sum_pointer[2]+=random_Pointer[i].z;
        my_result[2]+=over_zero(sum_pointer[2],random_sum_pointer[2]);
        Pointer[i].v=unresult_d(sqrt_table[i],sqrt_table[max-(i+1)]);
        sum_pointer[3]+=Pointer[i].v;
        random_Pointer[i].v=unresult_d(sqrt_table[rand()%max],sqrt_table[rand()%max]);
        random_sum_pointer[3]+=random_Pointer[i].v;
        my_result[3]+=over_zero(sum_pointer[3],random_sum_pointer[3]);
        sum_pointer[4]+=((sum_pointer[0]+sum_pointer[1]+sum_pointer[2]+sum_pointer[3])/4);
        random_sum_pointer[4]+=((random_sum_pointer[0]+random_sum_pointer[1]+random_sum_pointer[2]+
        random_sum_pointer[3])/4.);
        my_result[4]+=over_zero(sum_pointer[4],random_sum_pointer[4]);
       
    }
    for(i=0;i<value;i++)
    {
        cout<<sum_pointer[i]<<"   "<<random_sum_pointer[i]<<"   "<<my_result[i]<<endl;
    }
    delete random_Pointer;
    delete Pointer;
    delete random_sum_pointer;
    delete my_result;
    delete sum_pointer;
    delete sqrt_table;
   
    return 0;
}

Brak komentarzy:

Prześlij komentarz