Docsity

CSCI 1100 Problem Solving in C++: Binary Number Operations, Study Guides, Projects, Research of Computer Science

Toggle sidebar
bg1
CSCI 1100 RPI
Problem Solving in C++ Fall 2006
Select one problem.
Problems are graded on a 0−
1
2
−1scale.
Code must be submitted according to the submission guidelines.
Any written work must be submitted in class on the due date.
Write your name, RIN, RCS id and section # on your work, and staple written work together.
Decimal to Binary Conversion
Don’t forget: Name your functions and files exactly as the problem asks. In this pro ject you must create
a header file binary.h which will contain the definitions of any classes, member functions or functions required
for your project. You only need to submit the header file binary.h and a README file with pseudo-code.
Decimal numbers (eg. 1234) will be represented as a sequence vector <int >. Similarly a binary number
(eg. 1010) is also a sequence vector <int >of zeros and ones.
Create a class binary with one member variable number of type vector <int >which will store a non-
negative binary number. Add two constructors: the default; and, one taking a single vector <int >input
parameter which initializes number. If number is empty, it corresponds to 0. The problems ask you to add
member functions to this class. You should write a driver program to test your functions with inputs for
which you know the answers. A sample driver program will be posted on the website.
1. [10 pts] Add a member function mult2 which multiplies number by 2.
2. [20 pts] Add a recursive void member function add one which adds 1 to number.
[Hint: if the last digit of number is 0, then add one replaces it with 1. If number is empty, then add one
makes it a vector containing only 1. Otherwise, add one should remove the last digit, add one to
the remaining number and then add back a 0. (Try to understand the reasoning – go back to the
high-school algorithm for adding.)]
3. [30 pts] Add a recursive member function add which takes a binary input and adds it to number. Thus
a.add(b) increments the binary number in aby the binary number in b.
[Hint: to add two binary numbers, first add the last digits. If they add up to less than 2, then you pop
off the last digits, and add the remaining two binary numbers. Then you add back the digit sum of the
last digits. If the last digits add to 2, then you pop off the last two digits, and add the remaining binary
numbers. You then add 1 to this result (the previous problem may be helpful) and finally append a 0
to the result of adding 1. (Try to understand the reasoning.)]
4. [40 pts] Add a member function mult10 which multiplies by 10.
[Hint: What binary number does 10 correspond to. Multiplying by 10 then corresponds to adding two
specific numbers related very closely to number. The previous problem may then be helpful.]
5. [50 pts] Add a recursive member function convert which takes as input a vector <int >corresponding
to a decimal number, and replaces number with the binary equivalent of the input. So, a.convert(b)
will replace a.number with the binary equivalent of b.
[Hint: let the decimal number digits be dk, dk−1,...,d2, d1, d0. Let bm,...,b0be the binary equiva-
lent of dk, dk−1,...,d2, d1and cr,...,c0the binary equivalent of d0. Then the binary equivalent of
dk, dk−1,...,d2, d1, d0is related to bm,...,b0and cr,...,c0. The previous problems will be useful.]
1

Partial preview of the text

Download CSCI 1100 Problem Solving in C++: Binary Number Operations and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

CSCI 1100 RPI

Problem Solving in C++ Fall 2006

Select one problem. Problems are graded on a 0 − 12 − 1 scale. Code must be submitted according to the submission guidelines. Any written work must be submitted in class on the due date. Write your name, RIN, RCS id and section # on your work, and staple written work together.

Decimal to Binary Conversion

Don’t forget: Name your functions and files exactly as the problem asks. In this project you must create a header file binary.h which will contain the definitions of any classes, member functions or functions required for your project. You only need to submit the header file binary.h and a README file with pseudo-code. Decimal numbers (eg. 1234) will be represented as a sequence vector < int >. Similarly a binary number (eg. 1010) is also a sequence vector < int > of zeros and ones. Create a class binary with one member variable number of type vector < int > which will store a non- negative binary number. Add two constructors: the default; and, one taking a single vector < int > input parameter which initializes number. If number is empty, it corresponds to 0. The problems ask you to add member functions to this class. You should write a driver program to test your functions with inputs for which you know the answers. A sample driver program will be posted on the website.

  1. [10 pts] Add a member function mult2 which multiplies number by 2.
  2. [20 pts] Add a recursive void member function add one which adds 1 to number. [Hint: if the last digit of number is 0, then add one replaces it with 1. If number is empty, then add one makes it a vector containing only 1. Otherwise, add one should remove the last digit, add one to the remaining number and then add back a 0. (Try to understand the reasoning – go back to the high-school algorithm for adding.)]
  3. [30 pts] Add a recursive member function add which takes a binary input and adds it to number. Thus a.add(b) increments the binary number in a by the binary number in b. [Hint: to add two binary numbers, first add the last digits. If they add up to less than 2, then you pop off the last digits, and add the remaining two binary numbers. Then you add back the digit sum of the last digits. If the last digits add to 2, then you pop off the last two digits, and add the remaining binary numbers. You then add 1 to this result (the previous problem may be helpful) and finally append a 0 to the result of adding 1. (Try to understand the reasoning.)]
  4. [40 pts] Add a member function mult10 which multiplies by 10. [Hint: What binary number does 10 correspond to. Multiplying by 10 then corresponds to adding two specific numbers related very closely to number. The previous problem may then be helpful.]
  5. [50 pts] Add a recursive member function convert which takes as input a vector < int > corresponding to a decimal number, and replaces number with the binary equivalent of the input. So, a.convert(b) will replace a.number with the binary equivalent of b. [Hint: let the decimal number digits be dk, dk− 1 ,... , d 2 , d 1 , d 0. Let bm,... , b 0 be the binary equiva- lent of dk, dk− 1 ,... , d 2 , d 1 and cr ,... , c 0 the binary equivalent of d 0. Then the binary equivalent of dk, dk− 1 ,... , d 2 , d 1 , d 0 is related to bm,... , b 0 and cr ,... , c 0. The previous problems will be useful.]