Click here to Skip to main content
15,174,684 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 4: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.
   
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)
Richard MacCutchan 11-Aug-21 8:41am
   
True.
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[^]
   
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
Johnson Rockstar 6-Aug-21 13:30pm
   
I still can't understand why some are being rude, a simple suggestion of a tutorial would be fine
jeron1 6-Aug-21 13:40pm
   
Here's a tutorial site.

https://www.cplusplus.com/doc/tutorial/
OriginalGriff 7-Aug-21 1:25am
   
Nobody is being rude - except you by assuming that others should spend their valuable time doing your work!
You really won't learn anything from your current approach: even if someone did do it for you and hand over a finished solution you can hand in, that gets you out of this weeks problem only - and next week it will be harder because it assumes you learned how to do this weeks and can build on that.

Seriously: try and do it yourself. With a little effort you could have finished by now ...
OriginalGriff 6-Aug-21 12:30pm
   
What part of "we aren't going to do your homework for you" did you misunderstand? I can explain more clearly if you tell me which bit is giving you difficulties ...
Johnson Rockstar 6-Aug-21 13:06pm
   
Can you at least suggest a tutorial video for this?
OriginalGriff 6-Aug-21 15:03pm
   
You seriously expect someone to produce a "tutorial video" on how to solve a specific homework question that your teacher invented for this class alone?

You do realize that you have already spent more time and effort on trying to get others to do your homework for you that you would have expended by actually doing it yourself?

How do you expect to learn anything if all you do is try to copy'n'paste?
Oh, and one other thing ... You do realise that teachers do know about sites like this, and can usually spot plagiarism nearly as quickly as we can?
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
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



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