Using C++ Do not make use of any variables in your answers to this question. Onl
ID: 3834830 • Letter: U
Question
Using C++
Do not make use of any variables in your answers to this question.
Only consider positive numbers.
Part A. Write a function parta(int x) which prints a sequence of numbers starting with x itself, and counting down, but stopping after it first prints a number that is divisible by 11. For example, parta(30) should print:
30 29 28 27 26 25 24 23 22
Part B. Write another function partb(int x) which does not print anything, but instead returns as its result the length of the sequence that parta(x) would have printed.
For example, const int n = partb(30) should set n to 9, because parta(30) prints 9 numbers.
Part C. There is a mysterious mathematical thing called a Collatz Sequence. It can start with any number N. If N is odd, the rest of the sequence is the collatz sequence starting from 3*N+1, but if N is even, the rest of the sequence is the collatz sequence starting from N/2. The sequence stops after it reaches 1.
Write a function partc(int N) which prints the entire Collatz sequence starting from N.
For example, partc(24) should print:
24 12 6 3 10 5 16 8 4 2 1
Part D. Write another function partd(int N) which returns as its result the length of the Collatz sequence that starts with N.
For example, partd(24) is equal to 11, because as you can see from the previous example, the Collatz sequence starting from 24 consists of 11 numbers.
Explanation / Answer
Dear Student,
Here is the answer...
Below i have written the functions for each part.
------------------------------------------------------------------------------------------------------------------------------------
PART:A function..
int parta(int x)
{
printf("%d ", x);
if(x%11==0)
{
exit(0);
}
return parta(x-1);
}
----------------------------------------------------------------------------------------------------------------------------------------
PART:B function
int partb(int x)
{
//varible declaration
static int ;
//run until the x%11 != 0
while(x%11 != 0)
{
n++;
partb(x-1);
}
//return count
return n;
}
----------------------------------------------------------------------------------------------------------------------------------------
PART:C function
int partc(int N)
{
//while N>0
while(N>0)
{
//if N is odd
if(N%2 != 0)
{
printf("%d", N);
N = 3*N+1;
}
//if N is even
else if(N%2==0)
{
printf("%d", N);
N = N/2;
}
}
return;
}
----------------------------------------------------------------------------------------------------------------------------------------
PARTD: function
int partd(int N)
{
//initialize count to zero
static int count = 0;
//while N>0
while(N>0)
{
//if N is odd
if(N%2 != 0)
{
printf("%d", N);
N = 3*N+1;
//increment count
count++;
}
//if N is even
else if(N%2==0)
{
printf("%d", N);
N = N/2;
//increment count
count++;
}
}
//return total numbers
return count;
}
----------------------------------------------------------------------------------------------------------------------------------------
Kindly Check and Verify Thanks....!!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.