Answered step by step
Verified Expert Solution
Question
1 Approved Answer
USING C++ LANGUAGE ONLY. Please do use the functions and info mentioned. Sample out example shown at the end, please follow that. Our first recursive
USING C++ LANGUAGE ONLY. Please do use the functions and info mentioned. Sample out example shown at the end, please follow that.
Our first recursive function First, let's write a function to sum all the integers in the range between two integers. Algorithm design: We need a function that will take two parameters and return an integer. So we will have a header that looks something like: Our basis will be that if the range only has one number, the value is that number. If the two numbers are equal then our range contains only one value. We will never have a sum of zero, unless zero is the only value in the range. As a result, if the parameters are equal, we will return the value of one of the parameters (I chose the first one, but they're equal so it doesn't matter). if ((val1== val2 ) return vali Next, what happens if our numbers are not in the correct order... If we_assume_values are in one order and they are in the opposite order that would not work. So we will correct parameters provided so they are in the order we want. We can choose the first number as smaller or larger with only minor changes to our algorithm. Here I will swap the values. if (val1 For my example code I will choose the first number as the larger number. This is where our recursive call begins. We add parameter 1 to the sum of the remaining values in the range. Our code could look something like: return val1 + sumRange( val1-1, val2); That's pretty much it. When vall == val 2 our recursion will end, otherwise we compute the sum of the range between val1 - 1 and val2. Because we subtract one each time they will eventually be equal. Implement the summation function/method and in your main function allow someone to enter a range of values as two integers to test the function. Here is some sample output (the main program name is recursive): $./ recursive Please enter your range of integers (on one or more lines) 10 11 The sum of all the integers from 10 to 11 is 21 $./ recursive Please enter your range of integers (on one or more lines) 55 The sum of all the integers from 5 to 5 is 5 $./ recursive Please enter your range of integers (on one or more lines) 11 The sum of all the integers from 1 to 1 is 0 $./ recursive Please enter your range of integers (on one ore lines) 1000 The sum of all the integers from 100 to is 5050 Recursion to print out a string in reverse Now let's get some practice dealing with strings of characters. For this next part we will ask the user to enter a string characters and then print out the reverse of that string... To do this we need to remember that a string is essentially an array of characters. Our basis is: If the string has zero characters, print nothing Recursion: Print the string after the given letter, then print the given letter Our process is: - Ask for the string - Read in the string (use a function that reads an entire line of input, NOTE you may need to remove a newline character) - Call printRevString with the string and the a character counter starting at zero To read a complete line into a string: You will need to watch the input buffer. Since we read in an integer, the input contains the newline... The newline must be removed to read in a string. In my C++ code I use the cin.ignore function to ignore input up through a newline character. In C you can use the statement: while (getc () = ); NOTE this will read until a newline character is found, clearing the input. This is a link to a Java example for clearing scanner input to allow a string to be entered. \( G_{\text {_ }} \) Depending on the version you are using Java may use another technique, andhere is a link to another example G_. For more information on the Java Scanner see the Oracle Java 11 Scanner documentation. G Regardless, for all three languages, the newline character MAY BE included at the end of the string. Change the character at the length of the string - 1 to a NULL to remove the newline character. Definitely needed for C, probably not for C++ (it isn't using gcc), unknown for Java. If the character at the counter is not NULL (equal to zero) call printRevString with the string a counter +1 print the character at the position equal to counter Our method header would be: To check the character, we can use the same notation as we would with an array: NOTE: we can also use integer values with characters, as they are 8-bit unsigned integer values. Here is some more sample output: $./ recursion Please enter your range of integers (on one more lines) 36 The sum of all the integers from 3 to 6 is 18 Please enter a string: Able was I ere I saw Elba. The reverse of your input is: .able was I ere I saw elbA ./ recursion Please enter your range of integers (on one or more lines) 2030 The sum of all the integers from 20 to 30 is 275 Please enter a string: Twizzle, Twazzle, Twahzle, Twone, time for this one to come home - Mr. Wizard The reverse of your input is: draziW .rM - emoh emoc ot eno siht rof emit, enowT , elzhawT , elzzawT , elzziwTStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started