Task - Using pointers to process arrays (C Language) Example #5 below expected output is 45, but from the program below its coming out to 46. Please help make it come out to 45 as expected In a TV show, each minute can be either interesting or boring. Assume that if 7 consecutive minutes are boring, then an average viewer will stop watching the show. Write a C program that calculates how many minutes that an average viewer will watch a TV show, given the interesting minutes. Assume the TV shows are 45 minutes long. Requirements Name your program project4_minutes.c. Follow the format of the examples below. The program will read in the number of interesting minutes, then read in the interesting minutes. The program should include the following function. Do not modify the function prototype. int find_minute(int *minutes, int n); minutes represents the input array for interesting minutes, n is the length of the array (the number of interesting minutes). The function returns the how many minutes an average viewer will watch the show. This function should use pointer arithmetic– not subscripting – to visit array elements. In other words, eliminate the loop index variables and all use of the [] operator in the function. In the main function, call the find_minute function and display the result. Pointer arithmetic is NOT required in the main function. Examples (your program must follow this format precisely) Example #1 Enter the number of interesting minutes: 3 Enter the interesting minutes: 7 13 28 Output: 20 Note: In the first example, minutes 14, 15, 16, 17, 18, 19, 20 are all boring and thus an average viewer will stop watching after the 20th minute. So, the average viewer will watch for 20 minutes. Example #2 Enter the number of interesting minutes: 3 Enter the interesting minutes: 7 13 18 Output: 25 Note: In the second example, minutes 19, 20, 21, 22, 23, 24, 25 are all boring and thus an average viewer will stop watching after the 25th minute. So, the average viewer will watch for 25 minutes. Example #3 Enter the number of interesting minutes: 6 Enter the interesting minutes: 9 13 28 32 38 42 Output: 7 Note: In the third example, minutes 1, 2, 3, 4, 5, 6, 7 are all boring and thus an average viewer will stop watching after the 7th minute. So, the average viewer will watch for 7 minutes. Example #4 Enter the number of interesting minutes: 6 Enter the interesting minutes: 5 12 18 24 31 38 Output: 45  Note: In the fourth example, there are no consecutive 7 boring minutes So, the average viewer will watch for the whole 45 minutes.  Example #5 Enter the number of interesting minutes: 8 Enter the interesting minutes: 2 9 13 20 25 29 33 39 Output: 45  Program- #include //Function prototype int find_minute(int *minutes, int n); int main(){     int minutes[100];     int n;     //Ask user to enter the value     printf("Enter the number of interesting minutes: ");     scanf("%d", &n);     printf("Enter the interesting minutes: ");     //Read the interesting minutes     for(int i = 0; i < n; i++){         scanf("%d", &minutes[i]);     }     //Call the function     int result = find_minute(minutes, n);     //Print the result     printf("Output: %d\n", result);     return 0; } //Function definition int find_minute(int *minutes, int n){     //start of the tv show at 0     int currTime = 0;         //Iterating using pointer arithmetic    // minutes+i is equivalent to &minutes[i]     for(int i = 0; i < n; i++){         if(*(minutes+i) <= currTime+7){             currTime = *(minutes+i);         }         else{             break;         }     }     int result;     if(currTime < 45){         result = currTime + 7;     }     else{         //Show stops at 45 minutes, result should not be greater than 45         result = 45;     }     return result; }

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter8: Advanced Data Handling Concepts
Section: Chapter Questions
Problem 7PE
icon
Related questions
Question

Task - Using pointers to process arrays (C Language)

Example #5 below expected output is 45, but from the program below its coming out to 46. Please help make it come out to 45 as expected

In a TV show, each minute can be either interesting or boring. Assume that if 7 consecutive minutes are boring, then an average viewer will stop watching the show. Write a C program that calculates how many minutes that an average viewer will watch a TV show, given the interesting minutes. Assume the TV shows are 45 minutes long.

Requirements

  • Name your program project4_minutes.c.
  • Follow the format of the examples below.
  • The program will read in the number of interesting minutes, then read in the interesting minutes.
  • The program should include the following function. Do not modify the function prototype.

int find_minute(int *minutes, int n);

    • minutes represents the input array for interesting minutes, n is the length of the array (the number of interesting minutes). The function returns the how many minutes an average viewer will watch the show.
    • This function should use pointer arithmetic– not subscripting – to visit array elements. In other words, eliminate the loop index variables and all use of the [] operator in the function.
  • In the main function, call the find_minute function and display the result.
  • Pointer arithmetic is NOT required in the main function.

Examples (your program must follow this format precisely)

Example #1

Enter the number of interesting minutes: 3
Enter the interesting minutes: 7 13 28
Output: 20

Note: In the first example, minutes 14, 15, 16, 17, 18, 19, 20 are all boring and thus an average viewer will stop watching after the 20th minute. So, the average viewer will watch for 20 minutes.

Example #2

Enter the number of interesting minutes: 3
Enter the interesting minutes: 7 13 18
Output: 25

Note: In the second example, minutes 19, 20, 21, 22, 23, 24, 25 are all boring and thus an average viewer will stop watching after the 25th minute. So, the average viewer will watch for 25 minutes.

Example #3

Enter the number of interesting minutes: 6
Enter the interesting minutes: 9 13 28 32 38 42
Output: 7

Note: In the third example, minutes 1, 2, 3, 4, 5, 6, 7 are all boring and thus an average viewer will stop watching after the 7th minute. So, the average viewer will watch for 7 minutes.

Example #4

Enter the number of interesting minutes: 6
Enter the interesting minutes: 5 12 18 24 31 38
Output: 45 

Note: In the fourth example, there are no consecutive 7 boring minutes So, the average viewer will watch for the whole 45 minutes. 

Example #5

Enter the number of interesting minutes: 8
Enter the interesting minutes: 2 9 13 20 25 29 33 39
Output: 45 

Program-

#include <stdio.h>

//Function prototype
int find_minute(int *minutes, int n);

int main(){
    int minutes[100];
    int n;
    //Ask user to enter the value
    printf("Enter the number of interesting minutes: ");
    scanf("%d", &n);
    printf("Enter the interesting minutes: ");
    //Read the interesting minutes
    for(int i = 0; i < n; i++){
        scanf("%d", &minutes[i]);
    }
    //Call the function
    int result = find_minute(minutes, n);
    //Print the result
    printf("Output: %d\n", result);
    return 0;
}

//Function definition
int find_minute(int *minutes, int n){
    //start of the tv show at 0
    int currTime = 0;

    
   //Iterating using pointer arithmetic
   // minutes+i is equivalent to &minutes[i]
    for(int i = 0; i < n; i++){
        if(*(minutes+i) <= currTime+7){
            currTime = *(minutes+i);
        }
        else{
            break;
        }
    }
    int result;
    if(currTime < 45){
        result = currTime + 7;
    }
    else{
        //Show stops at 45 minutes, result should not be greater than 45
        result = 45;
    }
    return result;
}

Expert Solution
Step 1

Introduction

The given question is asking for a program that calculates the maximum number of minutes an average viewer will watch a TV show given the number of interesting minutes. The program assumes that if there are 7 consecutive minutes of boring content, then the average viewer will stop watching the show. The user is prompted to enter the number of interesting minutes, followed by the interesting minutes themselves.

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning