Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

9 . 1 4 LAB: Flipping for tails Given main ( ) and GVCoin struct, complete function FlipForTails ( ) in main.c that counts and

9.14 LAB: Flipping for tails
Given main() and GVCoin struct, complete function FlipForTails() in main.c that counts and returns the number of flips taken to achieve a desired number of tails. Function FlipForTails() has a GVCoin struct and an integer representing the desired number of tails as parameters. Review the GVCoin struct in "GVCoin.h" and the function implementations in "GVCoin.c" by clicking on the orange arrow.
Note: For testing purposes, a GVCoin struct is created in the main() function using a pseudo-random number generator with a fixed seed value. The program uses a seed value of 15 during development, but when submitted, a different seed value will be used for each test case.
Ex: If the GVCoin struct is created with a seed value of 15 and the desired number of tails is 100, then the function FlipForTails() returns 213 and the program outputs:
Total number of flips for 100 tails: 213
#ifndef GVCOIN_H_
#define GVCOIN_H_
#include
#include
typedef struct GVCoin_struct {
bool isHeads;
int flips;
int heads;
} GVCoin;
GVCoin InitGVCoin(int seed);
GVCoin Flip(GVCoin coin);
int NumFlips(GVCoin coin);
int NumHeads(GVCoin coin);
int NumTails(GVCoin coin);
bool IsHeads(GVCoin coin);
#endif
#include
#include
#include
#include "GVCoin.h"
GVCoin InitGVCoin(int seed){
srand(seed);
GVCoin coin;
coin.heads =0;
coin.flips =0;
coin.isHeads =1;
return coin;
}
GVCoin Flip(GVCoin coin){
coin.isHeads = rand()%2;
coin.flips++;
if (coin.isHeads){
coin.heads++;
}
return coin;
}
int NumFlips(GVCoin coin){
return coin.flips;
}
int NumHeads(GVCoin coin){
return coin.heads;
}
int NumTails(GVCoin coin){
return coin.flips - coin.heads;
}
bool IsHeads(GVCoin coin){
return coin.isHeads;
}
#include
#include "GVCoin.h"
#include "GVCoin.h"
int FlipForTails(GVCoin coin, int goal){
int num_flips =0;
int num_tails =0;
while (num_tails < goal){
Flip(coin);
num_flips++;
if (IsHeads(coin)){
num_tails++;
}
}
return num_flips;
}
int main(){
GVCoin coin = InitGVCoin(15); // Create a GVcoin object with seed value 15
int numTails =100; // Desire 100 tails
int total = FlipForTails(coin, numTails); // Should return 213 using GVcoin object with seed value 15
printf("Total number of flips for 100 tails: %d
", total);
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database And SQL

Authors: Lucy Scott

3rd Edition

1087899699, 978-1087899695

More Books

Students also viewed these Databases questions