-1

I have a C++ problem:

Input an sequence of digit [ 0 - 9 ] and terminated by three 9 consecutivly, print on standard output the number of subsequences consisting of three consecutive equal digits on standard output.

Example: Given the sequence { 1 2 2 2 2 0 0 3 3 3 7 9 9 9 }, the subsequence are identified:
{ 2 2 2}, { 2 2 2 }, { 3 3 3 } .
Therefore, the program should print on standard output the number 3, equal to sequences present.

I try to use an array. My code ended up like this:

int main(){
    int i;
    int N = 0, A[100];
    
    
    while( (A[i] && A[i+1] && A[i+2]) != 9 ){
        N++;
    
        for( i = 0; i <= N; i++ ){
            cout << "A[" << i + 1 << "]:";
            cin >> A[i];
        }
        for(int i = 0; i <= N; i++ ){
            cout << "A[" << i + 1 << "]:" << A[i];
        }   
    }
}

My problem is, I have no idea how to terminate the sequence by three 9's consecutively. So I try to use an array. I hope someone can help me to elaborate the idea.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
aprilio
  • 11
  • 1
  • 5
  • 1
    You can do this assignment without storing the input into an array. And since the job is to detect repeated numbers anyway, it should be trivial to stop when you detect repeated nines. – Botje Jun 21 '21 at 19:54
  • 1
    Alternatively, use `std::vector` and its automatic resizing properties. – Botje Jun 21 '21 at 19:55
  • 1
    The question title seems to have nothing in common with the question content. This makes the question less useful for future askers. If you were looking for help with detecting subsequences would you bother reading a question about user-created arrays? – user4581301 Jun 21 '21 at 20:03

2 Answers2

1

You can do that by breaking the loop when three consecutive 9 is found.

#include <iostream>

const int ARRAY_SIZE = 100;

int main(){
    int i;
    int N = ARRAY_SIZE, A[ARRAY_SIZE];

    for( i = 0; i < ARRAY_SIZE; i++ ){
        std::cout << "A[" << i + 1 << "]:";
        std::cin >> A[i];
        // stop when three consecutive 9 is found
        if (i >= 2 && A[i - 2] == 9 && A[i - 1] == 9 && A[i] == 9){
            N = i + 1;
            break;
        }
    }

    for(int i = 0; i < N; i++ ){
        std::cout << "A[" << i + 1 << "]:" << A[i] << '\n';
    }

    return 0;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Thanks, the code is work! But, to count the subsequence, i put if( A[i] == A[i+1] && A[i+1] == A[i+2] ){ count++; cin >> A[i]; } after break; } but didnt work – aprilio Jun 21 '21 at 20:12
  • `A[i+1]` and `A[i+2]` are not initialized at that point, so you mustn't use their values. – MikeCAT Jun 21 '21 at 20:14
  • Yeah u right, i just realized that. so I change to if( i >= 2 && A[i - 2] == A[i - 1] && A[i - 1] == A[i] ){ count++; } and it works! – aprilio Jun 21 '21 at 20:18
0

Instead of an array with the number of elements equal to the magic number 100 what you need is an array of exactly three elements.

Here is a demonstrative program.

#include <iostream>

int main() 
{
    const size_t N = 3;
    int a[N];
    
    size_t count = 0;
    
    for ( size_t i = 0, j = 0; std::cin >> a[j++];  )
    {
        j %= N;
        
        if ( i != N - 1 )
        {
            ++i;
        }           
        else
        {
            size_t k = 1;
            while ( k < N && a[k] == a[k-1] ) k++;
            
            if ( k == N )
            {
                if ( a[0] == 9 ) break;
                else ++count;
            }
        }
    }
    
    std::cout << "count = " << count << '\n';
    
    return 0;
}

If to enter the sequence of numbers

1 2 2 2 2 0 0 3 3 3 7 9 9 9

then the program output will be

count = 3

instead of the inner while loop you could use for example the algorithm std::all_of.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335