From Binary to Octal Conversion Help

Hi, nice to meet you all. I'm having some trouble with my homework in my C++ coding class, and needed some help, even a push in the right direction would be appreciated!

My professor gave us this line of code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//PLACE YOUR NAME HERE

#include <iostream>
using namespace std;

int main()
{
	for(int i = 0; i < 2; i++)
		for(int j = 0; j < 2; j++)
			for(int k = 0; k < 2; k++)
				cout << i << j << k << "\n";

	return 0;
}


Then he posed this question:
Type the program above in and run it. This program counts from 000 to 111 in Binary (0 – 7 Decimal) using only digits 0 – 1. A related numbering system is Octal, and uses the digits 0 – 7. Modify the program above to count from 00 to 77 in Octal (0 – 63 Decimal).
Hint: You will only need 2 for loops instead of 3.

I'm a bit behind in my studying in this class, but this assignment will be due before I believe I can figure it out from the book. Can somebody help me understand what I need to type to make this code work?
Thank you!
you need an outer loop, that goes from 0-7 and writes that.
you need an inner loop, that goes from 0-7 as well and write that.

so can you write (easier, from scratch than to modify above mess) 2 loops that go from 0-7 and print the loop variables with an end of line?

for (int L = 0; .... ???)
for (int R = 0; ...???)
cout << L << ?????...

can you fill in the blanks with that starting point?

prefer << endl for end of line instead of "\n". It does the same thing, but endl is the C++ and "cout" way.

Prefer: indent 1 -2 spaces, not 20 ... you run out of room to work with that much indent once you code becomes a bit more complex.

Last edited on
closed account (SECMoG1T)
Plus you'll also require at least one variable and an if statement,

the variable will help you alter the limit to your second loop,
an if statement will help you know when to alter the limit

1
2
3
4
var limit

 if my outer loop get's here
     then change my inner loop {limit} to .... 
Thank you guys. I found out what my teacher was wanting me to do with his question.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main()
{
	for (int i = 0; i < 8; i++)
		for (int j = 0; j < 8; j++)
				cout << i << j << "\n";

	getchar();

	return 0;
}


Taking out the k lines, and replacing the 2's with 8's for octal values gets me just where I need to be. Thank you for the push!
Now, another question:

My instructor presented me with this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//PLACE YOUR NAME HERE

#include <iostream>
using namespace std;

int main()
{
	int i = 0;

	while (i <= 10)
	{
		cout << i << "\n";
	}

	return 0;
}


When ran, it only displays repeated lines of 0's without end.
The instructions are:

"Type the above program and run it. Exit with Ctrl-C. What happens when you run it? How do you fix this program? Using an if statement and continue statement, only cout the even numbers."

Any suggestions on this one? I keep trying different ways to insert and if statement, but can't get a working result, especially with a continue.
closed account (SECMoG1T)

find a way to change the value of i on each iteration.
The loop repeats continually while i is less than 10, however, i never changes.
Incrementing i inside the loop body would be a good start.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main()
{
	int i = 0;

	while (i <= 10)
	{
                ++i; // add one to the value of i
		cout << i << "\n"; 
        }

	return 0;
}


Then, check to see if i is odd, and if it is, skip it.
Last edited on
closed account (48T7M4Gy)
Regarding the use of '\n' and endl there are good reasons why endl is not the way to go.

Read: https://github.com/isocpp/CppCoreGuidelines/issues/357 including final resolution.

I don't know whether there is an efficiency bonus but '\n' not "\n"
Last edited on
Topic archived. No new replies allowed.