Binary matrix building | Sololearn: Learn to code for FREE!

+1

# Binary matrix building

Hello everyone, I have bad english, but I hope you will understand my problem. I have a task to create algorithm to build binary matrix for N variables, using only "for" loops and dynamic arrays. For example: We ask user to enter the N variable User enters 3 Program displays: 1 1 1 1 1 0 1 0 1 1 0 0 0 1 1 0 1 0 0 0 1 0 0 0 I hope you get it. I have my own code, but it always get an error in VS. Here the link on my code https://code.sololearn.com/c8Q01ipCY76o/#cpp

12/16/2019 11:21:27 AM

Artem

+2

Basically 3 is equal to number of digits used to display the output which is equal to number of bits used to represent a number in binary. So if the user enters 4 how many values to show? You know total possible values that can be shown using N bits is equal to 2^N-1(2 to the power of N, -1 because counting starts from 0) So find out total number of output values to display, run a loop starting from 0 to total values(or in reverse) in increment/decrement of 1, convert each number to it's binary equivalent and display.

+2

Unnecessarily making it complex and quite an unusual way to convert to binary. And why are you allocating an extra column(n+1). Alsk at the end you are not freeing the memory. The program is crashing because 'i' is incremented twice. For n > 3 When the j value is 2 or 1 your 'i' is going out of bounds. Why don't you fill the matrix horizontally instead of vertically?

+2

I know you are trying to implement using truth table but it is unnecessary and it will not give you the values in order. See the output for n = 3. Your 1 1 1 appears at 5th (0 based) row. Also include cmath header, your compiler might be including it indirectly but the code does not compile without including it at sololearn.

+1

Truly, it is a truth table for Boolean function, and the right column enters the user. There will be the 0 and 1 for users function. So, why don't fill matrix horizontally? I don't know how XD. Btw thanks for answering.

+1

For horizontal filling start at row = 0 now when you calculate the binary by repeated division you know you get the answer in reverse, so start filling from column n-1. Do you really need to store the results(binary values)? If not then you just need 1D array int main() { cout << "Variables count?" << endl; int n, ** matr; cin >> n; int m = pow(2.0, n); int *b = new int [n]; for(int i=0; i<m; ++i){ int j=i; int id=n-1; while(j > 0){ b[id] = j%2; j /=2; id--; } for(int k=0; k<n; ++k){ cout << b[k] << " | " ; } cout << endl; } delete [] b; return 0; }

+1

Thank you, u really helped me

+1

Your code is Ok you just miss to include cmath header and thats all Just #include <cmath> On the top of your code and it will work perfectly

0

Share your code here.

0

Yes, I added a code, so you can see that program works for n≤3, but for bigger n it crashes because of some memory troubles. I can't solve this for 3 days, so I have to ask for help.

0

It's look good, but I need whole matrix, coz I build sknf/sdnf using this matrix. I thought I should tell this all from the start, so sorry for it. But anyway thanks for help, think I can do smth with your advices.

0

You allocate the memory like you are doing currently (need just n not n+1) Then in while loop do b[i][id] remove the for loop for displaying. Display the matrix at end like you are doing currently. Don't forget to release the memory.

0

Artem You are Welcome ☺👍🙏

0

سلام

0

https://code.sololearn.com/cCiM14BEFdnK/?ref=app

0

MASTER KILLER Do not post unrelated code in someone's else thread. This is spamming refrain from such activities in future ! What do you people gain by posting random nonsense in random thread ?