1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Yakvenalex [24]
3 years ago
10

Function Name: d2x Parameters: int, int Returns: string Description: Write function, d2x, that takes as input a nonnegative inte

ger v (in the standard decimal representation) and an integer x between 2 and 9 and returns a string of digits that represents the base-x representation of v. To convert a value, v from base-10 to base-x, v is divided by x successively until the quotient of v divided by x is 0. On each successive division, the remainder is the new most significant digit in your answer. For the function call, d2x(10, 2), should return '1010':

Computers and Technology
1 answer:
Marina CMI [18]3 years ago
6 0

Answer:

I am writing a Python program. Let me know if you want the program in some other programming language.

#definition of function d2x which takes two parameters v: which is a non #negative integer and x which is an integer between 2 and 9. this method #returns string of digits that represents the base-x representation of v

def d2x(v,x):

   remainder = v%x  

   if v<=1:

       return str(v)

   else:

       return str(d2x(v//x,x)) + str(remainder)

 

v= int(input("Enter a non negative integer: "))

x= int(input("Enter an integer between 2 and 9: "))

print(d2x(v,x))

Explanation:

I will explain the code line by line.

def d2x(v,x) This is the definition of function d2x which takes two parameters v: which is a non negative integer and x which is an integer between 2 and 9. This method returns string of digits that represents the base-x representation of v.

Now lets take an example to understand the working of this function.

Suppose the value of v = 10 and x = 2.

remainder = v%x  takes the mod of v and x which returns the remainder of the division of v by x. So v%x = 10 % 2 = 0. So remainder = 0.

if v<=1 This if condition checks if the value of v is less than or equal to 1. This is false because v=10 and 10 is greater than 1. If v is 1 or less than return str(v)  will return the value of v as it is i.e. 10.

Since the IF condition is false so the else part will execute which has the following statement: return str(d2x(v//x,x)) + str(remainder)

This calls the d2x function recursively. Here v is divided by x successively until the quotient of v divided by x is 0. On each successive division, the remainder is the new most significant digit. In v//x, the double slash is called floor division or integer division which we use as both v and x are integers. So using recursion the above statement becomes:

str(d2x(v//x,x)) + str(remainder)

str(d2x(10//2,2) + str(0)

d2x(5,2) + (0)

Now d2x will again be called recursively to perform the division and this successive division continues until quotient of v divided by x is 0. Here str() method is used returns the result in the form of string.  So v%x is the number which is added to the end of resultant number. v//x is the recursive portion which keeps using the answer of the previous binary number. This means 10 in base 2 calls convert(5,2), then adds 0 at the end. So when v//x=5 and v%x=0 So the answer is 1010.

At the end v= int(input("Enter a non negative integer: "))

x= int(input("Enter an integer between 2 and 9: "))  these two statement wills take integer input from user and print(d2x(v,x)) calls d2x method and print the result.

You might be interested in
Some of y'all make me lose braincells with your questions
boyakko [2]

Answer:

ok done we will try

BTW have a nice day enjoy your day Stay blessed stay happy stay strong

5 0
2 years ago
Read 2 more answers
When writing HTML code, what is meant by 'syntax'?​
aleksandrvk [35]

Answer:

   Syntax is essentially the punctuation and grammar rules for a computer language.

Explanation:

  Certain characters and words have special meanings and must appear in a particular order for the computer code to make any sense. A simple example from line 3 in Figure 2.2 is the piece of HTML code <head>.

8 0
3 years ago
Explain why a sound background in auto electricity and electronics is absolutely necessary if you are to troubleshoot
Fynjy0 [20]

the electrical system important for starting up your vehicle, but it is also important for keeping your vehicle running as you drive it. In addition, it is the electrical system that is also responsible for the functioning of things such as your headlights, your radio, and your dashboard

4 0
2 years ago
Write a program that simulates a fishing game. In this game, a six-sided die is rolled to determine what the user has caught. Ea
m_a_m_a [10]

Answer:

Answered below

Explanation:

//Python code

numberOfCasts = 3

j = 0

fishingPoints = 0

sum = 0

while j < numberOfCasts:

dieNum = randint(1 , 6)

if dieNum ==1:

fishingPoints = 2

elif dieNum == 2:

fishingPoints = 4

elif dieNum == 3:

fishingPoints = 6

elif dieNum == 4:

fishingPoints = 8

elif dieNum == 5:

fishingPoints = 10

elif dieNum ==6:

fishingPoints = 12

sum += fishingPoints

j++

if sum > 30:

print ("Great catch")

elif sum > 10 and sum <= 30:

print("Good catch")

else:

print ("Bad day")

3 0
2 years ago
Which tool would you use if you wanted to arrange a list of words in alphabetical order?
dem82 [27]
Answer:
A
Explanation:
You can quickly and easily sort the text of a bulleted or numbered list in Microsoft's popular Word program so that your text is in alphabetical order. In the Sort Text dialog box, under Sort by, click Paragraphs and Text, and then click either Ascending or Descending.
5 0
3 years ago
Read 2 more answers
Other questions:
  • A ______________ is a document created when planning resource management to help promote teamwork and clarify team communication
    13·1 answer
  • Create a division formula.
    9·1 answer
  • Need deveolpers not software
    10·2 answers
  • What is the key benefit of using RAM in a computer?
    12·1 answer
  • When you use the tilde operator (~ in a url for the attribute of a server control, it represents the _____ directory of the webs
    8·1 answer
  • Anyone wanna play among us UwU code is TAEQPQ
    15·2 answers
  • For this scenario related to turtle drawing, indicate whether it is better to write a loop or a function (or a set of functions)
    13·1 answer
  • What is the output by the code system.out.print(8-4+2);
    13·1 answer
  • 1 pts Question 5 Which of the following calculations would evaluate to 12? (36) + 2/2, 3* ((6+2)/2), 3* 6+2/2, (306+ 2)/2​
    14·1 answer
  • You are planning to depart on a flight from area 2 to area 4 in 12 hours. What weather is forecast to occur along your route?
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!