Question: In this assignment we are going to somewhat step away from our Byte class for a moment and take a look at recursion. In the

In this assignment we are going to somewhat step away from our Byte class for a moment and take a look at recursion. In the Byte class you have functions called setValue and bitsToIn that use a loop to compute a value from the bits. These functions look like this:
void Byte::setValue(int val)
{
int mask =1;
for (int i =0; i <8; i++)
{
if (val & mask)
{
this->set(i,1);
}
else
{
this->set(i,0);
}
mask = mask <<1;
}
return;
}
int Byte::bitsToInt() const
{
int val =0;
int mask =1;
for (int i =0; i <8; i++)
{
if (this->at(i)==1)
{
val += this->at(i)* mask;
}
mask = mask <<1;
}
return val;
}
Your task is to write these as recursive functions but DO NOT use them in your Byte project. Simply create a main.cpp and two functions below it. You should pass an array to these functions. In other words, your function prototypes should look something like this:
int bitsToInt(int ar[]);
void setValue(int ar[], int value);
The setValue function will simply fill the array with the correct bits. As an example, if value is 7 you should fill the array with 00000111
The bitsToInt function will do the exact opposite. It will take the array of bits and covert it to a value. As an example, if array has 00000111 the function will return 7.
Write some simple code in main to test your recursion.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!