Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In my computer assignment one of the requirements in converting between the base number systems is to not use any loops? How would you convert an octal integer to a binary without a while loop?
Edit: I can receive the input as a string or as an int in c++. Whichever is more feasible is fine. I.e. I can ask to enter an octal number. Enter an octal number. Then set that input to whatever i need in order to make the conversion to print a decimal number work. Where im getting hung up on is how to accomplish a conversion without using any form of loop. Additionally I need to be able to convert to hexadecimal as well but I hope if I'm able to figure out this step that I'll be able to code the rest myself.

What I have tried:

I've tried using if statements but im not sure how to decide how many to use for each conversion.
Posted
Updated 12-Jul-20 22:53pm
v2
Comments
Rick York 13-Jul-20 2:06am    
Define "an octal integer" and "a binary." This is important because you have not identified the terms of the conversion. If this is two pieces of binary data then there is no conversion necessary. If one is a string and the other isn't that is different than if both are strings. It is very, VERY important to be precise in one's definitions and terminology because misinterpretations can lead to big mistakes.
Patrice T 13-Jul-20 2:10am    
Show your work to get help.
CPallini 13-Jul-20 2:11am    
"I've tried using if loops but im getting lost in how to know how many to use."
There is no such a thing. Possinly you tried to use if chains.
BTW you could try using a switch statement.

Well, the basic unit of an octal number is the digit - which is always the same length: three bits: 000, 001, 010, 011, 100, 101, 110, 111 - eight possible values, hence the name "octal".

So write a simple function that takes an octal digit and converts it to binary - there are loads of different ways to do that: chained ifs, a switch, or (most efficient) an array of strings are the three that spring to mind, in increasing efficiency. Test that, and when you have it working move on to extracting the digits from the integer: that's also simple as C++ has Shift (>> and <<) operators, and a masking AND operator &
So to extract the least significant digit you would AND the number with 7 (or 111 binary) to discard all other digits.
To extract the next least significant digit you shift it right three places (to discard the least significant digit) and AND it with 7
The next one is a right shift of six, and another AND.
And so on for the the other digits.
Since you know how many octal digits are in your number - the size of an integer sets that - you can call the function you created earlier several times, extracting each digit as you go without using a loop.

Give it a try: it's easier than it sounds!
 
Share this answer
 
v2
I have to ask: did your lectures already conver recursion? Because if it did, then that is the only way I can think of how to do this without a loop.

If it didn't, then I think the constraints you posted are not entirely correct, since any special function or syntax that you could use would involve an internal loop at the very least.

Here's a rough outline in pseudo code for a recursive approach:

C++
string convert_char_to_bin(char octal_char)
{
   // see solution 1
}
string remove_first(string my_string)
{
   // remove first char and return remainder of my_string
}
string convert_octal_to_binary(string octals) {
   return octals.empty() ? "" : convert_char_to_bin(octals[0] + convert_octal_to_binary(remove_first(octals);
}

Technically this would fulfil the conditions, although using the '?' ternary operator may not have been something your tutor wanted to see either.
 
Share this answer
 
v2
Comments
Ryan Thomsen 13-Jul-20 22:50pm    
This is from the first assignment of my class. I have been told:
1. No "if" statements.
2. No "while" statements.
3. No "for" statements.
4. No "do" statements.
5. No "switch" statements.
6. No "conditional" statements.

I assume based on the original wording recursion would not be allowed as it is looping statements but i will ask.
Stefan_Lang 14-Jul-20 4:42am    
If this is really your first assignment, I doubt your tutor wanted you to write a recursive solution - especially since you would normally need an 'if' just to check for the initialization of the recursion (input without any octals in it). Which reminded me that my pseudo code was missing just that check, so I updated my solution to include it, and used the '?' ternary operator to avoid the need for an 'if'.

However, I suspect your tutor wanted to see something different, and your best bet is to check the notes from your lecture(s): it should feature whatever technique your tutor had in mind.

The only 'first assignment level' solution I can think of, is unroll the loop into a sequence of steps.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900