Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3753728
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:26:10+00:00 2026-05-19T09:26:10+00:00

This is a task to be done in C. Can you tell me how

  • 0

This is a task to be done in C. Can you tell me how to approach this?

A train has a length of n meters. It is made up of individual compartments of 1 or 2 meters in length. How many different combinations of such compartments exist for a train of a given length? Write a function Train (n) that computes this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T09:26:11+00:00Added an answer on May 19, 2026 at 9:26 am

    Start with the simplest cases and look for regularities.

    1. Train (1) is obviously 1: (1).
    2. Train (2) is obviously 2: (1 1) and (2).
    3. Train (3) is 3: (1 1 1), (1 2), and (2 1). The first two can be combined into conjoining (1) with (1 1) resp (2). The latter are exactly the combinations of Train (2). So, Train (3) is Train (2) + 1.
    4. Train (4) is 5: (1 1 1 1) (1 1 2) (1 2 1) (2 1 1) (2 2). Again, we can combine the first as (1) with (1 1 1), (1 2), and (2 1), which are the combinations of Train (3). The last are (2) conjoined with (1 1) and (2), which are the combinations of Train (2). So, Train (4) is Train (3) + Train (2). Now, looking back at Train (3), we see that the + 1 is Train (1).

    Now it seems clear that Train (n) is always Train (n - 1) + Train (n - 2). That is exactly the definition of the Fibonacci sequence.

    Now, let us see how that is translated to C.

    1. The function skeleton: Train takes one integer argument and returns an integer:

      int Train (int n) {
      }
      
    2. The definition we worked out:

      int Train (int n) {
          return Train (n - 1) + Train (n - 2);
      }
      
    3. This will recurse infinitely, so we need to stop it at the base case. One base case is clear: Train (1) is 1:

      int Train (int n) {
          if (n == 1) {
              return 1;
          } else {
              return Train (n - 1) + Train (n - 2);
          }
      }
      
    4. This is still not enough. Imagine what Train (2) does: it will calculate Train (1) + Train (0). Train (1) is no problem, but Train (0) will calculate Train (-1) + Train (-2), which again recurses infinitely. So, we need another base case: Train (2) is 2.

      int Train (int n) {
          if (n == 1) {
              return 1;
          } else if (n == 2) {
              return 2;
          } else {
              return Train (n - 1) + Train (n - 2);
          }
      }
      
    5. This works, but we can simplify the base cases:

      int Train (int n) {
          if (n < 3) {
              return n;
          } else {
              return Train (n - 1) + Train (n - 2);
          }
      }
      
    6. If you now just paste that last code snippet into your homework without working through the “too long, didn’t read” preliminaries, I have successfully undermined your education, and you will never learn to program. You are welcome.

    7. This is not the best way to calculate Fibonacci numbers. Why? How should you modify the code to avoid duplication of effort? Are there different approaches imaginable? Which ones?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a task like this: @task def test(): time.sleep(10) test.update_state(state=PROGRESS) time.sleep(10) return done
I use an ExecutorService to execute a task. This task can recursively create other
Can anyone tell me why this code generates queue after starting the threads? Basically,
Hi I have done this task. But I have to clarify some thing. I
I have this Task model: class Task < ActiveRecord::Base acts_as_tree :order => 'sort_order' end
I was once given this task to do in an RDBMS: Given tables customer,
I have this task for the project with 4 nested subprojects using Maven: For
Please explain what this task is about? Create a generic linked list class that
what's the best way to accomplish this task? (new to sharepoint) Do we need
I have had no luck with this task so far so grateful for any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.