I need HELP!

This assignment is due today and I am just stumped on where I should even begin with it, any help would be appreciated.

The program asks a user to enter a number that is used as the total binary digits (0s and 1s)
If a user enters 3, for example, the program prints eight 3 binary digit
numbers (2^3= 8)on the console (Command Prompt)in the ascending order.
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1

If a user enters 4, then sixteen 4 binary digit binary numbers (2^4= 16)
are displayed.

The program then asks the user whether to continue or not. If the user enters n, the program ends. If the user enters any
other key, the program repeats.

Make the program generic in such a way that the 1s and 0s can be replaced
with a special character (e.g., x and o)

You can start by writing the hello-world program, and instead of displaying "hello world" you display "Enter the number of binary digits: " instead.

Have you learned how to print numbers in binary? If you have, this program will be easy for you.
I can do that I'm just confused on how I will display all the rows of numbers
If you can print numbers in binary then I don't know why displaying the rows of numbers confuses you ;)
This is what I have
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

int main() {

    int numberOfRows,numberOfCols,binaryn,matrix;

    cout << "Enter the number of binary digits: " << endl;
    cin >> binaryn;

    numberOfRows= 2^binaryn;
    numberOfCols= binaryn;

    for(int i=0; i < numberOfRows; i++){
        for(int j=0; j < numberOfCols; j++){
            if (i % numberOfRows / 2^j < numberOfRows / 2^(j+1))
            {
                matrix[i][j]=0;
            }
            else{
                matrix[i][j]=1;
            }
        }
    }
}


But I keep getting errors on lines 18,21 where the matrix is, it says error invalid types int[int] for array subscript
You do not need a matrix.
idk what else I can do
You said you knew how to print a number in binary, all you have to do for this assignment is that same thing but with spaces between the digits. You do not need a matrix - you do not need to store the digits anywhere.
Please study this code and ask questions about what you do not understand.
I know spoonfeeding is bad, but the route you're taking doesn't make sense (trying to use subscripts with single variables instead of arrays), so I thought i'd throw you a bone here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <stdlib.h> 
#include <string>
#include <cstdlib>
using namespace std;

int main() {

    int numberOfRows,numberOfCols, binaryn;
	string IndividualLine;
	char CharToReplace0 = 0; //Initialize a char to hold the character to replace the 0's
	char CharToReplace1 = 0; //Initialize a char to hold the character to replace the 1's

    cout << "Enter the number of binary digits: " << endl;
    cin >> binaryn;

    numberOfRows= pow(2,binaryn); //Calculates 2 to the power of binaryn
    numberOfCols= binaryn;

	cout << "Enter the character to replace the 0's with:";
	cin >> CharToReplace0;

	cout << "Enter the character to replace the 1's with:";
	cin >> CharToReplace1;

	for (int i=0;i<numberOfRows;i++)
	{
		char buffer[256]; //This buffer will hold the binary version of the number
		itoa(i,buffer,2); //This converts the number we're at in our for loop (i) to a binary number (base 2) and copies the contents into the char buffer
		int index=0; //Initialize an index for going through and replacing the 1's and 0's
		int CountDigitsFilled=0; //This variable will count how many digits are filled, and fill in the necessary zero's to line up our binary numbers
		while (buffer[index] != '\0') //as long as we're not at the null terminator ( end of the string )
		{
			if (buffer[index]=='1') //If number is a 1, add our CharToReplace1 to the string
			{
				IndividualLine+=CharToReplace1;
				CountDigitsFilled+=1;
			}
			if (buffer[index]=='0') //If number is a 0, add our CharToReplace0 to the string
			{
				IndividualLine+=CharToReplace0;
				CountDigitsFilled+=1;
			}

			index+=1; //Moves index up to the next character to check in our char array
		}
		if (CountDigitsFilled<numberOfCols) //If we don't have enough digits, we need to add 0s to line it up right
		{
			for (int i=CountDigitsFilled; i<numberOfCols; i++)
			{
				IndividualLine = CharToReplace0+IndividualLine;
			}
		}
		cout << IndividualLine << endl; //Displays our converted string
		IndividualLine = ""; //Clears our line for the next print.
	}
     return 0; //end of program
}
Last edited on
Alright I think I understand it, thanks alot!
Pindrought: your solution is overly complex ;)
@LB: I don't know of a simpler way to do it.
Hi, I think it is too complicated.
I don't usually do this, but since a solution has been posted, I'll show my solution:
vinnyo assignment wrote:
The program asks a user to enter a number that is used as the total binary digits (0s and 1s)
If a user enters 3, for example, the program prints eight 3 binary digit
numbers (2^3= 8)on the console (Command Prompt)in the ascending order.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main()
{
    unsigned digits;
    std::cin >> digits;
    for(unsigned i = 0; i < (1 << digits); ++i)
    {
        for(unsigned j = digits; j > 0; --j)
        {
            std::cout << ((i >> (j - 1)) % 2) << " ";
        }
        std::cout << std::endl;
    }
}
http://ideone.com/J5URU4
Last edited on
Wow LB, i've never seen anything like that. Thanks for posting.
Topic archived. No new replies allowed.