I just need a function. Thank You Consider a sequence of single-digit natural nu
ID: 3637378 • Letter: I
Question
I just need a function.Thank You
Consider a sequence of single-digit natural numbers written out on a line, e.g.,
3, 4, 4, 2
A subsequent line of numbers is defined in terms of its immediately preceding line by the following recursive rule:
“Each line n (for n>1) describes the previous line (n-1) as a sequence of pairs of numbers, as follows. Line n starts with a pair of numbers x, y describing how many times (x) the leading number (y) from line (n-1) occurs at the start of line (n-1). Then, the same procedure is applied on the remainder of line (n-1) – i.e., after the first x characters are ignored/removed from it – and the result is appended to line n.”
For example, the very next line after the sample one above would be
1, 3, 2, 4, 1, 2,
representing that the former line consists of 1 digit 3, followed by 2 digits 4, followed by 1 digit 2.
Write functions in C++ that, given one line of such a sequence of numbers (e.g., provided by the user as command-line arguments), computes the immediate next line, as well as the immediate previous line. Be sure to test your code for correctness.
For example, given the line:
1, 3, 3, 1
the next line would be
1, 1, 2, 3, 1, 1
while the previous line would be
3, 1, 1, 1
Explanation / Answer
well i'm too lazy to add comments to clarify the codes...
#include <iostream>
using namespace std;
int main(int argc, char * argv[])
{
void printNextSequenceLine(const char *str, int num = 0, int count = 0);
void printPreviousSequenceLine(const char *str, int times = 0);
char test[] = "3, 1, 1, 1, 1, 1, 1, 1, 1";
char test2[] = "1, 3, 3, 1";
char test3[] = "3, 4, 4, 2";
char test4[] = "1, 3, 2, 4, 1, 2";
char test5[] = "1, 1, 1, 3, 1, 2, 1, 4, 1, 1, 1, 2";
const int REAL_LEN = 101;
char real[REAL_LEN];
//Hardwired tests
printNextSequenceLine(test);
printNextSequenceLine(test2);
printNextSequenceLine(test3);
printNextSequenceLine(test4);
printNextSequenceLine(test5);
cout << endl;
printPreviousSequenceLine(test2);
printPreviousSequenceLine(test3);
printPreviousSequenceLine(test4);
printPreviousSequenceLine(test5);
//User-defined tests
cout << endl;
cout << "Enter a sequence: ";
cin.getline(real, REAL_LEN);
cout << endl;
cout << "Previous sequence: ";
printPreviousSequenceLine(real);
cout << " Entered sequence: " << real << endl;
cout << " Next sequence: ";
printNextSequenceLine(real);
cout << endl;
return 0;
}
///Please use string with no white spaces/other characters than number
//before or after the sequence of single-digit numbers
///Please use ", " or 2 white spaces to seperate the single-digit numbers
void printNextSequenceLine(const char *str, int num = 0, int count = 0)
{
if (!num && !count)
{
printNextSequenceLine(str + 3, *str - 48, 1);
}
else
{
if (*str - 48 == num)
{
count++;
if (*(str + 1))
{
printNextSequenceLine(str + 3, num, count);
}
else
{
cout << count << ", " << num << endl;
}
}
else
{
cout << count << ", " << num;
if (*(str + 1))
{
cout << ", ";
printNextSequenceLine(str + 3, *str - 48, 1);
}
else
{
cout << ", 1, " << str << endl;
}
}
}
}
///Please use string with no white spaces/other characters than number
//before or after the sequence of single-digit numbers
///Please use ", " or 2 white spaces to seperate the single-digit numbers
void printPreviousSequenceLine(const char *str, int times = 0)
{
if (!times)
{
printPreviousSequenceLine(str + 3, *str - 48);
}
else
{
for (int i = 0; i < times; i++)
{
cout << *str - 48;
if (i + 1 != times || *(str + 1))
{
cout << ", ";
}
}
if (*(str + 1))
{
printPreviousSequenceLine(str + 3, 0);
}
else
{
cout << endl;
}
}
}