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 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.
#include <iostream>
#include <stdlib.h>
#include <string>
#include <cstdlib>
usingnamespace 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
}
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.