Create a function randpi that will continuously generate random digits until pro
ID: 3837828 • Letter: C
Question
Create a function randpi that will continuously generate random digits until producing a sequence which reproduces t 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 pi 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: 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>
#include <stdlib.h>
#include <time.h>
int main ()
{
int found;
int count;
/* initialize random seed: */
srand (time(NULL));
found = 0;
while (found == 3) {
count++;
num1 = rand() % 10;
if (num1 == 3){
found++
continue;
}
if (found == 1 ){
if (num == 1){
found++;
continue;
}
else
found = 0;
}
if (found == 2 ){
if (num == 4)
found == 3;
else
found = 0;
}
}
printf("%d", count - 3);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.