Question: Edit the code so that it fufils the requirements. Create a C program file named longest _ streak.c . In that file, write a function

Edit the code so that it fufils the requirements.
Create a C program file named longest_streak.c. In that file, write a function with the following signature: unsigned longest_streak(int[], unsigned)
The function takes an int array and its length as arguments and returns the longest streak. By longest streak, we mean the largest number of times the same value appears consecutively in the array. For example, if int test1[]={1,2,3,2,4,4,4,2,1,1}, then longest_streak(test1,10) should return 3 because the longest streak consists of 3 consecutive values (the value 4 appears 3 consecutive times). If the length is 0, the function should return 0.
Hint: you can create a main() method inside of longest_streak.c or in a separate .c file for testing the longest_streak() function while you develop it.
Create a header file named longest_streak.h so that other code that wants to use your function can #include "longest_streak.h". Include a header guard. (Note: a header guard is not strictly necessary in this case since you only have declarations, not definitions, but it is good practice.)
(Comment-out the main method from longest_streak.c if it exists and ensure there are no print statements in the longest_streak() function.
longest_streak.h:
#ifndef LONGEST_STREAK_H
#define LONGEST_STREAK_H
unsigned longest_streak(int arr[], unsigned length);
#endif // LONGEST_STREAK_H
longest_streak.c:
#include "longest_streak.h"
unsigned longest_streak(int arr[], unsigned length){
if (length ==0) return 0;
unsigned max_streak =1;
unsigned current_streak =1;
for (unsigned i =1; i length; i++){
if (arr[i]== arr[i -1]){
current_streak++;
} else {
if (current_streak > max_streak){
max_streak = current_streak;
}
current_streak =1;
}
}
if (current_streak > max_streak){
max_streak = current_streak;
}
return max_streak;
}
int main(){
int test1[]={1,2,3,2,4,4,4,2,1,1};
unsigned length = sizeof(test1)/ sizeof(test1[0]);
unsigned result = longest_streak(test1, length);
printf("Longest streak: %u
", result); // Expected output: 3
return 0;
}
Edit the code so that it fufils the requirements.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!