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

Write a function (that doesn\'t use math.h or string.h) int lenOfRun(int a[], in

ID: 3621624 • Letter: W

Question

Write a function (that doesn't use math.h or string.h)
int lenOfRun(int a[], int n);
that replaces the numbers in the array a of length n by the lengths of its runs, namely the numbers of identical integers in consecutive locations. lenOfRun returns the new size of the array. For example, if the input array is a=[1,1,1,2,2,3,4], then lenOfRun(a, 7) changes the values in a to a=[3,2,1,1], and returns 4.
main asks the user to input integers and store them into an array (the input stops when scanf fails to read an integer). It then repeatedly calls lenOfRun until the array has size 1. Output the arrays for all intermediate steps. You may assume that the array initially has at most 100 numbers.
Sample runs follow.
(~)$ a.out
Array of integers: 1 1 1 2 2 3 4 x
Next: 3 2 1 1
Next: 1 1 2
Next: 2 1
Next: 1 1
Next: 2
(~)$ a.out
Array of integers: 0 0 1 1 1 0 1 0 1 0 6 6 9 9 !
Next: 2 3 1 1 1 1 1 2 2
Next: 1 1 5 2
Next: 2 1 1
Next: 1 2
Next: 1 1
Next: 2
(~)$ a.out
Array of integers: 5 @

Explanation / Answer

#include <iostream>

 

using namespace std;

 

int lenOfRun(int *a, int n) {

int runner = 0;

int counter = 0;

for(int i=0; i<n; i=i) {

int tmp = a[i];

while(tmp==a[i]) {

i++;

counter++;

}

a[runner] = counter;

runner++;

counter = 0;

}

return runner;

}

 

 

int main() {

int a[] = {1, 1, 1, 1, 2, 3, 4, 5, 5, 5, 6, 7, 8, 0};

int len = 14;

while((len = lenOfRun(a, len)) >= 1) {

for(int i=0; i<len; i++) {

cout << a[i] << endl;

}

if(len ==1) break;

cout << "New Array" << endl;

}

}

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