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

Please help with the following coding question! Edit the program to add the func

ID: 3672091 • Letter: P

Question

Please help with the following coding question!

Edit the program to add the functionality of

– reporting the maximum number of heads and tails in a row.

– determining which is longer.

Edit the main() function to use a while loop and switch statements instead of a for loop and if else statements.

Source Codes in text:

#include <stdio.h>

#include <stdlib.h>

char coins [100];

int main(int argc, char** argv) { //main function

    int heads = 0; //counter for heads

    int tails = 0; //counter for tails

    int count;    //keep track of the rounds

    bool coinFlip; //coin value at each round

      

    for (count = 0; count < 100; count++) {

        coinFlip = rand() % 2; //get a random number and make it a 1 or 0;

        if (coinFlip) {

            coins[count] = 'H'; //Assign nth spot in array as 'H'

              tails++;

       }

        else {

            coins[count] = 'T'; //Assign nth spot in array as 'T'

              heads = heads + 1;

             

        printf("Value at entry %i: %c ", count+1, coins[count]);

//this is a print statement that prints each entry in the array

                                                                 //on a new line. We use formatting such as %i and %c to prevent

                                                                 //the use of a broken string with '+' in them. The %i means to format the

//the entry as an integer, %c is to be formatted as a char.

    }

   

    printf("There are %i heads and %i tails", heads, tails); //tell us how many of each coin face we got

    return (EXIT_SUCCESS);

}

In picture:

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
char coins [100];
int main(int argc, char** argv) { //main function

int heads = 0; //counter for heads
int tails = 0; //counter for tails
int count; //keep track of the rounds
bool coinFlip; //coin value at each round
  
count =0;
while(count <100){
  
coinFlip = rand() % 2; //get a random number and make it a 1 or 0;
if (coinFlip) {
coins[count] = 'H'; //Assign nth spot in array as 'H'
tails++;
}
else {
coins[count] = 'T'; //Assign nth spot in array as 'T'
heads = heads + 1;

printf("Value at entry %i: %c ", count+1, coins[count]);
//this is a print statement that prints each entry in the array
//on a new line. We use formatting such as %i and %c to prevent
//the use of a broken string with '+' in them. The %i means to format the   
//the entry as an integer, %c is to be formatted as a char.
}

printf("There are %i heads and %i tails", heads, tails); //tell us how many of each coin face we got
  
  

return (EXIT_SUCCESS);
count ++;
}
}