Fix this C++ below so does not use any loops at all. Use recursion instead. here is some guides given.
|
Create a directory to hold assignment 3. Copy files hailstone.cpp, Makefile and dotestassignment 2 to the assignment 3 directory. Edit the comments at the top of hailstone.cppto say that this is assignment 3. You should be able to keep the same contracts, 'next' function and 'main' function from assignment 2, unless they contained errors that needed to be fixed. If necessary, edit those functions so that they do not change the value of any variable, once the variable has a value. |
|
2. Modify the other functions so that they use recursion instead of loops. Modify them one at at time, and test each one after modifying it. Do not try to modify them all then test them all together. The remaining items give some hints. |
|
3. Modify the function that prints the entire sequence. Include a special case for n = 1. When n > 1, the sequence that starts at n contains n followed by the sequence that starts at next(n). |
|
4. Suppose that you want to find the length of the hailstone sequence starting at 7. Notice that next(7) = 22. If you ask for the length of the hailstone sequence starting at 22 (result: 16), how would you determine the length of the sequence starting at 7? Handle the length of the sequence starting at 1 as a special case. Use an if-statement to decide whether this is the special case of n = 1 or not. |
|
5. Suppose that you want to know the largest number in the sequence starting at 7. Ask for the largest number in the sequence starting at 22. (The answer is 52.) The largest number for 7 is clearly the larger of 7 and 52, since 7 is the only number that was not already taken into account in the sequence starting at 22. As another example, suppose that you want to know the largest number in the sequence starting at 52. Ask for the largest number in the sequence starting at 26 (since next(52) = 26). The answer is 40. What you want is the larger of 52 and 40: clearly 52. Important Note. In terms of efficiency, recursion acts as an amplifier. Algorithm designers use it because, if they think of a clever idea and they use it in a recursive definition, recursion amplifies it into a very efficient algorithm. But recursion also amplifies bad ideas. If you do something in a slopply way in a recursive function, recursion can amplify it and make a very slow algorithm. In particular, if you do the same recursive call twice, which is a waste of time, recursion will be very slow. Try your program on input 97. Does it respond quickly, or is it really slow? Try it on a few larger numbers, say around 1000. |
|
6. Suppose you want to compute the length of the longest sequence starting on a number from 1 to n. Ifn = 1, the answer is obvious. If n > 1, first get the length k of the longest sequence starting on a number from 1 to n − 1. Then compute the larger of length(n) and k. |
|
7. Suppose you want to compute the starting value of the longest sequence starting on a number from 1 to n. Again, the answer is obvious when n = 1. If n > 1, first get the starting value m of the longest sequence starting on a number from 1 to n − 1. Then compare length(n) and length(m). How can you determine the answer from the result of that comparison? HERE IS THE CODE Fix this C++ below so does not use any loops at all. Use recursion instead. // next //=========================================== //next(n) returns next value in the hailstone //sequence that follows n //=========================================== #include int next(int n) { int x = n; if (x % 2 == 0) { return x / 2; } else { return 3 * x + 1; } } //=========================================== // hailOut //=========================================== //hailOut(n) prints the hailstone sequence //to the standard output //=========================================== void hailOut(int n) { for (int x = n; x > 1; x = next(x)) { printf("%i ", x); } printf("1 "); } //=========================================== // hailLength //=========================================== //hailLength(n) returns the length of the //hailstone sequence starting at n //=========================================== int hailLength(int n) { int count = 1; int x = n; while (x > 1) { x = next(x); count++; } return count; } //=========================================== // hailLargest //=========================================== //hailLargest(n) returns the largest value of //the hailstone sequence starting at n //=========================================== int hailLargest(int n) { int ans = n; int x = n; while (x != 1) { if (ans <= x) { ans = x; } x = next(x); } return ans; } //=========================================== // hailLargestLength //=========================================== //hailLargestLength(n) returns the largest //possible length of a sequence that starts //from 1 to n //=========================================== int hailLargestLength(int n) { int x = n; int ans = hailLength(x); if (x == 1) { return 1; } while (x > 1) { if (ans <= hailLength(x)) { ans = hailLength(x); } x = x - 1; } return ans; } //=========================================== // hailLargestStartValue //=========================================== //hailLargestStartValue(n) returns the start //value of the longest hailstone sequence //starting with a number from 1 to n. //=========================================== int hailLargestStartValue(int n) { int x = n; int val = n; int ans = hailLength(x); if (x == 1) { return 1; } while (x > 1) { if (ans <= hailLength(x)) { ans = hailLength(x); val = x; } x = x - 1; } return val; } //=========================================== // hailSequenceLargestValue //=========================================== //hailSequenceLargestValue(n) returns the //largest value that occurs in a hailstone //sequence that starts with a number from 1 to n. //=========================================== int hailSequenceLargestValue(int n) { int x = n; int ans = hailLargest(x); if (x == 1) { return 1; } while (x > 1) { if (ans <= hailLargest(x)) { ans = hailLargest(x); } x = x - 1; } return ans; } //=========================================== // main //=========================================== int main() { int n = 0; printf("What number shall I start with? "); scanf("%i", &n); printf("The hailstone sequence starting at %i is: ", n); hailOut(n); printf(" The length of the sequence is %i. ", hailLength(n)); printf("The largest number of the sequence is %i. ", hailLargest(n)); //printf("There are %i local maxima. ", hailMaxima(n)); printf("The longest hailstone sequence starting with a number up to %i has length %i. ", n, hailLargestLength(n)); printf("The start value of the longest hailstone sequence starting with a number up to %i is %i. ", n, hailLargestStartValue(n)); printf("The largest value that occurs in a hailstone sequence that starts with a number up to %i is %i. ", n, hailSequenceLargestValue(n)); return 0; } |
#include <iostream>
using namespace std;
//prints the hailstorm series
void hailOut(int N)
{
static int c;
//prints the series
cout<<N<<" ";
if (N == 1 && c == 0) {
// N is initially 1.
return;
}
else if (N == 1 && c != 0) {
// N is reduced to 1.
c++;
return;
}
else if (N % 2 == 0) {
// If N is Even.
c++;
hailOut(N / 2);
}
else if (N % 2 != 0) {
// N is Odd.
c++;
hailOut(3 * N + 1);
}
}
//finds the legth of the series
int HailLength(int N)
{
static int c;
int count;
if (N == 1 && c == 0) {
// N is initially 1.
return c;
}
else if (N == 1 && c != 0) {
// N is reduced to 1.
c++;
count= c;
c=0;
return count;
}
else if (N % 2 == 0) {
// If N is Even.
c++;
HailLength(N / 2);
}
else if (N % 2 != 0) {
// N is Odd.
c++;
HailLength(3 * N + 1);
}
}
// returns largest value of the series
int HailLargest(int N)
{
static int c;
//stores the largest number of the series
static int max;
if(N>max)
max=N;
if (N == 1 && c == 0) {
// N is initially 1.
return c;
}
else if (N == 1 && c != 0) {
// N is reduced to 1.
c++;
return max;
}
else if (N % 2 == 0) {
// If N is Even.
c++;
HailLargest(N / 2);
}
else if (N % 2 != 0) {
// N is Odd.
c++;
HailLargest(3 * N + 1);
}
}
//return length of longest sequence in series 1 to N
int longestHailstoneSequence(int N)
{
int lengthOfSeries;
static int longestSeries;
if(N>0)
{
lengthOfSeries=HailLength(N);
if(lengthOfSeries>longestSeries)
longestSeries=lengthOfSeries;
longestHailstoneSequence(N-1);
}
else
return longestSeries;
}
//returns max value of the series from 1 to N
int hailSequenceLargestValue(int N)
{
int max;
static int seriesMax;
if(N>0)
{
max=HailLargest(N);
if(max>seriesMax)
seriesMax=max;
hailSequenceLargestValue(N-1);
}
else
return seriesMax;
}
// returns start value of longest sequence
int hailLargestStartValue(int N)
{
int lengthOfSeries;
static int longestSeries;
static int startValue;
if(N>0)
{
lengthOfSeries=HailLength(N);
if(lengthOfSeries>longestSeries)
{
longestSeries=lengthOfSeries;
startValue=N;
}
hailLargestStartValue(N-1);
}
else
return startValue;
}
int main(void) {
//starting number for the sequence
int N;
int end;
cout<<"What number shall I start with?"<<endl;
cin>>N;
cout<<"Hailstone starting at "<<N<<" is:";
hailOut(N);
cout<<endl<<"Length of
series="<<HailLength(N)<<endl;
//cout<<"Largest number in the
series="<<HailLargest(N)<<endl;
cout<<"Enter the end number of the sequence 1 to
N"<<endl;
cin>>end;
cout<<"Longest sequence length starting with 1 to
"<<end<<"
is:"<<longestHailstoneSequence(end)<<endl;
cout<<"Largest value starting with 1 to "<<end<<"
is:"<<hailSequenceLargestValue(end)<<endl;
cout<<"The start value of the longest hailstone sequence
starting with a 1 up to "<<end<<"
is:"<<hailLargestStartValue(end)<<endl;
return 0;
}
Sample Output:
Hope this helps.. Thanks!
Fix this C++ below so does not use any loops at all. Use recursion instead. here...
Need help in my C language assignment... all I need to do is
1.Use recursion instead
2. NO LOOPS ALLOWED
Output _______________________________________
What number shall I start with? 16
sequence: 16 8 4 2 1
length: 5
largest: 16
For start values from 1 to 16:
longest: 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
length: 20
containing largest: 15 46 23 70 35 106 53 160 80...
Assignment 6: Recursion Learning Outcomes • Learn how to craft solutions using recursion instead of loops. Instructions This assignment will be different than previous assignments (and most assignments which come after it). In this assignment, you will be crafting four solutions to four different problems. This assignment will also have special requirements regarding how you may code. You are not allowed to use assignment statements. This includes using preincement, postincrement, predecrement, and postdecrement. You are allowed to use assignment to...
In python... All functions must use recursion. Do not use any iteration or slicing. 1. Return the reverse of the given string. Use the index argument to keep track of position, which starts at zero. reverse_string(chars: str, index: int) -> str 2. Return the highest number in the list of integers. Use the index argument to keep track of position, which starts at zero. find_max(ints: List[int], index: int) -> int 3. Return True if the given string is a palindrome...
C++ Recursion Practice! 1)Multiplication #include <iostream.h> int Multiply(int M, int N) //Performs multiplication using the + operator. //Pre : M and N are defined and N > 0. //Post: Returns M x N { int Prod; if (N == 1) Prod = M; //base case else Prod = M + Multiply(M, N - 1); //recursive step return Prod; } 2) Reverse #include <iostream.h> void Reverse(int N) //Displays string of length N in the reverse order //Pre : N...
Can someone fix the program below so it does what the picture
says it won't work for me.
import java.util.Scanner;
public class Userpass {
static String arr[];
static int i = 0;
public static void main(String[] args) {
String username, password;
int tries = 0, result;
do {
System.out.print("Enter the username: ");
username = readUserInput();
System.out.print("Enter the password: ");
password = readUserInput();
result = verifyCredentials(username, password);
tries++;
if (result == -1)
System.out.println("The username is incorrect!\n");
else if (result == -2)...
Can someone fix my coding please it's a calendar some days are placed wrong. I just want the Main menu to display first and second option and four option to end the program properly. Please remove option number 3 Find the number of days between 2 selected dates and the code that belongs to this function. #include<stdio.h> void days(int,int,int,int,int,int); int month(int,int); int mon[12]={31,28,31,30,31,30,31,31,30,31,30,31}; #define TRUE 1 #define FALSE 0 int days_in_month[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; char *months[]= { " ", "\n\n\nJanuary", "\n\n\nFebruary", "\n\n\nMarch", "\n\n\nApril",...
C programming Let's try to use the template below to implement a Set with double hashing. Here we assume the Set contains names with 3 characters long. Since it is a Set, we do not need to have an explicit value for each key. We will use a token value of 1 if a name belongs to the Set. Let's reuse the exact hash functions we have seen in example in Part 7 of the lecture notes, page 3. As...
Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...
please do in c++Q4. Using recursion only (no loops allowed!), write a C++ function of the form int count7s(int) that when passed a non-negative integer, returns the number of occurrences of the digit 7 in the number. So, for example: count7s(717) → 2 count75(7) →1 count7s(123) → 0 Q5. Write a C++ function of the form string mirrorEnds(string)that when given a string, looks for a mirror image (backwards) string at both the beginning and end of the given string. In other words, zero or...
Please Complete the following C Code with Comments explaining your solution and post a screenshot of it working. Summary: This project explores pattern matching techniques to find a pattern in a DNA sequence containing letters in the DNA alphabet {A, C, G, T}. For example, suppose we have a DNA sequence as follows: ATGACGATCTACGTATGGCAGCCACGCTTTTGATGTTAAGTCACACAGCCAAGTCA ACAAGGGCGACTTCATGATCTTTCCGCTCCGTTGGTGTAGGCCCGTGTTCAAATTC AATGGCTGATTGGAATTACCTTTGAAATACTCCAACCGACCGCCACGGCCAGGGT CCCGCTCGCTCTCTGTGGCCCTCCCACAAAACTCCGGTGAAAGTTGATTTGGACAC GGACCCAAAGCAGCGTAGATTATTCGAGCGTATTCGGTAGTCATTGAGGCCCCAA The pattern “AATGG” can be found at the beginning of the third line. Note that overlapping matches are counted individually. For example,...