Select related compiler and library such as intel C and Fort
Select related compiler and library such as intel C and Fortran compiler as your default compiler and library in LONI machines. Write a sample code for calculating constant PI with an accuracy of 12 digit number. You can use random number function in C or Fortran or other methods, e.g. rand() in matlab. Late on we need this code for parallelization. Submit your code and result online.
Solution
// C code approximate pi
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <time.h>
int main()
{
time_t t;
srand((unsigned) time(&t));
int i;
int totalIteration=1000000;
double xcoordinate,ycoordinate;
int countpi=0;
double square;
double approx_pi;
countpi=0;
for ( i=0; i<totalIteration; i++)
{
xcoordinate = (double)rand()/RAND_MAX;
ycoordinate = (double)rand()/RAND_MAX;
square = xcoordinate*xcoordinate+ycoordinate*ycoordinate;
if (square<=1) countpi++;
}
approx_pi=(double)countpi/totalIteration*4;
printf(\"Approximate value of pi: %0.12lf \ \",approx_pi);
}
/*
output:
Approximate value of pi: 3.143728000000
*/
