Ask your own question, for FREE!
Mathematics 41 Online
OpenStudy (shubhamsrg):

I am trying to generate this pattern on 'c' or 'c++' but I am unable to grasp the logic. Any advice? 1,11,21,1211,111221,312211,...... I just want to input 1, and then the rest of the numbers should automatically be generated. The pattern is simple, 1 --> one 1 hence next number is 11 11--> two 1 hence next number is 21 21 --> one 2 one 1 hence next number is 1211 and so on...

OpenStudy (shubhamsrg):

@ganeshie8

OpenStudy (anonymous):

I don't exactly follow the pattern.

OpenStudy (shubhamsrg):

1 is spoken as one 1 , so next number is 11 11 is spoken as two 1, so next number is 21 21 is spoken as one 2 one 1 , so next number is 1211 ans so on..

OpenStudy (anonymous):

#include <string> #include <sstream> std::string lookandsay(const std::string &s) { std::ostringstream r; for (unsigned int i = 0; i != s.length(); ) { unsigned int new_i = s.find_first_not_of(s[i], i+1); if (new_i == std::string::npos) new_i = s.length(); r << new_i - i << s[i]; i = new_i; } return r.str(); } #include <iostream> int main() { std::string laf = "1"; std::cout << laf << std::endl; for (int i = 0; i < 10; i++) { laf = lookandsay(laf); std::cout << laf << std::endl; } return 0; }

OpenStudy (anonymous):

happy to help :)

OpenStudy (shubhamsrg):

can you add comments in between and perhaps a bit more explanation/elaboration ! ?

OpenStudy (shubhamsrg):

bc :)

OpenStudy (anonymous):

ok

OpenStudy (anonymous):

@INeedHelpPlease? Isnt that from http://rosettacode.org/wiki/Look-and-say_sequence#C.2B.2B lol

OpenStudy (anonymous):

omg they copied my code in <1 hour

OpenStudy (shubhamsrg):

dafaq! -_-

OpenStudy (anonymous):

Wut. Are you sure they copied from you lol

OpenStudy (aravindg):

plagiarism?

OpenStudy (anonymous):

Maybe. He even said it was copied even though this is the default coding of C++ programming and he was unable to explain properly.

OpenStudy (anonymous):

Anyways heres some advice for improving programming. 1. Learn the assembly language properly (if you read it wrong this might make ALOT of mistakes) (if you use a bytecode compiled language (e.g. Java, C#, Python), learn its assembly language as well) and get your compiler to produce assembly output of your sources (or use a disassembler if your compiler doesn't have that option. For gcc, use the -S command-line flag). Read the assembly code and understand it properly. Experiment with different methods of achieving the same thing. Look at which one is fastest and the best one for you (usually the one which produces the least number of instructions, but not always). 2. Learn compiler theory. This helps with #1 :b 3. Learn a language that's completely different to anything else you've ever used. I recommend Haskell and Scheme. You can skip this step if you want to hehe 4. Read the standard/specification for your chosen language(s). Then you'll know all the caveats and nuances of the features of the language, which will help you learn what is faster and also help you write more maintainable code.

OpenStudy (anonymous):

Depending on the design problem they address, design patterns can be classified in different categories, of which the main categories are: Creational Patterns Structural Patterns Behavioral Patterns. You can research the three.

OpenStudy (anonymous):

@shubhamsrg ye sab bc h tum meri baat maano :)

OpenStudy (shubhamsrg):

ye to chutiye hai hi, tum bhi chori karke kam chutiyap nahi kiye! :3

OpenStudy (anonymous):

zubaan sambhal ke,boss is always right

OpenStudy (shubhamsrg):

anyways, my problem still remains unsolved, anyone can explain me the logic in the code ?

OpenStudy (anonymous):

*

OpenStudy (anonymous):

#include<iostream> #include<string> using namespace std; int main(){ int noOfSeq = 0; cout << "Please input an integer" << endl; cin >> noOfSeq; int countingSeq = 0; string prevSeq = "1"; cout << "The first " << noOfSeq << " numbers in the sequence are \n1" << endl; while(countingSeq < (noOfSeq-1)){ string nextSeq = ""; for(int i=0; i<prevSeq.length();i++){ int count = 1; int digitProcessed=0; char countingElement = prevSeq[digitProcessed]; while(digitProcessed < prevSeq.length()){ while(prevSeq[i+1] == countingElement){ count ++; i++; } i++; nextSeq += to_string(count); nextSeq += countingElement; digitProcessed += count; countingElement = prevSeq[digitProcessed]; count = 1; } } cout << nextSeq << endl; prevSeq = nextSeq; countingSeq++; } return 0; }

OpenStudy (anonymous):

This generates first n numbers of the sequence, where n is the input from the user. It's fun to code this! :D

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!