My binary converter trigger question about vector, datatype, sizof op. etc...

This is a discussion on My binary converter trigger question about vector, datatype, sizof op. etc... within the C++ Programming forums, part of the General Programming Boards category; That's incredible. I work out a binary converter which convert decimal value into binary.A lot of people has already done ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Location
    Budapest
    Posts
    26

    Cool My binary converter trigger question about vector, datatype, sizof op. etc...

    That's incredible. I work out a binary converter which convert decimal value into binary.A lot of people has already done the same that before,but now I also has maneged to do it. I didn't think so far that this task can be coded in such a simply and brief way. How effective c++ sometimes can be! Here you are:

    #include<iostream>
    #include<vector>
    #include<cmath>
    using namespace std;

    vector<int>binary(1); int i, s=0 ;

    vector<int> Binary(int dec);

    int main(){

    cout<<"Give a positive integer: ";

    while(cin>>i){ cin.ignore(); Binary(i);
    cout<<endl; i = binary.size();

    while(i>0){ i -=1; s+=sizeof(binary[i]);
    cout<<binary[i]<<" ";
    } cout<<"\tThe size of your vector in byte: "<< s;

    cout<<endl<<endl; binary.resize(1); s=0; main(); }

    cin.get();

    }

    vector<int> Binary(int dec){ int l=1, m=0; bool flag = 0; binary[0]=0;

    loop: while(l<dec){
    l*=2; m++; if (flag==false && l<=dec) binary.push_back(0);


    }



    if(dec==0){binary[0]=0;}
    else if(dec==1){binary[0]=1;}
    else if(dec-l==0)binary[m]=1;
    else{binary[m-1]=1; dec -= l/2; flag = true; l=1; m=0; goto loop;}

    }

    My binary converter trigger question about vector, datatypes, sizeof op. and their usage which may be worth to discuss.

    However this solvation put up some brand new question to me as well.

    The point is, the following behaviour of the code: If you replace the type of the data that binary vector contains with bool

    type (allocated 1byte space) the program doesn't do the converting in the right way.
    The following event happens to programme with vector of bool type:

    1st input(any power of the number two): e.g. 16 1st output: 10000

    2nd input(any number that isn't power of two): e.g. 7 2nd output: 000 (the number of digit is right)

    Then the program goes well without any mistake.

    Am I right when I come to the conclusion that vector class is a description (designe) of such data containers that don't

    work with bool datatype properly, Maybe, there are defined flag bits which needs to mark the beginning or the end of the

    allocated memory region, for example.

    The another event which not cause problem just simply new for me. The behavour of the sizeof operator:

    In the case of code with vector of integer type (allocated 4 byte space for each element) : e.g.:

    1st input: 3 1st output: 11 The size of your vector in byte 8
    2nd input: 16 2nd output: 10000 The size of your vector in byte 20

    In the case of code with vector of bool type (allocated 1 byte space for each element): e.g.:

    1st input: 3 1st output: 11 The size of your vector in bit 16
    2nd input: 16 2nd output: 10000 The size of your vector in bit 40

    Yes, you can see well, you must overwrite even the text part "in byte" as I think sizeof operator, in case of bool type,

    calculat tha space of mamory not byte by byte but bit by bit. That's the curious happening suprises me.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    vector<bool> is a special case of vector that uses one bit per bool. If you need a container type with usable Boolean values, you're supposed to use a deque<bool>. vector is different from deque in terms of how it manages memory, and deque offers push_front() and pop_front(), but beyond that, they offer a similar container.

    I think you need to read this: << !! Posting Code? Read this First !! >> and http://xkcd.com/292/, specifically, the panels on goto. I don't think it has anything on calling main(), but you're not supposed to do that either. It's unnecessary. If you need to repeat the program, you can put everything in a simple loop and decide when to exit main().
    Last edited by whiteflags; 03-05-2011 at 11:58 AM.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Location
    Budapest
    Posts
    26

    Here is the code!

    Code:
    #include<iostream>
    #include<vector>
    #include<cmath>
    using namespace std;
    
    vector<int>binary(1); int i, s=0 ; 
    
    vector<int> Binary(int dec);
                
    int main(){ 
              
        cout<<"Give a positive integer: ";
        
      while(cin>>i){ cin.ignore();  Binary(i);
        cout<<endl; i = binary.size();
        
                     while(i>0){ i -=1; s+=sizeof(binary[i]);
                    cout<<binary[i]<<" ";
                    } cout<<"\tThe size of your vector in byte: "<< s;
        
        cout<<endl<<endl; binary.resize(1); s=0; main(); }
        
        cin.get();
            
        }
        
    vector<int> Binary(int dec){ int l=1, m=0; bool flag = 0; binary[0]=0;
                            
    loop:                               while(l<dec){
                                                    l*=2; m++; if (flag==false && l<=dec) binary.push_back(0);                                      
                                                           }     
                                                                                                                                                     
                                 if(dec==0){binary[0]=0;}      
                                 else if(dec==1){binary[0]=1;}
                                 else if(dec-l==0)binary[m]=1;
                                 else{binary[m-1]=1; dec -= l/2; flag = true; l=1; m=0; goto loop;}                             
                                  
                                             }
    I hope it is sufficient.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Turn your compiler warnings on and pay attention to them. That code is broken in a bad way.

    Don't use 'l' as a variable, it looks too similar to '1'.

    You don't need a vector for this. Instead of storing the ones and zeros you can just output them. Make sure you start from the right end of course.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    You seriously need to indent better.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Location
    Budapest
    Posts
    26
    My poor English don't let me understand the last remark. Can you explain me? (to indent or to intend) though it doesn't matter, wheter one or other do not make any sense for me.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Feb 2010
    Location
    Budapest
    Posts
    26
    Ok! But how I understand the whole statment?

  9. #9
    Registered User
    Join Date
    Feb 2010
    Location
    Budapest
    Posts
    26
    Hm. I still haven't got it. Help me please I don't wanna open a new thread for that.

  10. #10
    Registered User
    Join Date
    Feb 2010
    Location
    Budapest
    Posts
    26
    Be polite, if you can.

  11. #11
    Registered User
    Join Date
    Feb 2010
    Location
    Budapest
    Posts
    26
    OK. I see it. So you have problem with my style.

    My answer is: I am a hobbiest and hardly have enough chance to become a proffessional coder I am a mechanical engineer, and inspite of this fact I am glad to have a job at all, and can get a little time to write piece of work like that.

    I am interesting in any constructive opinion. Style is important, but as you can see, I don't know even how c++ work. I think, style can wait. :P

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    You need to appreciate how bad this code looks. Look at how many tabs you put in here.
    Name:  crazyindenting.jpg
Views: 211
Size:  80.8 KB
    Can you tell me without looking for curly braces or other hints what statements belong inside the while loops? Where does the first loop end? Anyway, the point is: To properly indent, you're supposed to line up code against a margin, and the code isn't indented until you could understand what is inside of what without the help of curly braces. People aren't going to help if you continue to write code no one can read.

  13. #13
    Registered User
    Join Date
    Feb 2010
    Location
    Budapest
    Posts
    26
    Uhm. Just a qustion. I use dev c++. Could you run it with cod blocks?

    You know, this work is pasted into txt format before copy to here, then try your suggested method "". That series of actions could caused some reformating. I will look after that.
    I usually intend to write clear, understandable codes.

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    I get the following messages when I build.


    -------------- Build: Debug in sandbox ---------------

    Compiling: main.cpp
    C:\Documents and Settings\Owner\My Documents\sandbox\main.cpp: In function 'int main()':
    C:\Documents and Settings\Owner\My Documents\sandbox\main.cpp:34:14: warning: ISO C++ forbids taking address of function '::main'
    C:\Documents and Settings\Owner\My Documents\sandbox\main.cpp: In function 'std::vector<int> Binary(int)':
    C:\Documents and Settings\Owner\My Documents\sandbox\main.cpp:74:1: warning: no return statement in function returning non-void

    Since I straightened the code out, things are different from yours, but line 34 refers to where you call main() and line 74 refers to the Binary function, where you return nothing, even though you said it returns a vector of ints.

    I'm kinda scared to run it since you call freaking main() in main() and I may never get out but you lucked out on my machine. It works. Despite all the things you did.

    I'm curious, did you not see how my earlier post addresses all of your questions and issues? If you don't understand, I'm here to clarify myself.
    Last edited by whiteflags; 03-06-2011 at 10:43 AM.

  15. #15
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    I don't know what teachers do, but it's not the first time I can see recursive main() calls.
    I never put signature, but I decided to make an exception.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. binary heap (build question)
    By axon in forum C++ Programming
    Replies: 1
    Last Post: 03-01-2004, 09:49 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Another binary file question
    By girlrobot in forum C Programming
    Replies: 5
    Last Post: 07-30-2002, 05:40 PM
  5. Question about conversions between assembly, binary, and c
    By Waldo2k2 in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 07-18-2002, 05:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21