Need help on finding the longest run??

I am writing a program for an assignment, which is: Write a program that creates an int array of 20 elements, would ask the user how many tosses are required(1-20), fills the array with the result of tossing a number cube as many times as requested by the user, prints the values of the tosses horizontally then displays the longest run found in the sequence of tosses. For the above example the output would be if the user requested 18 tosses:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

1 5 5 4 3 1 2 2 2 2 6 1 3 3 5 5 5 5
The longest run occurs at index 14.
I wrote the program for the first part, but I am stuck on trying to find the index of the longest run. This is my code so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  #include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

const int SIZE = 20;

int main()
{
    int arr[SIZE];
    int numToss;

        cout << "How many tosses are required? Enter a value from 1 to 20: " << endl;
        cin >> numToss;
            while (numToss > SIZE || numToss < 1)
            {
                cout << "Please enter a value for the number of tosses greater than 1 and less than 20: " << endl;
                cin >> numToss;
            }
    srand(time(0));
        for (int i = 0; i < numToss; i++)
        {
            arr[i] = 1 + rand() % 6;
            cout << " " << arr[i];
        }

Any help on how to do the second part? Thanks for your help!
using std::adjacent_find:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

int main()
{
    std::vector<int> myVec {1, 5, 5, 4, 3, 1, 2, 2, 2, 2, 6, 1, 3, 3, 5, 5, 5, 5 };
    size_t run{};//to keep the run score

    auto holding_itr = myVec.begin();//to keep the position score
    auto running_itr = std::adjacent_find(myVec.begin(), myVec.end());
    //http://en.cppreference.com/w/cpp/algorithm/adjacent_find
    if(running_itr != myVec.end())
    {
       while (running_itr != myVec.end())
       {
            auto leading_itr = running_itr + 1;//for checking the next element
            size_t tempRun{1};
            while (leading_itr != myVec.end() && (*running_itr == *leading_itr))
            {
                if(*running_itr == *leading_itr)
                {
                    ++tempRun;
                    ++leading_itr;
                }
             }
            if (tempRun >= run )//if new max run found update as below
            {
                run = tempRun;
                holding_itr = running_itr;
            }

            running_itr = std::adjacent_find(leading_itr, myVec.end());//now find the next run ...
        }
    }

    std::cout << std::distance(myVec.begin(), holding_itr);
}

btw, there is also a 4-streak run in your example starting at index 6
What do you mean when you say there is a 4-streak run? I thought it was correct.
I should've mentioned that I can't use vectors or algorithms. I came up with the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

const int SIZE = 20;

int main()
{
    int arr[SIZE];
    int numToss;
    srand(time(0));

        cout << "How many tosses are required? Enter a value from 1 to 20: " << endl;
        cin >> numToss;
            while (numToss > SIZE || numToss < 1)
            {
                cout << "Please enter a value for the number of tosses greater than 1 and less than 20: " << endl;
                cin >> numToss;
            }

        for (int i = 0; i < numToss; i++)
        {
            arr[i] = 1 + rand() % 6;
            cout << " " << arr[i];
        }
    cout << endl;
  int max_count = 0;
int length = arr[0];
        for (int i = 0; i < length; i++)
        {
            int count = 1;
            for (int j = i + 1; j < length; j++)
            {
                if (arr[i] == arr[j])
                count++;
            }
            if (count > max_count)
                max_count = count;
        }
        int result = 0;
        for (int i = 0; i < length; i++)
        {
            int count=1;
            for (int j = i + 1; j < length; j++)
            {
                if (arr[i] == arr[j])
                count++;
            }
            if (count == max_count)
              {
                max_count = arr[i];
                result = i;
                cout << "The longest run occurs at " << result << endl;
              }
        }

    return 0;
}


However, this doesnt print the right index. Can anyone point out what is wrong with it? Ive tried everything and am running out of ideas, please help!!!
you have a run of 4 2's and a run of 4 5's - check your vector
edit: OK, you can disregard this as it's not in your actual program, just the illustrative example you provided has this structure
Last edited on
You can check if the number at the array is equal to max. If it is, print out index.
Last edited on
What do you mean? I am confused.
The code that I have now runs, but only correctly outputs sometimes and other times it does not. How can I find the index?
Also I was wondering if there is another way to write the code, if there is what would it look like?
Please help, this is due tomorrow and I've spent hours trying to figure it out but I cant seem to.
Thank you so much!
Also I was wondering if there is another way to write the code

well there is one using vectors, algorithms but you seem to have some constraints, perhaps its your prof
So then how can I improve my code so that it prints correctly? I seriously don't know what I am doing wrong.
There are many ways to write code.

The way I would approach this is having two variables. One to keep track of the max number and one to keep track of the max index.

Go through the array, if the array[index] > max, then the new max would the number at array[index]. As you swap max, you should also keep track of the maxIndex, so that you can later display where max was located.

Hope that clarifies what I was saying.

How is your code suppose to handle duplicates?
Last edited on
My professor didn't clarify that, and I don't know how to handle those either.

But would finding the max number give the most frequent value that occurred in the array? I am still a little confused as to how it would work.
Have a look at this, and then try to write the program on your own (start with an empty file):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
    std::srand( std::time(nullptr) ) ;

    const int SIZE = 20;
    const int NUM_FACES = 6 ;
    int arr[SIZE]{} ;

    int numToss;
    std::cout << "How many tosses are required? Enter a value from 1 to " << SIZE << ": " ;
    std::cin >> numToss;
    if( numToss > SIZE ) numToss = SIZE ;

    int longest_run_start = 0 ;
    int longest_run_length = 1 ;

    int current_run_start = 0 ; // the first run starts at position 0
    int current_run_length = 1 ; // with an initial length of 1

    // place the first random value in the array and print it out
    arr[0] = std::rand() % NUM_FACES ;
    std::cout << arr[0] << ' ' ;

    for( int pos = 1 ; pos < numToss ; ++pos ) // for each value after the first value
    {
        // generate and print the value at position pos
        arr[pos] = std::rand() % NUM_FACES ;
        std::cout << arr[pos] << ' ' ;

        // if the current value is the same as the previous value
        if( arr[pos] == arr[pos-1] ) ++current_run_length ;

        else // a new run is about to start
        {
            // check if the run that has just ended is the longest so far
            if( current_run_length > longest_run_length )
            {
                longest_run_start = current_run_start ;
                longest_run_length = current_run_length ;
            }

            current_run_start = pos ; // the new run starts at position pos
            current_run_length = 1 ; // with an initial length of 1
        }
    }

    // special-case for the last run being the longest run
    if( current_run_length > longest_run_length )
    {
        longest_run_start = current_run_start ;
        longest_run_length = current_run_length ;
    }

    std::cout << "\nlongest run of length " << longest_run_length
              << " starts at position " << longest_run_start
              << " value: " << arr[longest_run_start] << '\n' ;
}


Once you have got it working, try to write the same program, this time without using an array.
Hint: at any point in time, we need to look only at one previous value tht was generated.
Thank you for your help, this is becoming more clear now!
Topic archived. No new replies allowed.