Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN: 9780133594140
Author: James Kurose, Keith Ross
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
write this in c++ format
function sequence(n){
var n = 10; // this is just for explanation you can take any value from user input
var arr = [n]; //this is to store result
//Continue to generate numbers in this way until N becomes equal to 1
while(n > 1){
//check if even
if(n % 2 == 0){
//If N is an even number, then divide N by two to get a new value for N
n = n / 2;
}
else{
//If N is an odd number, then multiply N by 3 and add 1 to get a new value for N.
n = (n * 3) + 1;
}
arr.push(n);
}
Logger.log("sequesnce: "+ arr)
Logger.log("Number of terms: "+arr.length);
}
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by stepSolved in 2 steps
Knowledge Booster
Similar questions
- code in python program please Write a function that takes one integer parameter, makes sure it is an integer, prints sum of every two digits and return the sum of digits from the number entered by the user up to 1, using recursion.Sample input: 4 Sample output: 4+3 = 77+2 = 99+1 = 10arrow_forwardWrite a function called subsequence() that finds the longest common subsequence of two DNA strands. The function should take two DNA strands (strings) as input and should return the longest common subsequence. Example:subsequence(“AATTCAT”, “CAT”) returns “CAT”subsequence(“CAAAT”, “CAT”) returns “CA”subsequence(“CCG”, “TTA”) returns “”subsequence(“AATGTTACCC”, “AATCTTACCT”) returns “TTACC”subsequence(“AATCTTAGCC”, “AATCAAAGCC”) returns “AATC”If there are multiple subsequences of the same length, then return the firstarrow_forward2. Write a recurrence relation describing the worst case running time of each of the following algorithms and determine the asymptotic complexity of the function defined by the recurrence relation. Justify your solution. You may not use the Master Theorem as justification of your answer. Simplify and express your answer as (nk) or (nk log₂ n) whenever possible. If the algorithm is exponential just give exponential lower bounds. a) function func(A,n) if n ≤ 5 then return A(1) else for i=1 to n for j = i to n-1 A(j) A(j) + A(i) + 3 /* endfor */ /* endfor */ wwwwwwwwww y ← func(A, n-6) return (y)arrow_forward
- Explain in detail how this recursive function works: Program description: The program counts then number of 7's in a number C++ code: #include <iostream>using namespace std;//definig numSevens functionint numSevens(int s){//checking if number is 0if(s==0)//returning 0return 0;//checking if last digit is 7else if(s%10==7)//returning 1 and recursively calling numSevens function for the next digitreturn(1+numSevens(s/10));else//returning 0 and recursively calling numSevens function for the next digitreturn(0+numSevens(s/10));}int main(){//calling numSevens function for a sample value and printing resultcout<<numSevens(47364778)<<endl;return 0;}arrow_forwardWrite a function count_evens() that has four integer parameters, and returns the count of parameters where the value is an even number (i.e. evenly divisible by 2). Ex: If the four parameters are: 1 22 11 40 then the returned count will be: 2 Hint: Use the modulo operator % to determine if each number is even or odd. Your program must define the function:count_evens(num1, num2, num3, num4) python # Define your function here if __name__ == '__main__': num1 = int(input()) num2 = int(input()) num3 = int(input()) num4 = int(input()) result = count_evens(num1, num2, num3, num4) print('Total evens:', result)arrow_forwardcode neededarrow_forward
- In C++: Write a function which will find the average of all elements of the sequence: {2, 4, 6, 8, 10, 12}. Use call by reference, and return the average as the function's return value.arrow_forwardI would appreciate your help. I attached instructions to a C program. Could you copy and paste the code please?arrow_forwardin c++arrow_forward
- Write a function that returns the greatest common divisor of 2 integers.arrow_forwardWrite a c++ program that includes two functions for sorting integers. your program must acts as following: Ask the user to enter 5 numbers and then by calling Asc() and Dsc() functions sort these 5 numbers in ascending and descending orders. Sample output: How many integers you wants to sort? 5 Please enter 5 integers: 5 8 -1 0 2 the ascending order of your list is: -1 0 2 5 8 The Descending order of your list is: 8 5 2 0 -1 Note: to do the above question do not use the array , vector or class. Use swaparrow_forwardin the C++ version please suppose to have a score corresponding with probabilities at the end and do not use the count[] function. Please explain the detail when coding. DO NOT USE ARRAY. The game of Pig The game of Pig is a dice game with simple rules: Two players race to reach 100 points. Each turn, a player repeatedly rolls a die until either a 1 ("pig") is rolled or the player holds and scores the sum of the rolls (i.e. the turn total). At any time during a player's turn, the player is faced with two decisions: roll - if the player rolls 1: the player scores nothing and it becomes the opponents turn. 2 - 6: the number is added to the player's turn total and the player's turn continues. hold - The turn total is added to the player's score and it becomes the opponent's turn. This game is a game of probability. Players can use their knowledge of probabilities to make an educated game decision. Assignment specifications Hold-at-20 means that the player will choose to roll…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY
Computer Networking: A Top-Down Approach (7th Edi...
Computer Engineering
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:PEARSON
Computer Organization and Design MIPS Edition, Fi...
Computer Engineering
ISBN:9780124077263
Author:David A. Patterson, John L. Hennessy
Publisher:Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:9781337569330
Author:Jill West, Tamara Dean, Jean Andrews
Publisher:Cengage Learning
Concepts of Database Management
Computer Engineering
ISBN:9781337093422
Author:Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:Cengage Learning
Prelude to Programming
Computer Engineering
ISBN:9780133750423
Author:VENIT, Stewart
Publisher:Pearson Education
Sc Business Data Communications and Networking, T...
Computer Engineering
ISBN:9781119368830
Author:FITZGERALD
Publisher:WILEY