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

Write a program to determine the length of bursts of zero\'s in a list of N numb

ID: 3597207 • Letter: W

Question

Write a program to determine the length of bursts of zero's in a list of N numbers stored in an array. The program should record the length of the bursts of zero's in another array. The program should then print the lengths of the bursts. Using this second array with the lengths of the bursts, it should also compute and print the following: the average length of the bursts, the minimum burst length, the maximum burst length, and the total number of zeros (the sum of the burst lengths). Your program should: Prompt the user for the size of the list (N). Then it should prompt the user to enter N numbers and store them into an array called list. Note, you should specify the maximum size of N allowed (based on how large you declare your array). Given N and list, your program should compute the lengths of the bursts of zeros and store them in an array called Burstlengths. After computing and storing the burst lengths you should terminate your Burstlengths array with a sentinel (-1). Use a for loop in this step. Given the Burstlengths array with a sentinel (-1), use a while loop to print the list of burst lengths. Format this nicely in a table with headings. Given the Burstlengths array with a sentinel (-1), compute and then print the following information: 1. 2. 3. 4. a. b. c. d. Average burst length Minimum burst length Maximum burst length Total number of zeros

Explanation / Answer

Solution to given problem is mentioned below. Sample execution output is also provided for reference.

File: burst_lengths.c

#include <stdio.h>

int main()
{
int n;
int list[100];

int burst_lengths[100];
int burst_start = 0;
int burst_index = 0;

int total_num_zeros = 0;
int min_burst_length = 0;
int max_burst_length = 0;
int num_bursts = 0;

do
{
printf("Enter size of list (0 < n <= 100): ");
scanf("%d", &n);
} while (n < 0 || n > 100);

printf("Enter list of %d numbers: ", n);

for (int i = 0; i < n; i++)
{
scanf("%d", &list[i]);
}

for (int i = 0; i < n; i++)
{
if (list[i] == 0)
{
if (burst_start == 0)
{
// Identified first zero in new burst

burst_start = 1;
burst_lengths[burst_index] = 1;
}
else
{
// Found consequent zero in identified burst

burst_lengths[burst_index] += 1;
}
}
else
{
if (burst_start == 1)
{
// Identified end of burst

burst_start = 0;
burst_index++;
}
}
}

if (burst_start == 1)
{
// Identified end of last burst

burst_index++;
}

// Set sentinel
burst_lengths[burst_index] = -1;

if (burst_index > 0)
{
printf("Burst # Burst Length ");
printf("------- ------------ ");
}

burst_index = 0;

while (burst_lengths[burst_index] != -1)
{
if (burst_index == 0)
{
min_burst_length = burst_lengths[burst_index];
max_burst_length = burst_lengths[burst_index];
}
else
{
if (burst_lengths[burst_index] < min_burst_length)
{
min_burst_length = burst_lengths[burst_index];
}

if (burst_lengths[burst_index] > min_burst_length)
{
max_burst_length = burst_lengths[burst_index];
}
}

total_num_zeros += burst_lengths[burst_index];
num_bursts++;

printf("%d %d ", burst_index + 1, burst_lengths[burst_index]);
burst_index++;
}

if (num_bursts > 0)
{
printf("Average burst length: %.2f ", total_num_zeros * 1.0 / num_bursts);
printf("Minimum burst length: %d ", min_burst_length);
printf("Maximum burst length: %d ", max_burst_length);
printf("Total number of zeros: %d ", total_num_zeros);
}
else
{
printf("No zero bursts! ");
}
}

Sample Execution Output #1:

Enter size of list (0 < n <= 100): 10
Enter list of 10 numbers: 1 1 1 1 0 1 0 0 1 1
Burst # Burst Length
------- ------------
1 1
2 2
Average burst length: 1.50
Minimum burst length: 1
Maximum burst length: 2
Total number of zeros: 3

Sample Execution Output #2:

Enter size of list (0 < n <= 100): 10
Enter list of 10 numbers: 1 1 1 1 1 1 1 1 1 1
No zero bursts!

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