Before you read ahead or try to help, this question is regarding my homework so the requirements to this question will be very specific.
I am writing a code that takes a user input between 0 and 511 and converts it into a binary number. Then the program will replace all the 1's in the binary number with T and all the 0's in the number as H. Afterwards it will print out the results (the binary number with the H and T replacement) as a 3*3 matrix.
This is the desired output (not what I have but what I want):
Enter a number between 0 and 511: 299
The binary number is: 100101011
The matrix is:
THH
THT
HTT
The first problem I feel like is a bit more simple, but the printout of my array of the H and T all print out in one single line and I cannot figure out a way to print them 3 values at a time. I have tried to do when i = 3 add a line break but that didn't seem to work. The second problem with my code is that for some odd reason the first H or 0 in the example that I put above shows up as TTH THT HTT while it should be THH THT HTT
Although this is in the C++ language, what I have learned is the C style syntax (no std:: kinds of code or stuff like that because I haven't learned it yet and I will not understand it) So far I have learned basic arrays, loops, and functions.
#include <iostream>
using namespace std;
int main(){
int arr[10];
string stringArr[10];
int input, i;
cout<<"Enter a number between 0 and 511: ";
cin>> input;
for(i = 0; input > 0; i++){
arr[i] = (input % 2);
input = input / 2;
}
cout<<"The binary number is: ";
for(i = i - 1; i >= 0; i--){
cout<<arr[i];
}
for(int i = 0; i < sizeof(arr)/sizeof(arr[10]); i++){
if(arr[i] == 1){
stringArr[i] = "T";
}
else if(arr[i] == 0){
stringArr[i] = "H";
}
}
cout<<" "<< endl;
for(int i = 0; i < sizeof(stringArr)/sizeof(stringArr[10]); i++){
cout<<stringArr[i]<< " ";
}
}