hi i am new to c++ i have problem to solve kindly help me
Start with any positive number n. If n is even divide it by 2, if n is odd multiply by 3 and add 1. Repeat until n becomes 1.For example sequence that starts at n = 10 is:
10, 5, 16, 8, 4, 2, 1
Write a function named lengthBumpy that returns the length of the bumpy sequence generated by its integer argument. The signature of the function is
int lengthBumpy(int n).suppose if n = 10 it return lenght of 7 (10, 5, 16, 8, 4, 2, 1)

help me please i canoot solve this problem i have spent 5 hours to do this but all in va in

Recommended Answers

All 3 Replies

hi i am new to c++ i have problem to solve kindly help me
Start with any positive number n. If n is even divide it by 2, if n is odd multiply by 3 and add 1. Repeat until n becomes 1.For example sequence that starts at n = 10 is:
10, 5, 16, 8, 4, 2, 1
Write a function named lengthBumpy that returns the length of the bumpy sequence generated by its integer argument. The signature of the function is
int lengthBumpy(int n).suppose if n = 10 it return lenght of 7 (10, 5, 16, 8, 4, 2, 1)

help me please i canoot solve this problem i have spent 5 hours to do this but all in va in

What part do you need help with? Make a loop in your function, or have the function recursive, and have it perform the operations:

count = 1;
while (n > 1){
  count = count + 1;
  if (n % 2) ...//do stuff here
  else ... //do stuff here
}

return count;

thanks i am in first semester .i used same approach but some one told me to use stack:(i havent studied stack yet it must be in my third semester thats why i got confused.:(

If you haven't learned stacks yet, then you should (probably) ignore the advice. Instead, just think through the problem in very small pieces:
1) Start with your input value. Is that part of the "bumpy" sequence? If so, count it.
2) Is your sequence finished? If so, you should already know how many items are in it. You're done.
3) Otherwise, compute the next value of the sequence, and count it also. Go back to step 2.

If you can can code a loop that addresses steps 2 and 3, then that's all that is required to code the assignment. (Hint: "Go back to step 2" is the problematic piece -- instead you really need a loop that keeps going until you discover that you're done.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.