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

Dani AI

Generated

Short, practical plan that builds on and : you can solve this with a simple loop or with recursion; you only need a stack if you must remember every term (for printing later). Break the task into tiny steps: validate input, produce the next term, count it, stop when the termination condition is reached. Keep types wide enough so 3*n+1 doesn't overflow for moderately large inputs.

A safe, compact C++ implementation that preserves the required int lengthBumpy(int n) signature while avoiding overflow and improving repeated-work cost with memoization:

#include <unordered_map>

static int lengthBumpyRec(unsigned long long x, std::unordered_map<unsigned long long,int>& memo) {
    if (x == 1) return 1;
    auto it = memo.find(x);
    if (it != memo.end()) return it->second;
    unsigned long long nxt = (x % 2 == 0) ? x / 2 : 3 * x + 1;
    int res = 1 + lengthBumpyRec(nxt, memo);
    memo[x] = res;
    return res;
}

int lengthBumpy(int n) {
    if (n <= 0) return 0; // or handle as the assignment requires
    std::unordered_map<unsigned long long,int> memo;
    return lengthBumpyRec(static_cast<unsigned long long>(n), memo);
}

Troubleshooting and tips

  • Use unsigned long long internally to avoid overflow when computing 3*n+1.
  • Recursion is neat but can blow the call stack for extreme inputs; switch to an iterative approach if that happens.
  • A stack was suggested only to retain the whole sequence for printing; to get the length you do not need to push every term. If you must print later, collect terms into a std::vector and print or reverse as needed.
  • If the grader runs many queries, memoization (shown above) speeds things dramatically by reusing previously computed lengths.

This keeps the solution simple for a first‑semester programmer while pointing out the practical pitfalls you might hit next.

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.