Stuck in loop.

I`ve got some problems here with homework and i can`t figure it out.You must input n numbers with no more than 4 digits.Find how many even numbers contains the longest sequence of even numbers.
exemple: n = 8 x = 5 3 4 6 8 2 7 9 seq must be 4
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
 #include <iostream>

int sequ(int);

int main(int argc, char const *argv[]) {
  int n;
  std::cout << sequ(n) << '\n';
  return 0;
}

int sequ(int n){
  int x, seq, seq1; // seq = the sequence of even numbers // seq1 is the variable where i deposit the first seq
  std::cin >> n;// the x is the input number
  while(n) {
    std::cin >> x;
      if (x % 2 == 0){
        seq++;
        if (seq >=2) {
          seq1 = seq;
        }
      }
      if (x % 2 == 1)
        seq = 0;
        n--;
      }
    if(seq1 >= seq) return seq1;
    else return seq;
      }
Hello Justin2001,

Either initialize "n" on line 6 or add code to input a number from the user. As it is on line 7 you are sending a garbage value to your function.

Not sure yet if I understand the concept of the function, but it seems to work. I think.

Hope that helps,

Andy
Last edited on
Your code will be a lot easier to work with if you indent it to match the block structure.

I don't see anything in your code that looks at individual digits in x. It looks like you're just checking whether x itself is even.

Try creating a function int longestEvenSequence(int num) The eventual goal is to have it return the longest sequence of even digits in the number num. To start, just write code that extracts and prints the digits in num. Once that's working, modify the code to find the longest sequence of even digits. Once you have this working, use it in your seq() function to get the answer to the problem.
Topic archived. No new replies allowed.