Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a function randpi that will continuously generate random digits until pro

ID: 3838669 • Letter: C

Question

Create a function randpi that will continuously generate random digits until producing a sequence which reproduces up to n significant figures, where n is the input argument to the function. The function should print the total number of random digits that were generated in the process.

For example, if the call to the function is

>>randpi(3)

digits are to be randomly generated until the sequence 3 1 4 is produced, which corresponds to with 3 significant figures, i.e., 3.14.

For sake of illustration, shown is a stream of randomly generated digits terminating at 3 1 4:

62942241613494624558375764618056730468499330976020373824054965418834695314

In this particular case, 74 digits were generated prior to reaching the desired sequence. The number 74 would then be output on the terminal.

Explanation / Answer

#include <stdio.h>
int randpi(int max_significant_digits); //declaring function randpi
int main()
{
int max_significant_digits;   
printf("enter max_significant_digits ");
scanf("%d",&max_significant_digits); //taking number of significant bits from user
randpi(max_significant_digits); //calling randpi function to find the random values upto that //significant number of bits
return 0;
}

int randpi(int max_significant_digits)
{
int arr[6]={3,1,4,1,5,9},n,i=0; //here considering approximate pi value as 3.14159 and storing these all digits into //an array
printf("random numbers are ");
while(i != max_significant_digits) //to stop producing random values after getting required number of significant //digits
{
n=rand()%10; //to produce one random value at a time
while(i<max_significant_digits)
{
printf("%d",n);
if(n==arr[i]) //checking for equality with pi value digits wise
{
i++;
n=rand()%10; //if equal then finding next random value and comparing with next array value
}
else
{
i=0;
break; //if not equal then again compare from first value of array
}
}
}
}

sample output:

sh4.2$ gcco main *.c sh4.2$ main

enter max_significant_digits 2 random numbers are 3675356291270936062618792023759228973612931

sh-4.2$ gcc -o main *.c

sh4.2$ main

enter max_significant_digits

3 random numbers are 36753562912709360626187920237592289736129319478450361063206155476569374525474430786884314

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote