Write C++ Program: Create Binary Sequences of Length 15

  • Comp Sci
  • Thread starter semc
  • Start date
  • Tags
    C++
In summary, The conversation discusses writing a program in C++ to generate all binary sequences of a given length. The participants suggest using a loop and arrays to accomplish this task and mention converting ASCII to binary as a possible solution. They also mention the importance of creating a converter function and suggest teaching a six-year-old how to write binary as a helpful exercise. One participant has successfully written the program, but is struggling with printing the correct number of zeros.
  • #1
semc
368
5
Write a C++ program that writes all binary sequences of length 15

Guys i have totally no idea how to go about doing this. Can anyone tell me how i should go about doing this?
 
Physics news on Phys.org
  • #2
Since 2^16 is the smallest integer using 16 binary bits how about writing n in binary with leading zeroes for n from 0 to 2^16 - 1 = 65535? Hopefully you don't have to print them.
 
  • #3
semc said:
Write a C++ program that writes all binary sequences of length 15

Guys i have totally no idea how to go about doing this. Can anyone tell me how i should go about doing this?
Write, by hand, all binary sequences of length 4.

Think about how you did it, devise an algorithm, Then implement it.

If you haven't figured out an algorithm, then try writing all binary sequences of length 5. If you still haven't figured out an algorithm...


Maybe, think about how you would give instructions to a six-year old that would allow them to write down all binary sequences of length 4. (without requiring them to understand what "binary" or "sequence" means)
 
  • #4
Actually i only know some basic command like loop and arrays so am i able to do this base on them? I was thinking is there a way for the computer to convert ASCII into binary then i will just use a loop to get all the sequence?
 
  • #5
The natural place to look would be the printf command. Unfortunately it doesn't have a built in binary converter, so you will have to roll your own. Which is likely the whole point of your assignment.
 
  • #6
LCKurtz said:
The natural place to look would be the printf command. Unfortunately it doesn't have a built in binary converter, so you will have to roll your own.

Not necesarilly - while function converting integers to ascii for a given radix is not a standard one, it is present in most C/C++ implementations.

But I agree that writing your own converter is a good exercise.
 
  • #7
I bet its easier to teach a 6years old to write binary then to ask a comp to do it for me haha. Alright thanks guys i will try to figure out the rest.:biggrin:
 
  • #8
Guys i had written the program and the last term is 1000000000000000. I have one problem though, the question requires me to print 0 as 000000000000000 and the rest of the binary. How do i do that?
 
Last edited:
  • #9
semc said:
Guys i had written the program and the last term is 1000000000000000

This is a 16 bit sequence. Besides, calling it last doesn't tell anything, as we have no idea about how the numbers were ordered.

If your program correctly outputs everything as binary numbers, 000000000000000 is a correct binary form of 0.
 
  • #10
So you mean for a binary sequence of length 15 the last binary will have 15 digits and not 16? Well i need to print out the zeros as stated in the question any idea how i can get the 1st term to be 000000000000000?
 
  • #11
PHP:
printf("000000000000000");
 
  • #12
Erm sorry but this only print 000000000000000 to the screen right? Because I wrote it in a way such that the nth term is calculated based on the (n-1)th term so i will need to print the 1st term as 000000000000000 in order for this to work. So is there any command to print them as string?
 
  • #13
I am afraid I can't find any sense in what you have just posted. You sure you know what 'print' means?
 

1. How do I create a binary sequence of length 15 in C++?

To create a binary sequence of length 15 in C++, you can use a loop to generate 15 random numbers between 0 and 1 and store them in an array. Then, you can use a for loop to print out the elements of the array, which will represent the binary sequence.

2. What is the syntax for creating a binary sequence in C++?

The syntax for creating a binary sequence in C++ involves using the data type "int" to store the binary digits, and using the "cout" function to print out the elements of the sequence. You can also use the "rand()" function to generate random numbers between 0 and 1.

3. How can I ensure that the binary sequence is exactly 15 digits long?

To ensure that the binary sequence is exactly 15 digits long, you can use a "while" loop to check the size of the array and add more elements if needed. You can also use a "do-while" loop to generate random numbers until the array has 15 elements.

4. Can I create a binary sequence of any other length besides 15?

Yes, you can create a binary sequence of any other length by changing the size of the array and the number of iterations in the loop. For example, if you want a binary sequence of length 10, you can change the loop to run for 10 iterations and the array size to be 10 elements.

5. How can I use the binary sequence in other parts of my C++ program?

You can use the binary sequence in other parts of your C++ program by passing it as a parameter to a function or by storing it in a variable. You can also use it in conditional statements or mathematical operations to manipulate the sequence as needed.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
968
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
Back
Top