Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Write a code for this

Write a generic & parameterized function that is able to generate the following (infinite) pattern:
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,



This is not a random pattern
hint to understand how the code has been created: if you count the number of 3s sandwitched between 2s
you will notice that it creates the same pattern
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 32         3            3         2          3           3           3


What I have tried:

I have tried but its not working
Posted
Updated 11-Aug-21 3:10am
Comments
jeron1 6-Aug-21 11:57am    
What have you tried?
Patrice T 6-Aug-21 12:15pm    
Show your try and explain how it is not working.

The is not a complex algorithm. Your function should accept two numbers (N1 and N2). The sequence then is something like:
print N1.
print N2 for N1 times.
print N1.
print N2 for N2 times.
repeat until the end of time.
 
Share this answer
 
Comments
Johnson Rockstar 6-Aug-21 12:07pm    
Hey Richard thanks for your answer I just need a complete optimized code for this please help me with that.
Richard MacCutchan 6-Aug-21 12:10pm    
Er, what is incomplete, or not optimised in my suggestion above?
Johnson Rockstar 6-Aug-21 12:13pm    
As the output of the question should be in this manner

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,

Thanks btw.
Richard MacCutchan 6-Aug-21 12:15pm    
So you just need to add ", " after each digit. What is the difficulty?
Stefan_Lang 11-Aug-21 8:28am    
The difficulty is that the pattern is defined recursively. Sometimes it's 2 and sometimes 3 '3's in a row in between the '2's. The repetitions are defined by the previous iteration of the recursion. Unfortunately, while the iteration is hinted at, the initial value is missing. But I guess it's simply '2'. That would mean the second iteration generates 2332, and the third generates 233233323332332. (insert punctuation as needed)
Quote:
Write a code for this

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Comments
Johnson Rockstar 6-Aug-21 12:17pm    
Hey there, here is the solution but I need in c or c++ solution


public class Pattern {
static void printTerm (String term, int noOfTimes)
{
for (int index=1; index <=noOfTimes; index++)
System.out.print (term);
}
public static void main (String a [])
{
String term1="2,3,3";
String term2="2,3,3,3";
int index=1;
While (true)
{
printTerm (term1, 1);
printTerm (term2, ++index);
System.out.println ();
}

}
}
Dave Kreskowiak 6-Aug-21 12:47pm    
No, YOU didn't solve it. You lifted a solution from https://javaconceptoftheday.com/number-pattern-programs-in-java/

Now it's going to be YOUR job to convert the code to C/C++.

Nobody is going to do your work for you.
Johnson Rockstar 6-Aug-21 12:59pm    
Why would I ask here if I know to solve it properly brother
OriginalGriff 6-Aug-21 13:26pm    
But ... but ... copy'n'paste is doign it yourself ... :sigh:
Dave Kreskowiak 6-Aug-21 13:27pm    
I found this amusing:

https://www.codeproject.com/Questions/5309834/Can-someone-suggest-a-video-tutorial-for-the-probl
The hint offers clear instructions how to iteratively generate longer and longer sequences matching the desired pattern. This means your main function needs to:
- declare a container to hold the sequence
- initialize it to hold the minimal sequence: '2' (the problem description doesn't actually state that, but just '2' will work!)
- write a loop to repeatedly replace your pattern with a new one
- print out the current pattern after each sequence

Start with this code and implement the functions:
C++
#include <vector>
#include <iostream>
std::vector<int> nextPattern(const std::vector<int>& pattern) {
    std::vector<int> next_pattern;

    // generate the next pattern here

    return next_pattern;
}
void printvec(std::vector<int> vec) {
    // write code to print your sequence
}
int main() {
    std::vector<int> pattern {2};
    for (int i = 0; i < 4; ++i) {
        pattern = nextPattern(pattern);
        printvec(pattern);
    }
}

If you do it right you should get (the last line is _very_ long!):
2,3,3,2,
2,3,3,2,3,3,3,2,3,3,3,2,3,3,2,
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,3,3,3,2,3,3,3,2,3,3,3,2,3,3,2,3,3,3,2,3,3,3,2,3,3,2,
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,3,3,3,2,3,3,3,2,3,3,3,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,3,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,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,3,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,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,3,3,3,2,3,3,3,2,3,3,3,2,3,3,2,3,3,3,2,3,3,3,2,3,3,2
 
Share this answer
 

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

  Print Answers RSS


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