1) Using c++, write a void function named insertAtFront, that takes two paramete
ID: 3676406 • Letter: 1
Question
1) Using c++, write a void function named insertAtFront, that takes two parameters: a vector of integers and an integer value, then insert the integer value from the second parameter into the vector and this inserted value becomes the first item in the vector. For example, after the following statements in the main:
then values[0] is 62, values[1] is 24, values[2] is 3, values[3] is 78, values[4] is -5 and values[5] is 57.
The function uses the following header:
2) A run is a sequence of adjacent repeated values. Write a function run_length() that counts the length of the longest run in a sequence stored in a vector. If there is no adjacent repeated values in a sequence, then your function should return 1. (A single number is a run with length of 1) For example, the longest run in the vector with elements:
1255412432222365566631
has length 4 (for the adjacent repeated values of 2 in this example). The function uses the following header:
Explanation / Answer
Please find the answers below :
1)
void insertAtFront(vector<int>& vecA, int item){
vecA.insert (vecA.begin(),item);
}
2)
int run_length(vector<int> nums){
int prev=nums[0],cur,count=1,max=1;
for(vector<int>::size_type i = 1; i != nums.size(); i++) {
if(num[i]==prev){
count++;
}else{
prev = num[i];
if(count>max){
max = count;
}
count=1;
}
}
return max;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.