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
To keep your computer working efficiently, it is a good idea to _____ unnecessary files. delete defragment save archive
Korolek [52]

Hey the answer to this is delete or remove. because it is a good idea to remove old files that your not using.

Hope this helps

-scav

3 0
2 years ago
Write a program that calculates the average rainfall for three months. The program should ask the user to enter the name of each
sergeinik [125]

Answer:

// program in C++.

#include <bits/stdc++.h>

using namespace std;

// main function

int main()

{

   // string array

   string m[3];

   // array to store rainfall

   double rainfall[3];

   // variables

   double avg_rainfall,sum=0;

   

   for(int i=0;i<3;i++)

   {

       cout<<"Enter name of month "<<i+1<<" :";

       // read month name

       cin>>m[i];

       cout<<"Enter rainfall (inches)  in month "<<i+1<<" :";

       // read rainfall

       cin>>rainfall[i];

       // sum of rainfall

       sum+=rainfall[i];

   }

   // Average rainfall

   avg_rainfall=sum/3;

   // print Average rainfall

   cout<<"Average rainfall for "<<m[0]<<","<<m[1]<<","<<m[2]<<" is "<<avg_rainfall<<" inches."<<endl;

return 0;

}

Explanation:

Create string array "m" to store name of month and double array "rainfall" to store rainfall. Read name of 3 months and rainfall in that month.Find the sum of all the rainfall  and the average rainfall.Print the average rainfall of 3 months.

Output:

Enter rainfall (inches)  in month 2 :45                                                                                    

Enter name of month 3 :july                                                                                                

Enter rainfall (inches)  in month 3 :43                                                                                    

Average rainfall for may,june,july is 42.6667 inches.

4 0
3 years ago
Select the true statement about HTML. HTML is a language that is used to create Web pages. HTML tags tell a web browser when to
Alexxx [7]

Answer: HTML is a language that is used to create Web pages.

Explanation:

HTML (Hypertext Markup Language) simply refers to the code which is used for the structuring of a web page and its content. e.g the content can be structured by using images, in data tables or within a set of paragraphs.

Therefore, the correct statement about HTML is that HTML is a language that is used to create Web pages.

6 0
3 years ago
Read 2 more answers
Advanced Communications, a leading mobile communication service provider, has a deal with On-the-Go, a chain of several thousand
Stells [14]

Answer:

The answer is "Strategic alliance"

Explanation:

These alliances are an indo-organizational type in which several trading stakeholders decide to put resources, transfer knowledge. It also collaborates in environmentally friendly-nomic price-creating exercises.

  • In this alliances major mobile phone service provider, Advanced Phone has a contract with On-the-Go, a chain of several thousand convenience stores nationwide.
  • It depends on synergy in both the assets and technical expertise to introduce to those of the partnership by any of the trade members, that's why the strategic alliance is the correct answer.

3 0
2 years ago
Which of the following practices can help you avoid harassment (or the appearance of
Advocard [28]

Answer:

B. Respect other users' requests not to send any jokes via e-mail or IM.

7 0
3 years ago
Read 2 more answers
Other questions:
  • How can i complain a rude comment
    7·1 answer
  • Instructions:Drag the tiles to the boxes to form correct pairs.
    11·2 answers
  • Some web and mobile applications allow users to share pictures and videos, as well as chat and message. True False
    5·2 answers
  • When you align or size a group of selected controls, the changes are made relative to the
    14·1 answer
  • What is the Default path for SYSVOL?
    14·1 answer
  • Clicking on the sheet tab with the _______ adds another worksheet to an Excel file.
    9·1 answer
  • What property do we use to distinguish a specific element from a form? value name click this
    12·1 answer
  • Harold included an excerpt from an Internet magazine in an article he wrote for the school newspaper. However, he did not ask pe
    13·2 answers
  • Why are medical coders using encoded software
    10·1 answer
  • How can i fix this, if i log in my acc it keeps saying wrong nickname or pass. even though my email and pass is correct
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!