Question

C++Write a program that reads in two Integers. A starting number and a number of iterations. For each iteration, you generate a

1 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

I will provide you with a code for the problem in c++. The code is quite is self-explanatory with just a twist. The twist is that since the size of the numbers gradually increases I have used the built-in "string" data structure in c++ to store the numbers instead of using integers or long integers. It is better this way since it will work for much larger numbers and also writing the code is much easier. I have written the code in a way easier for you to understand, just give it a read and I hope you will understand how it works. One important thing is that do remember to run the code in higher versions of c++ (preferably c++11) if you are using Linux use the flag --std=c++11. If you have any problem understanding the code leave down a comment below about what is bothering how and I will be more than happy to help you out. Now here goes the code:-

#include <bits/stdc++.h> // imports all necessary headers

using namespace std;

int main()
{  
   string num;
   int iter;
   cin >> num >> iter;
   for(int i = 1; i <= iter; i++) {
       int len = num.size();
       num += '?';
       string temp = "";
       int count = 0;
       for(int j = 0; j < len; j++) {
           if(num[j] == num[j + 1])
               count++;
           else {
               count++;
               temp += to_string(count);
               temp += num[j];
               count = 0;
           }
       }
       cout << "iteration no " << i << " -- " << temp << endl;
       num = temp;
   }
   return 0;
}
Thank you. Happy Coding.

Add a comment
Answer #2

#include <bits/stdc++.h>



int main()
{  
   string num;
   int iter;
   cin >> num >> iter;
   for(int i = 1; i <= iter; i++) {
       int len = num.size();
       num += '?';
       string temp = "";
       int count = 0;
       for(int j = 0; j < len; j++) {
           if(num[j] == num[j + 1])
               count++;
           else {
               count++;
               temp += to_string(count);
               temp += num[j];
               count = 0;
           }
       }
       cout << "iteration no " << i << " -- " << temp << endl;
       num = temp;
   }
   return 0;
}



Add a comment
Know the answer?
Add Answer to:
C++ Write a program that reads in two Integers. A starting number and a number of iterations. For each iteration, you ge...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT