Below is the code i have, but the output i am getting is zero and its wrong, so
ID: 3607116 • Letter: B
Question
Below is the code i have, but the output i am getting is zero and its wrong, so i request you to help me with the above problem to write a c program and its output.
#include<stdio.h>
#include<stdlib.h>
int unitSphere( float x, float y, float z ) {
printf("volume of sphere is %f ",x*x + y*y + z*z);
return (x*x + y*y + z*z) <= 1.0;
}
int main(){
float x, y, z,volume = 0;
int loops = 1000,i;
for (i=0; i<loops; ++i) {
x = rand();
y = rand();
z = rand();
if ( unitSphere( x, y, z ) ){
volume = x*x + y*y + z*z;
}
}
printf("estimated volume of sphere is %f ",volume);
return 0;
}
//most of the cases it is not satis fying the condition (x*x + y*y + z*z) <= 1
Write the c program for the following 1. Using the Monte Carlo method, estimate the volume of the unit sphere,Explanation / Answer
int isInSphere( float x, float y, float z ) {
return (x*x + y*y + z*z) <= 1.0;
}
Then loop a number of times:
float x, y, z;
int loops = 100000;
int countInside = 0;
int i;
for (i=0; i<loops; ++i) {
x = rand();
y = rand();
z = rand();
if ( isInSphere( x, y, z ) ) countInside++;
}
The sample space is a cube of volume 1.0 (size x=1, y=1, z=1). So the fraction of points inside would be the fraction of 1.0 volume that the 1/8 of the sphere has:
float eighthVol = (float) countInside / (float) loops;
printf("Sphere estimated volume = %f", 8.0 * eighthVol );
I can't recall whether you're writing in C or Java (and you didn't specify) so this is sort-of in C. Should be similar looking in either language.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.