[Solved] Algorithm Homework2-Base Conversion

$25

File Name: Algorithm_Homework2-Base_Conversion.zip
File Size: 329.7 KB

SKU: [Solved] Algorithm Homework2-Base Conversion Category: Tag:
5/5 - (1 vote)

Write a program to convert a whole number specified in any base (216) to a whole number in any other base (216).Digits above 9 are represented by single capital letters: 10 by A, 11 by B, 12 by C, 13 by D, 14 by E and 15 by F.InputThe input consists of three values:a positive integer indicating the base of the numbera positive integer indicating the base we wish to convert tothe actual number in the firs base that we wish to convert to the second base (this number will have lettersrepresenting any digits higher than 9; it may have invalid digits)OutputThe output should consist of the original number followed by the string base, followed by the original base number,followed by the string = followed by the converted number, followed by the string base followed by the new base.If the original number is invalid, output the statement:originalValue is an illegal base originalBase numberwhere originalValue is replace by the value to be converted and originalBase is replaced by the original basevalue.Example 1Input:2 10 10101Output:10101 base 2 = 21 base 10Example 2Input:5 3 126Output:126 is an illegal base 5 numberExample 3Input:15 11 A4COutput:A4C base 15 = 1821 base 11Page 1/2Big- or Little- EndianAll computer memory is organized into 8-bit bytes. For integer values that require a type with more than 8-bits, such as thetypical 2-byte, 4-byte, and 8-byte integral types available on most modern hardware, there is no universal agreement as tohow to order those bytes and two incompatible storage schemes exist.The first stores integers as groups of consecutive 8-bit bytes with the least significant byte occupying the lowest memoryaddress within the group and the most significant byte occupying the highest memory address.The second is just the reverse; the least significant byte is stored in the highest memory address within the group, and themost significant byte is stored in the lowest memory address.Those schemes have been dubbed Little Endian and Big Endian, respectively.We assume that signed integers are stored using twos complement representation.When binary integer data is shared between a Little Endian and Big Endian machine, a data conversion must be performedwhich involves reversing the bytes of the data. Once the bytes have been reversed the integer is then correctly interpretedby the hardware as the original value from the opposite-endian machine. Your task is to write a program that will read a listof integers and report the integers that the binary representations of the input integers would represent on an oppositeendian machine.InputA number n , -2147483648 <= n <= 2147483647 .OutputThe number n followed by the phrase converts to followed by the other endian-value (single space between the threeparts).Example 1Input:123456789Output:123456789 converts to 365779719Example 2Input:-123456789Output:-123456789 converts to -349002504Example 3Input:1Output:1 converts to 16777216Example 4Page 2/2Input:16777216Output:16777216 converts to 1Page 1/2Chess ChampionshipIn the World Chess Championship of 2035, 2^N contestants were supposed to participate. Because of sudden turn of thehurricane Alice, many flights were canceled last minute and many of the contestants could not arrive on time to participatein the championship.With all the matches pre-arranged ahead of time, the organizer decide to keep the schedule as is and allow walkovermatches in case when one of the players is missing.If both players are available, then there will be a normal match.If only one player is available, then this is a walkover match and the player automatically advances to the next level.If no player is available, then there is no match.In the figure on the right, the players 3 and 4 could not arrive.Their match is canceled. Players 1 and 2, play their regular matchand the winner advances to the next level match. But there is nosecond player there, so it is a walkover match.Given the list of players who could not arrive on time, calculatethe number of the walkover matches in the whole championship.InputEach test begins with two integers: 1 <= N <= 10 and0 <= M <= 2^N . 2^N is the number of players scheduled forthe championship. M is the number of players who could notarrive on time due to the canceled flights.The next line contains M integers, denoting the players whohave not arrived. The players are numbered 1 to 2^N .OutputThe number of walkover matches.Example 1Input:2 23 4Output:1Example 2Input:3 51 2 3 4 5Output:2Example 3Input:2 12Output:Page 2/21Page 1/2Negative baseHave you ever heard of base-2 (binary) numbers?Well, of course you did!!! You are a computer scientist.How about base-(-2) numbers? Yes, you read it right we are talking about a base that is negative!!!An integer n writte in in base-(-2) is a sequence of bits ( b_i ), written right to left. Each of which is either 0 or 1 (nonegative bits are allowed) and the following must hold:n = b_0 + b_1 * (-2) + b+2 * (-2)^2 + b_3 * (-2)^3 + Every integer in base-10 has a unique representation in base-(-2) and no negative signs are ever needed.Your task is to write a program that given a number in base-10 finds its base-(-2) representation.InputThe number in base-10, -1,000,000,000 <= N <= 1,000,000,000.OutputThe corresponding number in base-(-2) with no leading zeros.Example 1Input:1Output:1Example 2Input:7Output:11011Example 3Input:-2Output:10Example 4Input:0Output:0Page 2/2Page 1/1Splitting NumbersWe define the operation of splitting a binary number n into two numbers a(n) and b(n) as follows. Let0 <= i1 < i2 < < ik be the indices of the bits (with the least significant bit having index 0) in n that are 1.Then the indices of the bits of a(n) that are 1 are i1 , i3 , i5 , and the indices of the bits of b(n) that are 1 arei2 , i4 , i6 , For example, if n is 110110101 in binary then, again in binary, a = 010010001 and b= 100100100 .InputThe input consists of a single integer n between 1 and 2^31 1 written in standard decimal (base 10) format on asingle line.OutputThe output consists of a single line, containing the integers a(n) and b(n) separated by a single space. Both a(n)and b(n) should be written in decimal format.Example 1Input:6Output:2 4Example 2Input:7Output:5 2Example 3Input:13Output:9

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] Algorithm Homework2-Base Conversion
$25