Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,044 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,525 people online right now. Registration is fast and FREE... Join Now!




output a iterative binary number but while increasing the bit size at

 
Reply to this topicStart new topic

output a iterative binary number but while increasing the bit size at

urntme
1 Aug, 2008 - 07:10 PM
Post #1

New D.I.C Head
*

Joined: 1 Aug, 2008
Posts: 35


My Contributions
Here's what I want to do...

0
1
00
01
10
11
000
001
010
011
100
101
110
111

As in I want to output a iterative binary number but after it reaches its end, it must increase the number of bits and start anew.

I wish I could post some code here, but I have no idea how to go about this.. blink.gif

If someone could help.. I would be greatfull... smile.gif
User is offlineProfile CardPM
+Quote Post

Bench
RE: Output A Iterative Binary Number But While Increasing The Bit Size At
2 Aug, 2008 - 12:10 AM
Post #2

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 617



Thanked: 14 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
If you can't see the wood through the trees when trying to solve a problem, then you need to make it simpler by picking out just one of the difficult elements, and solve that on its own

Here you might try to first work out how to write a program which represents just one number (any number) to the user in binary.
User is offlineProfile CardPM
+Quote Post

urntme
RE: Output A Iterative Binary Number But While Increasing The Bit Size At
2 Aug, 2008 - 08:53 AM
Post #3

New D.I.C Head
*

Joined: 1 Aug, 2008
Posts: 35


My Contributions
QUOTE(Bench @ 2 Aug, 2008 - 01:10 AM) *

If you can't see the wood through the trees when trying to solve a problem, then you need to make it simpler by picking out just one of the difficult elements, and solve that on its own

Here you might try to first work out how to write a program which represents just one number (any number) to the user in binary.


that's not tough...
I can just output using a for loop

for (i=0;i<2;i++)
{
cout<<i<<"\n";
}

The question is now how do I output 2 0's and increment those numbers and continue the process...
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Output A Iterative Binary Number But While Increasing The Bit Size At
2 Aug, 2008 - 09:11 AM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
I think you missed the point... If I give you a number X, what is that number in binary.

How do you convert a number to binary?

once you solve that... then you program is just a counting loop displaying the binary representation of the numbers. i.e REALLY simple! the hard part is converting a number to binary. (It is really not very difficult).
User is offlineProfile CardPM
+Quote Post

urntme
RE: Output A Iterative Binary Number But While Increasing The Bit Size At
3 Aug, 2008 - 02:25 AM
Post #5

New D.I.C Head
*

Joined: 1 Aug, 2008
Posts: 35


My Contributions
QUOTE(NickDMax @ 2 Aug, 2008 - 10:11 AM) *

I think you missed the point... If I give you a number X, what is that number in binary.

How do you convert a number to binary?

once you solve that... then you program is just a counting loop displaying the binary representation of the numbers. i.e REALLY simple! the hard part is converting a number to binary. (It is really not very difficult).


hmmm... thanks for the tip..
okay.. done..
finally got a working one

CODE

#include <iostream.h>
#include <conio.h>
void binary(int);
void zero(int);
int r=2,l=0,m=2;
void main(void) {
    cout << "Please enter a positive integer: ";
    int n;
    cin>>n;
    int number;
     cout<<0<<"\n";
for(int i=1;i<=n;i++)
  {
   number=i;
   zero(i);
   if (number < 0)
        cout << "That is not a positive integer.\n";
    else {
        //cout << number << " converted to binary is: ";
        binary(number);
        cout << endl;
      }
}
  getche();

}
void zero(int i)
{
if (i==r)
   {
   for(l=0;l<m;l++)
   {
    cout<<0;
    }
    m=m++;
    cout<<"\n";
    r=(r*2);
    return;
    }
    else
     {
     return;}
     }
void binary(int number) {
    int remainder;
    if(number <= 1) {
        cout << number;
        return;
    }
    remainder = number%2;
    binary(number >> 1);    
    cout << remainder;
}


okay... thx for the help

This post has been edited by urntme: 3 Aug, 2008 - 03:25 AM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Output A Iterative Binary Number But While Increasing The Bit Size At
3 Aug, 2008 - 09:47 AM
Post #6

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
You should really use "#include <iostream>" rather than "iostream.h" -- of course if you do use "iostream" then you will need to add the lines:
CODE
using std::cout;
using std::cin;
using std::endl;
or you could simplify things by using using namespace std;

you also should not use "void main()" -- main returns a value so to be logically consistent you should "int main()". The of course means that you should add a "return 0;" to the end of the main() function.

You use global variables! Don't use global variables! The only global variables you should use are constants. Otherwise they are a really bad idea that tends to lead to some very hard to find irritating bugs where a variables value is changed outside the logic of the current process.

this: m=m++; is bad, it should be m++;.

if you think about it: m=m++; would be something like m = m = m + 1;

While we are at it: r = (r * 2); can be written as r *= 2;

User is offlineProfile CardPM
+Quote Post

urntme
RE: Output A Iterative Binary Number But While Increasing The Bit Size At
3 Aug, 2008 - 07:04 PM
Post #7

New D.I.C Head
*

Joined: 1 Aug, 2008
Posts: 35


My Contributions
QUOTE(NickDMax @ 3 Aug, 2008 - 10:47 AM) *

You should really use "#include <iostream>" rather than "iostream.h" -- of course if you do use "iostream" then you will need to add the lines:
CODE
using std::cout;
using std::cin;
using std::endl;
or you could simplify things by using using namespace std;

you also should not use "void main()" -- main returns a value so to be logically consistent you should "int main()". The of course means that you should add a "return 0;" to the end of the main() function.

You use global variables! Don't use global variables! The only global variables you should use are constants. Otherwise they are a really bad idea that tends to lead to some very hard to find irritating bugs where a variables value is changed outside the logic of the current process.

this: m=m++; is bad, it should be m++;.

if you think about it: m=m++; would be something like m = m = m + 1;

While we are at it: r = (r * 2); can be written as r *= 2;


okay cool, didn't know I could just simply use r*=2 and m++, thx for the tip will do.

And yeah, will try to make do without global variables too...

But what's with <iostream> instead of iostream.h, I mean what's the difference?

User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Output A Iterative Binary Number But While Increasing The Bit Size At
3 Aug, 2008 - 10:25 PM
Post #8

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
The "iostream.h" is a pre-standard header. It does not belong to the std namespace. Other than this there is really little differance in general. There are some oddities in a few of the headers for some of the compilers (i.e. IBM and Microsoft like to put thier own little bit into the header files). Mostly it is just a convention to introduce the std namespace which helps to keep naming clashes from happening too often.

So: <iostream> is newer, and standard.
User is offlineProfile CardPM
+Quote Post

urntme
RE: Output A Iterative Binary Number But While Increasing The Bit Size At
4 Aug, 2008 - 07:03 AM
Post #9

New D.I.C Head
*

Joined: 1 Aug, 2008
Posts: 35


My Contributions
okay cool... thx for the advice.. [smile.gif]
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 04:52PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month