C++ language write a function called countLongestOddRun which accepts an array o
ID: 3621348 • Letter: C
Question
C++ languagewrite a function called countLongestOddRun which accepts an array of integers, and the number of items in the array. The function will find the longest continuous run of odd numbers, and return the result to the calling function
Example: for the list
int arr[] = 11,15, 4, 19, 17, 21, 74, 5, 1, 27, 33, 47, 12;
numLongestOdds = countLongestOddRun(arr, 13);
numLongestOdds will hold the value 3, having found the longest continuous
run of odd numbers (i.e., 19, 17, 21) between two even numbers
The prototype for this function is:
int countLongestOddRun( int numArr[], int numItems );
Explanation / Answer
please rate - thanks
the longest odd run above is 5---5, 1, 27, 33, 47
#include <iostream>
using namespace std;
int countLongestOddRun( int numArr[], int numItems );
int main()
{int a[]={ 11,15, 4, 19, 17, 21, 74, 5, 1, 27, 33, 47, 12};
cout<<"The longest odd run is: "<< countLongestOddRun(a,13)<<endl;
system("pause");
return 0;
}
int countLongestOddRun(int numArr[], int numItems )
{int i,count=0,max=0;
for(i=0;i<numItems;i++)
{if(numArr[i]%2==0)
{if(count>max)
max=count;
count=0;
}
else
count++;
}
return max;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.