How to make this program generate a random 20000 array\'s elements with values b
ID: 3804192 • Letter: H
Question
How to make this program generate a random 20000 array's elements with values between 100 and -100#include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> #include <pthread.h>
#define NP 5 #define NITER 200000000 #define LEN (NITER/NP)
double real_pi=3.14159265358979323846264338327950288419716939937510;
// approximate pi using pi=(4/n)*Sum[i=0..n-1](1/(1+((2*(i+1)-1)/(2*n))**2)) // this can be used to sum just part of the series double pisum(int start,int end) { int i; double h,x,sum=0;
h=1.0/(double)(2*NITER); for(i=start;i<end;i++) { x=h*(double)(2*(i+1)-1); sum+=1.0/(1.0+x*x); } return 4.0*sum/(double)NITER; }
double res[NP];
// thread to calculate partial sum void *pithread(void *p) { int i=(int)(long long)p; res[i]=pisum(i*LEN,(i+1)*LEN); printf("The partial sum by threads : %lf " , res[i]); }
int main() { clock_t start_time, end_time; double pi=0; int i;
// get current time start_time=clock();
pthread_t threads[NP]; // create NP threads for(i=0;i<NP;i++) { printf("The Thread running is: %d " , i); pthread_create(&threads[i],0,pithread,(void *)(long long)i); end_time = clock(); printf("Time taken to complete by thread %d is %ld ms ", i, (end_time-start_time));
}
pi=0; for(i=0;i<NP;i++) { // wait for thread to finish pthread_join(threads[i],0); // add partial sum pi=pi+res[i]; } printf("The calculated value of pi using sum series is : %.20f error rate is =%g ",pi,fabs(pi-real_pi));
} How to make this program generate a random 20000 array's elements with values between 100 and -100
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> #include <pthread.h>
#define NP 5 #define NITER 200000000 #define LEN (NITER/NP)
double real_pi=3.14159265358979323846264338327950288419716939937510;
// approximate pi using pi=(4/n)*Sum[i=0..n-1](1/(1+((2*(i+1)-1)/(2*n))**2)) // this can be used to sum just part of the series double pisum(int start,int end) { int i; double h,x,sum=0;
h=1.0/(double)(2*NITER); for(i=start;i<end;i++) { x=h*(double)(2*(i+1)-1); sum+=1.0/(1.0+x*x); } return 4.0*sum/(double)NITER; }
double res[NP];
// thread to calculate partial sum void *pithread(void *p) { int i=(int)(long long)p; res[i]=pisum(i*LEN,(i+1)*LEN); printf("The partial sum by threads : %lf " , res[i]); }
int main() { clock_t start_time, end_time; double pi=0; int i;
// get current time start_time=clock();
pthread_t threads[NP]; // create NP threads for(i=0;i<NP;i++) { printf("The Thread running is: %d " , i); pthread_create(&threads[i],0,pithread,(void *)(long long)i); end_time = clock(); printf("Time taken to complete by thread %d is %ld ms ", i, (end_time-start_time));
}
pi=0; for(i=0;i<NP;i++) { // wait for thread to finish pthread_join(threads[i],0); // add partial sum pi=pi+res[i]; } printf("The calculated value of pi using sum series is : %.20f error rate is =%g ",pi,fabs(pi-real_pi));
} How to make this program generate a random 20000 array's elements with values between 100 and -100
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> #include <pthread.h>
#define NP 5 #define NITER 200000000 #define LEN (NITER/NP)
double real_pi=3.14159265358979323846264338327950288419716939937510;
// approximate pi using pi=(4/n)*Sum[i=0..n-1](1/(1+((2*(i+1)-1)/(2*n))**2)) // this can be used to sum just part of the series double pisum(int start,int end) { int i; double h,x,sum=0;
h=1.0/(double)(2*NITER); for(i=start;i<end;i++) { x=h*(double)(2*(i+1)-1); sum+=1.0/(1.0+x*x); } return 4.0*sum/(double)NITER; }
double res[NP];
// thread to calculate partial sum void *pithread(void *p) { int i=(int)(long long)p; res[i]=pisum(i*LEN,(i+1)*LEN); printf("The partial sum by threads : %lf " , res[i]); }
int main() { clock_t start_time, end_time; double pi=0; int i;
// get current time start_time=clock();
pthread_t threads[NP]; // create NP threads for(i=0;i<NP;i++) { printf("The Thread running is: %d " , i); pthread_create(&threads[i],0,pithread,(void *)(long long)i); end_time = clock(); printf("Time taken to complete by thread %d is %ld ms ", i, (end_time-start_time));
}
pi=0; for(i=0;i<NP;i++) { // wait for thread to finish pthread_join(threads[i],0); // add partial sum pi=pi+res[i]; } printf("The calculated value of pi using sum series is : %.20f error rate is =%g ",pi,fabs(pi-real_pi));
}
Explanation / Answer
THIS LOOP SHOULD BE INCLUDED....to generate random elements between -100 and 100
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.