Using Recursion to Convert Number to Other Number Bases Data Structures in Java with JUnit ©Rick Mercer.

Slides:



Advertisements
Similar presentations
NUMBER SYSTEM. How to convert hexadecimal numbers to decimal numbers? 230 Working from right to left, MULTIPLY each position with 8 raised to the power.
Advertisements

DATA REPRESENTATION CONVERSION.
Converting Numbers Between Bases. While the quotient is not zero…  Divide the decimal number by the new base.  Make the remainder the next digit to.
James Tam Beyond base 10: Non-decimal based number system What exactly is decimal? How do other number systems work (binary, octal and hex) How to convert.
2.2 General Positional-Number-System Conversion
Recursion Chapter 11 Chapter 11.
Unit 191 Recursion General Algorithm for Recursion When to use and not use Recursion Recursion Removal Examples Comparison of the Iterative and Recursive.
1 Section 2.5 Integers and Algorithms. 2 Euclidean algorithm for finding gcd Where L is a larger number, and S is a smaller number, to find gcd(L,S):
Number System Conversions Lecture L2.2 Section 2.3.
Number Systems and Arithmetic
 Binary Binary  Binary Number System Binary Number System  Binary to Decimal Binary to Decimal  Decimal to Binary Decimal to Binary  Octal and Hexadecimal.
Number systems Converting numbers between binary, octal, decimal, hexadecimal (the easy way)
Chapter 16 Binary and Hexadecimal Numbers. §16.2 thru 16.3 – Addition and Subtraction of Binary Numbers Binary = Base 2 Addition and subtraction are similar.
Simple Data Type Representation and conversion of numbers
Numbering Systems CS208.
20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Number Systems What is the Standard Base we
Conversion of Number System Conversion Among Bases The possibilities: Hexadecimal DecimalOctal Binary
NUMBER SYSTEM.
The Wonders of Conversion. A number system is a system in which a number is represented. There are potential infinite number systems that can exist (there.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
1 CS100J 09 March, 2006 More on Loops Reading: Secs 7.1–7.4 A Billion. The next time you hear someone in government rather casually use a number that includes.
When dividing a decimal by a whole number, place the decimal point in the quotient directly above the decimal point in the dividend. Then divide as you.
Examples of Recursion Data Structures in Java with JUnit ©Rick Mercer.
Number systems, Operations, and Codes
Numbering System Base Conversion. Number systems Decimal – 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Binary – 0, 1 Octal – 0, 1, 2, 3, 4, 5, 6, 7 Hexadecimal system.
Number System. Number Systems Important Number systems – Decimal – Binary – Hexadecimal.
Positional Notation 642 in base 10 positional notation is:
Examples of Recursion Data Structures in Java with JUnit ©Rick Mercer.
Number Base Conversions
Octal to Decimal Hexadecimal DecimalOctal Binary.
Positional Number Systems Decimal, Binary, Octal and Hexadecimal Numbers Wakerly Section
Chapter 17 Recursion Data Structures with Java © Rick Mercer Data Structures with Java © Rick Mercer.
DECIMALBINARY a) b) c) d) e) f) Revision Exercise DECIMALBINARY a) b) c)
Binary01.ppt Decimal Decimal: Base 10 means 10 Unique numerical digits ,00010,000 Weight Positions 3,
Codes Octal Power Hexadecimal ASCII BCD Code
Discrete Mathematics Numbering System.
Chapter 2 Number Systems Consists of a set of symbols called digits and a set of relations such as +, -, x, /.
Number Representation Lecture Topics How are numeric data items actually stored in computer memory? How much space (memory locations) is.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Characters and Strings
Week 7 : String and photo processing. Today’s Tasks  Practice list and string  Convert Decimal to any radix base number  Between Binary and Hexadecimal.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
 2012 Pearson Education, Inc. Slide Chapter 4 NumerationSystems.
ECE DIGITAL LOGIC LECTURE 2: DIGITAL COMPUTER AND NUMBER SYSTEMS Assistant Prof. Fareena Saqib Florida Institute of Technology Fall 2016, 01/14/2016.
CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases.
The Hexadecimal System is base 16. It is a shorthand method for representing the 8-bit bytes that are stored in the computer system. This system was chosen.
Number Systems and Binary Arithmetic Quantitative Analysis II Professor Bob Orr.
Chapter 4 Numeration and Mathematical Systems © 2008 Pearson Addison-Wesley. All rights reserved.
Positional Number Systems Decimal, Binary, Octal and Hexadecimal Numbers Wakerly Section
Digital Design Chapter One Digital Systems and Binary Numbers
Recursion DRILL: Please take out your notes on Recursion
Octal to Decimal Decimal Octal Binary Hexadecimal.
Discrete Mathematics Numbering System.
By: Jonathan O. Cabriana
Convert Decimal to Binary
Data Structures in Java with JUnit ©Rick Mercer
Base ‘b’ number for i = 0 to n – 1 for an n digit quantity
Number System conversions
Number Systems and Binary Arithmetic
Introduction to IT By: Muhammed s. anwar.
Number Systems Base 2, 10, 16.
Digital Electronics and Microprocessors
Chapter 2: Number Systems
Dividing a decimal by a whole number
Number systems Converting numbers between binary, octal, decimal, hexadecimal (the easy way)
Converting Numbers Between Bases
Dr. Clincy Professor of CS
Presentation transcript:

Using Recursion to Convert Number to Other Number Bases Data Structures in Java with JUnit ©Rick Mercer

99 10 is also Problem: Convert a decimal (base 10) number into other bases Message Return convert(99, 2) " " convert(99, 3) "10200" convert(99, 4) "1203" convert(99, 5) "344" convert(99, 6) "243" convert(99, 7) "201" convert(99, 8) "143" convert(99, 9) "120"

Multiply Digits by Powers of Base 10, 8, 2, or whatever Decimal numbers: multiply digits by powers of = 9 x x x x 10 0 Octal numbers powers of = 1 x x x x 8 0 = = Binary numbers powers of = 1 x x x x 2 0 = = 13 10

Converting base 10 to base 2 1) divide number (5) by new base(2), write remainder (1) 2) divide quotient (2), write new remainder (0) to left 3) divide quotient (1), write new remainder (1) to left __2_ 2 ) 5 Remainder = 1 __1_ 2 ) 2 Remainder = 0 __0_ 2 ) 1 Remainder = 1 Stop when the quotient is = Place remainders in reverse order

Converting base 10 to base 8 1) divide number by new base (8), write remainder (1) 2) divide quotient (2), write new remainder (0) to left 3) divide quotient (1), write new remainder (1) to left _12_ 8 )99 Remainder = 3 __1_ 8 )12 Remainder = 4 __0_ 8 ) 1 Remainder = 1 Stop when the quotient is = Place remainders in reverse order

Possible Solutions We could either 1. store remainders in an array and reverse it, or 2. write out the remainders in reverse order, or 3. postpone the output until we get quotient = 0 4. store result as a String (like a recursion assignment) Iterative Algorithm while the decimal number > 0 { Divide the decimal number by the new base Set decimal number = decimal number divided by the base Store the remainder to the left of any preceding remainders }

Recursive algorithm Base case if decimal number being converted = 0 do nothing (or return "") Recursive case if decimal number being converted > 0 solve a simpler version of the problem by using the quotient as the argument to the next call store the current remainder (number % base) in the correct place

One solution assertEquals("14", rf.convert(14, 10)); assertEquals(" ", rf.convert(99, 2)); assertEquals("143", rf.convert(99, 8)); assertEquals("98", rf.convert(98, 10)); // 9*10+8 public String convert(int num, int base) { if (num == 0) return ""; else return convert(num / base, base) + (num % base); }

Hexadecimal Convert this algorithm to handle all base up through hexadecimal (base 16) 10 = A 11 = B 12 = C 13 = D 14 = E 15 = F