Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
There's are 2 positive integers x and y we have to generate a pattern that looks like,
2,3,3,2,3,3,3,2,3,3,3,2,3,3,2,3,3,3,2,3,3,3,2,3,3,3,2,3,3,2....
If we look at the count of 3s inbetween 2s we can see the same pattern is created,
2 | 3 3 | 2 | 3 3 3 | 2 | 3 3 3 | 2 ......

What I have tried:

C++
#include <iostream.h>
#include <conio.h>

void main() {
  clrscr();
  int x = 5, y = 6;
  int u = x, l = 0, k = 0, j = x;

  while(k < 21) {
    cout<<x<<",";
    if(u == x && l > 0) {
      u = y;
      l = 0;
    }

    if(u == y && l == j) {
      if(j == x) {
	j = y;
      } else {
	j = x;
      }

      u = x;
      l = 0;
    }

    for(int i=0; i<u; i++) {
      cout<<y<<",";
    }
    l++;
    k++;

    cout<<endl;
  }

  getch();
}


It's working but I don't think its the optimal way to do it.
Posted
Updated 16-Nov-20 22:17pm
v2
Comments
[no name] 16-Nov-20 16:48pm    
I agree: lousy variable names; no comments; etc.
Rick York 16-Nov-20 20:03pm    
That doesn't look like "the same pattern" to me. Where is there a sameness in the pattern you posted? If it was 2,3,3,2,3,3,2,3,3,2 I can see a pattern but I do not see one in the sequence you posted.

1 solution

You just need two loops. The outer one should print the "2,3,3,2". The inner one needs to count from 2 up which gives the number of groups of "3,3,3" to display after the outer set.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900