Assignment help, functions.

My assignment is this.
The purpose of this assignment is to reinforce the concepts types, I/O, and the process of decomposing
problems into smaller units that can be implemented as functions and tested independently. Your program should begin by asking the user if they want to find the longest sequence in a range of
numbers, asking the user for their name, then asking the user for a positive integer number (n). Your program should then generate n sequences of numbers (one for every value between 1 and n, inclusive)
based on the following two rules. Starting with a number k, the next number in the sequence is k/2 if k is even; otherwise it is (3*k)+1. The sequence continues using these rules until a number less than
or equal to 5 is generated. When a number less than or equal to 5 is generated, the sequence ends
without counting that number as part of the sequence length. Therefore any number entered that has
a value of 5 or less has a sequence length of 0. For example, the sequence starting at 17 is
17 52 26 13 40 20 10 5
and its length is 7.
Your program should keep track of the length of each sequence generated and output initial number
of the longest sequence generated as well as its length. The program should repeat this behavior
(requesting if the user wants to find a sequence, requesting a name, computing the longest sequence)
until the user enters n or N for the first question.
You may notice that depending upon the starting number of the sequence, the length of the sequence can vary dramatically. For example, a sample run of your program may look like the following:

Would you like to find the longest sequence of a range of numbers? (Y or N) y
Please enter your name: Scott
Please enter value: 10
Scott entered value: 10
The initial number with the longest sequence is: 9
The sequence length for 9 is: 14
Would you like to find the longest sequence of a range of numbers? (Y or N)N
Thank you.

We get this response because the sequence starting with 9 is: 9 28 14 7 22 11 34 17 52 26 13 40 20 10 and it is longer than the sequences that start with all the other numbers between 1 and 10.

Here is another sample run:

Would you like to find the longest sequence of a range of numbers? (Y or N)y
Please enter your name: Scott
Please enter value: 1000
Scott entered value: 1000
The value with the longest sequence is: 871
The sequence length for 871 is: 173

Observe that you should not print out any of the sequences. Furthermore your program should accept the input exactly as described here.
To solve this problem we want you to use a particular set of functions. In this way you'll gain experience
with functions and the process of decomposing a problem into smaller parts that can be implemented with functions. These are the functions you must implement:

1. PromptUserToFindLongestSequence - This function asks the user if they want to find a longest sequence. The only valid input is Y, y, N, and n If any other character is entered, the user should
be informed that the input is invalid, and then re-prompted to enter their decision. This function returns the information indicating whether the user wishes to continue or not.

2. GetUserName - This function prompts the user for their name and returns this string value back to the calling program. The name cannot contain spaces.

3. GetUserInputValue - This function prompts the user for a positive integer number and validate it. The function repeatedly prompts the user for a positive integer number until one is entered.The function returns this value.

4. NextValueInSequence - This function takes a number as a parameter and returns the next number in the sequence using the rules given above.

5. SequenceLength -This function takes a positive number as a parameter and returns the length of the sequence starting with this number.

6. NumberWithLongestSequenceLength - This function takes a positive number n and returns the number between 1 and n that starts the sequence with longest length.

7. DisplayAndRecordResults - This function displays the initial number (n), the number which has the longest sequence length between 1 and n, and the length of the longest sequence found. This function also creates a file results.txt that stores the person's name on the first line,
the initial number n on the second line, the number with the longest sequence on the third line,
and finally the sequence length of that number on the fourth line. (NOTE: if the user runs this
multiple times, the file must contain only the data from the last run.)
***************************************************
so far I have written this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

string getUserName () {
	string name; 
	cout << "Please enter your name: ";
	cin >> name;
	return name;
}

bool promptUserToFindLongestSequence ()  {
	char userInput;
	cout << "Would you like to find the longest sequence of a range       of numbers? (Y or N)" << endl;
	cin >> userInput;
		if (userInput == 'y' || 'Y')
		return false;
		
	if (userInput == 'n' || 'N')
		return true;

}



int main () {
	
	char userAnswer;
	userAnswer = promptUserToFindLongestSequence ();
	if (userAnswer = true)
		getUserName ();
	if (userAnswer = false)
		cout << "Thank you." << endl;

	return 0;
}

The only things I care about for now are, promptUserToFindLongestSequence and getUserName.
now my problem is that no matter what I type in for promptUserToFindLongestSequence, it still asks me to input a name where it should just say "thank you" and the program should terminate.
Last edited on
OP wrote:
if (userInput == 'y' || 'Y')

Must be: if (userInput == 'y' || userInput=='Y')
And also for line: #21.


if (userAnswer = true)

Must be: if (userAnswer == true) //Note: two equal signs not one
Also for line: #34.

Thanks,
Aceix.
Last edited on
You are using the assignment operator = instead of the comparision operator == in the following if-else statement

1
2
3
4
	if (userAnswer = true)
		getUserName ();
	if (userAnswer = false)
		cout << "Thank you." << endl;


Moreover it is better to substitute it for

1
2
3
4
	if ( userAnswer == true )
		getUserName ();
	else
		cout << "Thank you." << endl;
This worked, thanks guys.
I now went on to write the next part of the code, the part that takes (n) and generates n sequences of numbers (1 to n) and finds the next number according to these rules:

if a number is even number=number/2
if number is odd number=(3*number)+1
this keeps going until number<=5
For example, the sequence starting at 17 is
17 52 26 13 40 20 10 5
and its length is 7.
5 is not counted in the sequence.

The program should keep track of the length of all sequences of numbers from 1 to 17 and show number will have the most numbers in its sequence. In this case the sequence that starts with 17 is the longest of sequences that start are from 1-17 inclusive, but if I enter 1000, the number with the longest sequence is 871.
my code so far finds the sequence for just a single number, and gives the count.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	int n;
	
	int count = 0;

	cout << "Please enter a value: " << endl;
	cin >> n;
	int number = n;
	while (n>5){
		if (n%2 == 0)
			n=n/2;
		else
			n=(3*n)+1;
		cout << n << ", " << endl;
		count ++;
	}
	cout << count;

My problem is this, I am trying to do this in a single loop, but I am not sure if this is possible. I had an idea where this loop is done for n, and then n=n-1 and the loop goes again, but that would stop at 6 because the loop rule is while (n>5)
but that would stop at 6 because the loop rule is while (n>5)

The loop's condition is not the condition you're thinking of. Must be: while(n<6).

Thanks,
Aceix.
What I want to do is to run this loop for every number 1 until a value entered by me, but so far it only loops for the value entered by me. So if I enter 1000 for example, i want the loop to run for each number from 1 to 1000.
Last edited on
Well, you need two loop. One is for-loop and the other do-while loop

Len assume that n is the entered number. In my code it will be defined as a constant.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{
	const unsigned int n = 20;

	unsigned int max_length, max_number; 

	for ( unsigned int i = 1; i <= n; i++ )
	{
		unsigned int j = i;
		unsigned int count = 0;
		do
		{
			if ( j % 2 ) j = 3 * j + 1;
			else j /= 2;
			count++;
		} while ( !( j <= 5 ) );

		if ( i == 1 || max_length < count )
		{
			max_length = count;
			max_number = i;
		}

		std::cout << "i = " << i << ", count = " << count << std::endl;
	}

	std::cout << "Max_number = " << max_number << ", max_length = " << max_length << std::endl;
}


EDIT: I tested the code and indeed for n = 1000 the maximum length is 173 for number 871.
Last edited on
I am quite new to c++ so I have not seen "do" yet. It looks like a loop but why is "while" in the end? Is there a way to rewrite this using just a while loop?
The do-while can be rewritten as while but it is more natural for this situation to use the do-while because we need at least one iteration for each unsigned number in the given range.
You can rewrite the do-while the following way

1
2
3
4
5
6
7
		while ( true )
		{
			if ( j % 2 ) j = 3 * j + 1;
			else j /= 2;
			count++;
			if ( j <= 5 ) break;
		}


Last edited on
and the true in the while loop is what? and what does break mean? Sorry that I am asking so many questions and frustrating you, I am just really new to this.
How are you going to use loops if you do not know what is true? Each loop shall evaluate a condition which is converted to a bool value true or false.
Last edited on
we havent learned that yet, so the condition for the while loop could be while(j >= 5)
EDIT
so I modified my while statement
it is now
1
2
3
4
5
while (j >= 5)
		{
			if ( j % 2 ) j = 3 * j + 1;
			else j /= 2;
			count++;


but this results in a max length that is too high. When I enter 1000 i get 871 and 176
and when I enter 9 i get 17 instead of 14
Last edited on
But still, Vlad was correct, the condition (j >= 5) is evaluated to see whether it is true or false, in order to control the loop.

while (true) could be thought of as meaning "always" or "repeat forever".

break allows a means of exiting immediately from the loop, or other control structure.
Topic archived. No new replies allowed.