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

  • Context: Comp Sci 
  • Thread starter Thread starter semc
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Discussion Overview

The discussion revolves around creating a C++ program to generate all binary sequences of length 15. Participants explore various approaches, algorithms, and challenges related to implementing this task in code.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant expresses uncertainty about how to start writing a C++ program for generating binary sequences.
  • Another suggests generating numbers from 0 to 65535 and converting them to binary, questioning the necessity of printing them.
  • A participant proposes breaking down the problem by first writing binary sequences of smaller lengths, such as 4 or 5, to develop an algorithm.
  • Some participants discuss the limitations of the printf command in C++ for binary conversion and suggest writing a custom converter.
  • One participant mentions their program outputs the last term as 1000000000000000 and seeks advice on formatting the output to include leading zeros for the first term.
  • Another participant clarifies that the last binary sequence should have 15 digits, not 16, and emphasizes that 000000000000000 is a valid binary representation of 0.
  • There is a discussion about how to print the first term correctly while calculating subsequent terms based on previous ones.

Areas of Agreement / Disagreement

Participants have not reached a consensus on the best approach to implement the program, and multiple competing views on how to handle binary conversion and output formatting remain present.

Contextual Notes

Some participants express confusion regarding the output requirements and the implications of printing binary sequences with leading zeros. There are also unresolved questions about the order of the generated sequences.

semc
Messages
364
Reaction score
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
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.
 
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)
 
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?
 
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.
 
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.
 
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:
 
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:
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?
 

Similar threads

  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 22 ·
Replies
22
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
2K
Replies
7
Views
3K