0

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]<< " ";

        }

}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • *no std:: kinds of code or stuff like that because I haven't learned it yet and I will not understand it* -- You are using `std::string`. Given that, maybe you should write this in C and use `printf` instead of C++. The C++ solution would simply be `std::bitset<9>` and a two line `for` loop. – PaulMcKenzie Apr 09 '21 at 05:51
  • @PaulMcKenzie I am actually not sure what this language actually is. The class I am taking the teacher said it is C++ but the C++ resources that I see online have this std:: things and I don't know what they are because I haven't learned them in class. Which is why I put C++ and that disclaimer there –  Apr 09 '21 at 05:57
  • @G33KTR0N using namespace std; should be an hint you are already using the C++ standard library – Alessandro Teruzzi Apr 09 '21 at 06:39
  • if the syntax confuses you then have a look at [this post](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), you will find list of C++ book pick any beginner book and start reading. this will answer most of your questions – ma1169 Apr 09 '21 at 06:46
  • @G33KTR0N As to your concern for being confused, [here is your program using the "std" stuff](http://coliru.stacked-crooked.com/a/58db11dadaf3d1c6). A `bitset` is a class that -- stores bits, just as the description states. To set the bitset, you -- set it using `=`. Then to get the string version of what you set -- you call `to_string`. So to be honest, a beginner would more than likely be confused with your code than the code that uses `bitset`. – PaulMcKenzie Apr 09 '21 at 12:54

1 Answers1

0

Regarding the formatting in three lines you can do something like:

        if (i%3 == 0 && i != 0) cout << std::endl;
        cout<<stringArr[i]<< " ";

% is the modulus operator that return the reminder of the integer division, you want to add an end line every three (skipping, I am assuming, the first one)

std::endl is adding an end line of the stream (standard output in this case) and it flushes the stream as well. Adding the line without flushing can be done with '\n' instead.

On your second issue, please note, that you are writing the two outputs in different direction. The binary number is printed from the end to the beginning ( looping like this (i = i - 1; i >= 0; i--) ) and the string is printed looping from 0 to the end of the array.

Not asked but I need to pointed it out

You use the std::string as an array of strings, which is really not needed in your case. You can simply use a single std::string and add to it single characters (literal strings are using double quotes eg. "foo" characters single quote eg. 'H').

Alessandro Teruzzi
  • 3,918
  • 1
  • 27
  • 41