Introduction to Algorithms
3rd Edition
ISBN: 9780262033848
Author: Thomas H. Cormen, Ronald L. Rivest, Charles E. Leiserson, Clifford Stein
Publisher: MIT Press
expand_more
expand_more
format_list_bulleted
Question
Chapter 30.2, Problem 3E
Program Plan Intro
To perform exercise
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
implement getbyte function.
getByte(x,n) Extract byte n from word x
/* * getByte - Extract byte n from word x* Bytes numbered from 0 (LSB) to 3 (MSB)* Examples: getByte(0x12345678,1) = 0x56* Legal ops: ! ~ & ^ | + << >>* Max ops: 6*/int getByte(int x, int n) {return 2;}
We want to build 10 letter “words” using only the first n=9n=9 letters of the alphabet. For example, if n=5n=5 we can use the first 5 letters, {a,b,c,d,e}{a,b,c,d,e} (Recall, words are just strings of letters, not necessarily actual English words.)
How many of these words are there in total?
How many of these words contain no repeated letters?
How many of these words start with the sub-word “ade”?
How many of these words either start with “ade” or end with “be” or both?
How many of the words containing no repeats also do not contain the sub-word “bed”?
Given a positive integer, N, the ’3N+1’ sequence starting from N is defined as follows:
If N is an even number, then divide N by two to get a new value for N.
If N is an odd number, then multiply N by 3 and add 1 to get a new value for N.
Continue to generate numbers in this way until N becomes equal to 1.
For example, starting from N = 3 the complete ’3N+1’ sequence would be:3, 10, 5, 16, 8, 4, 2, 1
Do the following:
Write a code in C++ to ask the user to enter a positive integer (N) in the main() function. Write a function sequence() that receives the integer value N and display the ‘3N+1’ sequence starting from the integer value that wasreceived (entered by the user). The function must also count and return the numbers that the sequence consists of. The returned value must be displayed from the main() function.
Example input and output is given in the following image.
Knowledge Booster
Similar questions
- Given a positive integer, N, the ’3N+1’ sequence starting from N is defined as follows:If N is an even number, then divide N by two to get a new value for NIf N is an odd number, then multiply N by 3 and add 1 to get a new value for N.Continue to generate numbers in this way until N becomes equal to 1For example, starting from N = 3 the complete ’3N+1’ sequence would be:3, 10, 5, 16, 8, 4, 2, 1Write code to ask the user to enter a positive integer (N) in the main() function. Write a function sequence()that receives the integer value N and display the ‘3N+1’ sequence starting from the integer value that wasreceived (entered by the user). The function must also count and return the numbers that the sequenceconsists of. The returned value must be displayed from the main() function.arrow_forwardSuppose L 1 is represented by 0*(0* 1*) and L 2 is represented by (0 1)*0*. Justify youranswers to the following true or false questions:a) L1 subset L 2,b) L2 subset L 1,c) L1 = L 2arrow_forwardWrite a program that replaces all occurrences of a word with anew word in all the files under a directory, recursively. Pass the parameters fromthe command line as follows:java Exercise18_31 dirName oldWord newWordarrow_forward
- 2022-02-14 20:52:35 Write the primitive counting results for your 40 sheep: 40= For example, the answer for "14" would be (1,1,1,0), meaning that the first division of sheep had none left over, but every other division of sheep had a leftover sheep. Include the commas and the parentheses in your answer.arrow_forwardFor the following question, use the mapping given in the table. A B C D E F G H I J S Z D U F R B K L M K L M N O P Q R S T J V G N A Q O Y W I U V W Z Y Z E C T H P X Encrypt the following: Pure calculation is, in its way, the poetry of logical way of thinking. Decrypt the message with shift of 3. Open WindowDecrypt the message with shift of 25. Openarrow_forwardWrite a program that takes in two positive integer inputs, m and n, and prints out a neatly formatted table of size m * n of all integers between 1 and m in n rows. For example; m = 3, n = 4 1 2 3 1 2 3 1 2 3 1 2 3 Please help me with this problem using c++.arrow_forward
- Given the following function, what happens if a[] contains just one element that doesn't match val? int binarySearch(int a[], int first, int last, int val){ if (first > last) return -1; int middle = (first + last) / 2; if (a[middle] == val) return middle; if (a[middle] < val) return binarySearch(a, middle+1, last, val); else return binarySearch(a, first, middle-1, val);} Group of answer choices binarySearch never calls itself again and terminates (recursion never happens) binarySearch calls itself once then terminates (recursion happens once) binarySearch calls itself twice then terminates (recursion happens twice) binarySearch calls itself 3 times then terminates (recursion happens 3 times)arrow_forwardusing c++ Given a positive integer, N, the ’3N+1’ sequence starting from N is defined as follows:If N is an even number, then divide N by two to get a new value for NIf N is an odd number, then multiply N by 3 and add 1 to get a new value for N.Continue to generate numbers in this way until N becomes equal to 1For example, starting from N = 3 the complete ’3N+1’ sequence would be:3, 10, 5, 16, 8, 4, 2, 1Write code to ask the user to enter a positive integer (N) in the main() function. Write a function sequence()that receives the integer value N and display the ‘3N+1’ sequence starting from the integer value that wasreceived (entered by the user). The function must also count and return the numbers that the sequenceconsists of. The returned value must be displayed from the main() function.arrow_forwardWrite a program Minesweeper.java that takes three integer command-line arguments m, n, and k and prints an m-by-n grid of cells with k mines, using asterisks for mines and integers for the neighboring mine counts (with two space characters between each cell). To do so, Generate an m-by-n grid of cells, with exactly k of the mn cells containing mines, uniformly at random. For each cell not containing a mine, count the number of neighboring mines (above, below, left, right, or diagonal)arrow_forward
- What is the value of the following expressions (or ERROR)?1. __________ s.length()2. __________ t.length()3. __________ 1 + a4. __________ a.toUpperCase()5. __________ "Tomorrow".indexOf("r")6. __________ "Tomorrow".lastIndexOf('o')7. __________ "Tomorrow".substring(2,4)8. __________ (a.length() + a).startsWith("a")9. __________ s = = a10. __________ a.substring(1,3).equals("bc")arrow_forwardPlease help me Josephus Problem is a theoretical problem related to a certain counting-out game. On thiscase, people are standing in a circle waiting to be executed. After a specified number ofpeople are skipped, the next person is executed. The procedure is repeated with theremaining people, starting with the next person, going in the same direction and skippingthe same number of people, until one person remains, and is freed.Arrange the numbers 1 , 2, 3 , ... consecutively (say, clockwise) in a circle. Now removenumber 2 and proceed clockwise by removing every other number, among those thatremain, until one number is left. (a) Let denote the final number which remains. Find formula for .(b) If there are 70 people, what is the safe number (the number that remains)?arrow_forwardwrite a program in C++ Fibonacci NumbersThe Fibonacci numbers are defined by the sequencef1 = 1f2 = 1fn = fn-1 + fn-2Reformulate that as5t1 = 1;t2 = 1;nextTerm = t1 + t2;After that, discard t1, which is no longer needed, and set t1 to t2 and t2 to nextTerm.Repeat fnew an appropriate number of times. Implement a program that given a numberN, prints the first N numbers in the Fibonacci sequence. For example, given the number8, the output isThe Fibonacci sequence: 1, 1, 2, 3, 5, 8, 13, 21arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education