Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

C program. PLEASE READ THE FULL REQUIREMENTS Consider the following algorithm: Take any natural number n. If n is even, divide it by 2 to

C program. PLEASE READ THE FULL REQUIREMENTS

Consider the following algorithm:

  1. Take any natural number n.
  2. If n is even, divide it by 2 to get n / 2.
  3. If n is odd, multiply it by 3 and add 1 to obtain 3n + 1.
  4. Repeat steps 2 and 3 until n becomes 1.

The Collatz Conjecture is that no matter what number you start with, you will always eventually reach 1. The conjecture has been verified for very large values of n, but has never been proven mathematically.

You are to write a program that investigates the length of sequences of values that are generated by the algorithm for different ranges of n. For example, if n is 3, you get the following sequence: 3, 10, 5, 16, 8, 4, 2, 1. The length of this sequence is 8. Similarly, if n is 4, you get the following sequence: 4, 2, 1. The length of this sequence is 3.

Write a program that:

  1. Prompts the user for an output data filename. The last character of this prompt must be a new line character ( ).
  2. Gets the filename from stdin.
  3. Opens the file for writing.
  4. Receives a line containing the first value in the range from the stdin (DO NOT PROMPT FOR THIS INPUT). The line is terminated by a . If this is a blank line, exit the main loop and terminate the program.
  5. Receives a line containing the last value in the range from the stdin (DO NOT PROMPT FOR THIS INPUT). The line is terminated by a .
  6. Invokes a function called computeMaxSequenceLength
    1. computeMaxSequenceLength does the following:
      1. For each value of n in the range
        1. Invokes a function called computeSequenceLength. computeSequenceLength does the following:
          1. Calls computeNextValue to compute each value of the sequence, using the algorithm shown above.
          2. Returns the length of the sequence.
        2. Updates the max sequence length of the current range of n values.
  7. Writes the first value in the range, the last value in the range, and the maximum sequence length for the range, separated by tabs (\t), and ended with a newline character ( ) to the output data file.
  8. Prints "Complete " to stdout.

Non-Functional Requirements

  1. Your project name must follow the pattern: {FLname}ThreeNPlusOne, where {FLname} is replaced by the first letter of your first name plus your last name. E.g. if your name is Maria Marciano, your project name must be MMarcianoThreeNPlusOne. If your project name does not follow this pattern, it will not be graded.
  2. Your source code file must be named main.c.
  3. main.c must be properly formatted (use Ctrl-Shift-f early and often).
  4. The first executable line of your program must turn off output buffering. This is a requirement for automated testing. The following line will do the trick: setvbuf(stdout, NULL, _IONBF, 0);
  5. Your program may have other functions, but at a minimum, it must have main(), computeMaxSequenceLength(), computeSequenceLength(), and computeNextValue().
  6. Every function, other than main, must have a function declaration as well as a definition.
  7. Every function must be preceeded by a comment that describes what the function does in terms of its parameters and return value.

Input

All ranges will be on the range 1 to 9,999. Sample screen output (black) and keyboard input (blue):

Enter the output filename: out.txt 1 10 900 1000 

Sample Output File

1 10 20 900 1000 174

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatio Temporal Database Management International Workshop Stdbm 99 Edinburgh Scotland September 10 11 1999 Proceedings Lncs 1678

Authors: Michael H. Bohlen ,Christian S. Jensen ,Michel O. Scholl

1999th Edition

3540664017, 978-3540664017

More Books

Students explore these related Databases questions