Stack & recursion help needed!!

PeterTheMaster - you out there tonight? I need your help!! (Still working on my circle program by the way).

Tonights problem is this:

Main function MUST look like this:

void main()
{ int input;
getinput(input);
print_stack(n);
print_recursion(n);
print_binary(n);
}

I'm trying to implement a class of STACK, and have three different outputs : stack, recursion, and no stack nor recursion.

Here is example of running program:

Please insert a number for binary representation:
10
The binary representation of 10 is (using stack): 1010
The binary representation of 10 is (using recursion): 1010
The binary representation of 10 is (no stack nor recursion): 1010

This is what I have so far: (PeterTheMaster, notice my new improved header!)

#include
using std::cout;
using std::cin;
using std::endl;
using std::istream;
using std::ostream;

class STACK{
int data[100];
int top;
int n;
public:
void getinput();
void push(int target) {data[top++] = target;};
int isEmpty(){return !top;};
int pop() {return data[--top];};
};

int print_stack(int n)
{
if (n == 0)
return n;

cout << "The binary representation of 10 is (using stack): " << n.pop() << endl;
}

STACK getinput(int input)
{ int n;
cout << "Please enter a number for binary representation:
";
cin >> n;
}

Ok, I'll admit, I'm out of my league here. I'm not sure where to go. I know I have big problems with the above. Please help!!

Comments

  • I don't know exactly what you are trying to do but, after your #include statement you should type: using namespace std; That way you don't have declare for every function you intend to use.

    : PeterTheMaster - you out there tonight? I need your help!! (Still working on my circle program by the way).
    :
    : Tonights problem is this:
    :
    : Main function MUST look like this:
    :
    : void main()
    : { int input;
    : getinput(input);
    : print_stack(n);
    : print_recursion(n);
    : print_binary(n);
    : }
    :
    : I'm trying to implement a class of STACK, and have three different outputs : stack, recursion, and no stack nor recursion.
    :
    : Here is example of running program:
    :
    : Please insert a number for binary representation:
    : 10
    : The binary representation of 10 is (using stack): 1010
    : The binary representation of 10 is (using recursion): 1010
    : The binary representation of 10 is (no stack nor recursion): 1010
    :
    : This is what I have so far: (PeterTheMaster, notice my new improved header!)
    :
    : #include
    : using std::cout;
    : using std::cin;
    : using std::endl;
    : using std::istream;
    : using std::ostream;
    :
    : class STACK{
    : int data[100];
    : int top;
    : int n;
    : public:
    : void getinput();
    : void push(int target) {data[top++] = target;};
    : int isEmpty(){return !top;};
    : int pop() {return data[--top];};
    : };
    :
    : int print_stack(int n)
    : {
    : if (n == 0)
    : return n;
    :
    : cout << "The binary representation of 10 is (using stack): " << n.pop() << endl;
    : }
    :
    : STACK getinput(int input)
    : { int n;
    : cout << "Please enter a number for binary representation:
    ";
    : cin >> n;
    : }
    :
    : Ok, I'll admit, I'm out of my league here. I'm not sure where to go. I know I have big problems with the above. Please help!!
    :

  • [b][red]This message was edited by the PeterTheMaster at 2002-7-1 12:32:48[/red][/b][hr]
    : I don't know exactly what you are trying to do but, after your #include statement you should type: using namespace std; That way you don't have declare for every function you intend to use.

    no you should not. it pollutes the global namespace. the best solution namespacewise would be to write std::cout<<x<<std::endl; every time, but i find this a quite high price to pay and i do it only if i get some name conflicts.

    this using std::cin; also helps to remember why you included a certain header. it makes you see quite clear at the top of the file what field your program is going to work in.

    :
    : : PeterTheMaster - you out there tonight? I need your help!! (Still working on my circle program by the way).

    thanks for asking me personally, i am flattered.

    : :
    : : Tonights problem is this:


    when do you have to turn in? because i dont have much time the next days.
    and: please write [ c o d e ] code here [ / c o d e ] without the spaces around your code you post here, makes it much more readable.


    : :
    : : Main function MUST look like this:
    : :
    : : void main()
    : : { int input;
    : : getinput(input);
    : : print_stack(n);
    : : print_recursion(n);
    : : print_binary(n);
    : : }

    i do not see how this is supposed to work.
    is n a global variable? (bad bad style, only use global variables if you really really have to (which is not often))

    and as far as i know (i think whoie mentioned it once) main() has to return int otherwise its non standard and the compiler is free to do with you what it wants.

    like

    [code]
    void getinput(int& input){
    cout<<"enter number to convert: ";
    cin>>input;
    }
    void print_stack(unsigned int n){
    cout<<"using stack: ";
    unsigned int nr=n; //no good style to change in-parameter
    stack<int> b;
    do{ b.push(nr&1); nr>>=1; }while(nr); //check afterwards to get 0
    while( b.size() ){ cout<<b.top(); b.pop(); }
    cout<<endl;
    }

    void binRek(unsigned int n){
    if(n>>1){ binRek(n>>1); } //if(n>>1) to treat 0 correctly
    cout<<(n&1);
    }
    void print_recursion(unsigned int n){
    cout<<"using recursion: ";
    binRek(n);
    cout<<endl;
    }

    void print_binary(unsigned int n){
    cout<<"using neither: ";
    unsigned int nr=n; //no good style to change in-parameter
    vector<int> b;
    do{ b.push_back(nr&1); nr>>=1; }while(nr); //check afterwards to get 0
    reverse(b.begin(),b.end());
    vector::const_iterator it = b.begin();
    while( it != b.end() ){cout<<*it++;}
    cout<<endl;
    }

    #define n input
    int main(){
    int input;
    getinput(input);
    print_stack(n);
    print_recursion(n);
    print_binary(n);
    return 0;
    }
    #undef n


    [/code]


    : :
    : : I'm trying to implement a class of STACK, and have three different outputs : stack, recursion, and no stack nor recursion.
    : :
    : : Here is example of running program:
    : :
    : : Please insert a number for binary representation:
    : : 10
    : : The binary representation of 10 is (using stack): 1010
    : : The binary representation of 10 is (using recursion): 1010
    : : The binary representation of 10 is (no stack nor recursion): 1010
    : :
    : : This is what I have so far: (PeterTheMaster, notice my new improved header!)
    : :
    : : #include<iostream>
    : : using std::cout;
    : : using std::cin;
    : : using std::endl;
    : : using std::istream;
    : : using std::ostream;
    : :
    : : class STACK{
    : : int data[100];
    : : int top;
    : : int n;
    : : public:
    : : void getinput();
    : : void push(int target) {data[top++] = target;};
    : : int isEmpty(){return !top;};
    : : int pop() {return data[--top];};
    : : };
    : :
    : : int print_stack(int n)
    : : {
    : : if (n == 0)
    : : return n;
    : :
    : : cout << "The binary representation of 10 is (using stack): " << n.pop() << endl;
    : : }
    : :
    : : STACK getinput(int input)
    : : { int n;
    : : cout << "Please enter a number for binary representation:
    ";
    : : cin >> n;
    : : }
    : :
    : : Ok, I'll admit, I'm out of my league here. I'm not sure where to go. I know I have big problems with the above. Please help!!
    : :
    :
    :



  • Ok Pete, here's what I have. Still getting a couple errors. And the assignment is due Tues night, yikes!! I'll try the code thing this time, not sure if I'm doing this right...Oh, and there was an error in main function, corrected below.

    [code]
    #include
    using std::cout;
    using std::cin;
    using std::endl;
    using std::istream;
    using std::ostream;

    class STACK{
    int data[100];
    int top;
    int n;
    public:
    void getinput();
    void push(int target) {data[top++] = target;};
    int isEmpty(){return !top;};
    int pop() {return data[--top];};
    };

    void getinput(int& n){
    cout<<"enter number to convert: ";
    cin>>n;
    }
    void print_stack(int n){
    cout<<"using stack: ";

    STACK b;
    do{ b.push(n&1); n>>=1; }while(n); //check afterwards to get 0
    while( b.size() ){ cout<<b.top(); b.pop(); }
    cout<<endl;
    }

    void binRek(unsigned int n){
    if(n>>1){ binRek(n>>1); } //if(n>>1) to treat 0 correctly
    cout<<(n&1);
    }
    void print_recursion(unsigned int n){
    cout<<"using recursion: ";
    binRek(n);
    cout<<endl;
    }

    void print_binary(unsigned int n){
    cout<<"using neither: ";
    unsigned int nr=n; //no good style to change in-parameter
    vector<int> b;
    do{ b.push_back(nr&1); nr>>=1; }while(nr); //check afterwards to get 0
    reverse(b.begin(),b.end());
    vector::const_iterator it = b.begin();
    while( it != b.end() ){cout<<*it++;}
    cout<<endl;
    }

    void main( )
    {
    int n;

    getinput(n );
    print_stack( n );
    print_recursion( n );
    print_binary( n );
    }

    [/code]


  • [b][red]This message was edited by the PeterTheMaster at 2002-7-1 15:42:50[/red][/b][hr]
    sorry i dont have time to ask you step by step,
    hope you learn a bit if i give you what i think in
    bigger code chunks.


    : using std::istream;
    : using std::ostream;

    why these?
    dont forget

    #include
    using std::vector;
    using std::vector;

    are you explicitely supposed to write your own stack class?
    if the assignment doesnt say so i wouldnt do it.
    reuse is much wiser then reinvent.

    in case you have to make your own stack but are allowed to use vector, heres my suggestion:
    [code]
    template
    class myStack{
    public:
    T top()const{return v.back();}
    void pop(){v.pop_back();}
    void push(const T&n){v.push_back(n);}
    int size()const{return v.size();}
    private:
    vector v;
    };

    typedef myStack STACK;
    [/code]

    btw: why do you want your class to be called STACK?
    names in all capital are usually used for constants
    or defines.

    :
    : class STACK{
    : int data[100];
    : int top;
    : int n;
    : public:
    : void getinput();
    : void push(int target) {data[top++] = target;};
    : int isEmpty(){return !top;};
    : int pop() {return data[--top];};
    : };

    and seems you forgot again to put the public stuff on top.



    : void main( )
    : {
    : int n;
    :
    : getinput(n );
    : print_stack( n );
    : print_recursion( n );
    : print_binary( n );
    : }

    please make main return int. you want to learn standard c++, not some
    strange dialect, right?

    look here
    http://www.programmersheaven.com/c/msgboard/read.asp?Board=3&MsgID=99956&Setting=A9999F0001

    i unfortunately dont have any more prove since the link in this post doesnt work any more.
    but since eric tetz said it it would be very hard to convince me that its wrong.


  • Can you clear something up for me please? Since I am self taught from books, and in all the programs using namespace std; was used. How does that polute the namespace? Also, how do you not remember that you included a specific header when at the beginning of the programs you are including and using namespaces?

    : [b][red]This message was edited by the PeterTheMaster at 2002-7-1 12:32:48[/red][/b][hr]
    : : I don't know exactly what you are trying to do but, after your #include statement you should type: using namespace std; That way you don't have declare for every function you intend to use.
    :
    : no you should not. it pollutes the global namespace. the best solution namespacewise would be to write std::cout<<x<<std::endl; every time, but i find this a quite high price to pay and i do it only if i get some name conflicts.
    :
    : this using std::cin; also helps to remember why you included a certain header. it makes you see quite clear at the top of the file what field your program is going to work in.
    :
    : :
    : : : PeterTheMaster - you out there tonight? I need your help!! (Still working on my circle program by the way).
    :
    : thanks for asking me personally, i am flattered.
    :
    : : :
    : : : Tonights problem is this:
    :
    :
    : when do you have to turn in? because i dont have much time the next days.
    : and: please write [ c o d e ] code here [ / c o d e ] without the spaces around your code you post here, makes it much more readable.
    :
    :
    : : :
    : : : Main function MUST look like this:
    : : :
    : : : void main()
    : : : { int input;
    : : : getinput(input);
    : : : print_stack(n);
    : : : print_recursion(n);
    : : : print_binary(n);
    : : : }
    :
    : i do not see how this is supposed to work.
    : is n a global variable? (bad bad style, only use global variables if you really really have to (which is not often))
    :
    : and as far as i know (i think whoie mentioned it once) main() has to return int otherwise its non standard and the compiler is free to do with you what it wants.
    :
    : like
    :
    : [code]
    : void getinput(int& input){
    : cout<<"enter number to convert: ";
    : cin>>input;
    : }
    : void print_stack(unsigned int n){
    : cout<<"using stack: ";
    : unsigned int nr=n; //no good style to change in-parameter
    : stack<int> b;
    : do{ b.push(nr&1); nr>>=1; }while(nr); //check afterwards to get 0
    : while( b.size() ){ cout<<b.top(); b.pop(); }
    : cout<<endl;
    : }
    :
    : void binRek(unsigned int n){
    : if(n>>1){ binRek(n>>1); } //if(n>>1) to treat 0 correctly
    : cout<<(n&1);
    : }
    : void print_recursion(unsigned int n){
    : cout<<"using recursion: ";
    : binRek(n);
    : cout<<endl;
    : }
    :
    : void print_binary(unsigned int n){
    : cout<<"using neither: ";
    : unsigned int nr=n; //no good style to change in-parameter
    : vector<int> b;
    : do{ b.push_back(nr&1); nr>>=1; }while(nr); //check afterwards to get 0
    : reverse(b.begin(),b.end());
    : vector::const_iterator it = b.begin();
    : while( it != b.end() ){cout<<*it++;}
    : cout<<endl;
    : }
    :
    : #define n input
    : int main(){
    : int input;
    : getinput(input);
    : print_stack(n);
    : print_recursion(n);
    : print_binary(n);
    : return 0;
    : }
    : #undef n
    :
    :
    : [/code]
    :
    :
    : : :
    : : : I'm trying to implement a class of STACK, and have three different outputs : stack, recursion, and no stack nor recursion.
    : : :
    : : : Here is example of running program:
    : : :
    : : : Please insert a number for binary representation:
    : : : 10
    : : : The binary representation of 10 is (using stack): 1010
    : : : The binary representation of 10 is (using recursion): 1010
    : : : The binary representation of 10 is (no stack nor recursion): 1010
    : : :
    : : : This is what I have so far: (PeterTheMaster, notice my new improved header!)
    : : :
    : : : #include<iostream>
    : : : using std::cout;
    : : : using std::cin;
    : : : using std::endl;
    : : : using std::istream;
    : : : using std::ostream;
    : : :
    : : : class STACK{
    : : : int data[100];
    : : : int top;
    : : : int n;
    : : : public:
    : : : void getinput();
    : : : void push(int target) {data[top++] = target;};
    : : : int isEmpty(){return !top;};
    : : : int pop() {return data[--top];};
    : : : };
    : : :
    : : : int print_stack(int n)
    : : : {
    : : : if (n == 0)
    : : : return n;
    : : :
    : : : cout << "The binary representation of 10 is (using stack): " << n.pop() << endl;
    : : : }
    : : :
    : : : STACK getinput(int input)
    : : : { int n;
    : : : cout << "Please enter a number for binary representation:
    ";
    : : : cin >> n;
    : : : }
    : : :
    : : : Ok, I'll admit, I'm out of my league here. I'm not sure where to go. I know I have big problems with the above. Please help!!
    : : :
    : :
    : :
    :
    :
    :
    :

  • : : void main( )
    : : {
    : : int n;
    : :
    : : getinput(n );
    : : print_stack( n );
    : : print_recursion( n );
    : : print_binary( n );
    : : }
    :
    : please make main return int. you want to learn standard c++, not some
    : strange dialect, right?
    :
    : look here
    : http://www.programmersheaven.com/c/msgboard/read.asp?Board=3&MsgID=99956&Setting=A9999F0001
    :
    : i unfortunately dont have any more prove since the link in this post doesnt work any more.
    : but since eric tetz said it it would be very hard to convince me that its wrong.

    Indeed, here is a link to a more updated source:

    http://www.research.att.com/~bs/bs_faq2.html#void-main

    Or, check the FAQ's in my signature for more.


    HTH,
    Will
    --
    http://www.tuxedo.org/~esr/faqs/smart-questions.html
    http://www.eskimo.com/~scs/C-faq/top.html
    http://www.parashift.com/c++-faq-lite/
    http://www.accu.org/


  • : Can you clear something up for me please? Since I am self taught from books, and in all the programs using namespace std; was used. How does that polute the namespace? Also, how do you not remember [red]*that[/red] you included a specific header when at the beginning of the programs you are including and using namespaces?


    * i didnt say that. i said why.

    pollution in this sense means that you put all the symbols from the std namespace into your namespace although you dont need all of them.
    in your books they probably typed just some quick and dirty solutions to show some points. what did they say why they used
    using namespace std;?
    it just kills the concept of the namespace std. no more protection against name conflicts.
    but hey, if youre too lazy just use it. in most cases its _only_ a question of style.


  • I do understand what you are saying. Also, the books I've been reading do emphasize that programming is more of an art than science because it is good style that will make the program as easy to read as possible because if you need to make changes down the road then you will have a much harder time with sloppy code. Unfortunately, since I haven't had any schooling on the subject I am forced to learn from my mistakes, but I'm getting there. What does really stump me though is why everyone is so against globals. Why are they so bad?

    : : Can you clear something up for me please? Since I am self taught from books, and in all the programs using namespace std; was used. How does that polute the namespace? Also, how do you not remember [red]*that[/red] you included a specific header when at the beginning of the programs you are including and using namespaces?
    :
    :
    : * i didnt say that. i said why.
    :
    : pollution in this sense means that you put all the symbols from the std namespace into your namespace although you dont need all of them.
    : in your books they probably typed just some quick and dirty solutions to show some points. what did they say why they used
    : using namespace std;?
    : it just kills the concept of the namespace std. no more protection against name conflicts.
    : but hey, if youre too lazy just use it. in most cases its _only_ a question of style.
    :
    :
    :

  • [b][red]This message was edited by the PeterTheMaster at 2002-7-3 2:42:16[/red][/b][hr]

    : What does really stump me though is why everyone is so against globals. Why are they so bad?

    global variables create (mostly) unwanted dependencies.
    like
    [code]
    int n;
    void input(){
    //do really cool and complicated stuff here
    cin>>n;
    }
    void output(){
    //make a really nice output
    cout<<n;
    }

    int main(){
    input();
    output();
    return 0;
    }
    [/code]
    you will never be able to reuse these input and output functions as opposed to here:
    [code]
    int input(){
    //do the cool stuff
    int n;
    cin n;
    return n;
    }
    void output(int n){
    //make a really nice output
    cout<<n;
    }
    int main(){
    int n=input();
    output(n);
    return 0;
    }
    [/code]


    see?
    and you could now go on increase the usefulness of these functions by making them templates, which would have been a pure waste in the previous case...

    the stuff with the global namespace is a completely different thing. thats just about style and name conflicts.


    :
    : : : Can you clear something up for me please? Since I am self taught from books, and in all the programs using namespace std; was used. How does that polute the namespace? Also, how do you not remember [red]*that[/red] you included a specific header when at the beginning of the programs you are including and using namespaces?
    : :
    : :
    : : * i didnt say that. i said why.
    : :
    : : pollution in this sense means that you put all the symbols from the std namespace into your namespace although you dont need all of them.
    : : in your books they probably typed just some quick and dirty solutions to show some points. what did they say why they used
    : : using namespace std;?
    : : it just kills the concept of the namespace std. no more protection against name conflicts.
    : : but hey, if youre too lazy just use it. in most cases its _only_ a question of style.
    : :
    : :
    : :
    :
    :







  • : [b][red]This message was edited by the PeterTheMaster at 2002-7-3 2:42:16[/red][/b][hr]
    :
    : : What does really stump me though is why everyone is so against globals. Why are they so bad?
    :
    : global variables create (mostly) unwanted dependencies.

    Would it be reasonable to declare a global variable based on a class which is used by six different files. That is what I have basically done in one of the programs I wrote. Splitting up the files was the only way to divide the code into modules that would make reading it much easier. The thing is is that the class is used by almost every function but doesn't need to use it exclusively. It could use, eg. another string, int, etc...

  • : declare a global variable based on a class

    what do you mean "based on a class"? is the type of this variable a class?

    : which is used by six different files.

    how do they use it?

    : Splitting up the files was the only way to divide the code into modules that would make reading it much easier.

    u usually do this in the first place. each class 2 files. (if the classes are not too big and work very closely together you can put more together)

    : The thing is is that the class is used by almost every function but doesn't need to use it exclusively.

    using a type in many functions isnt the same as using global variables, its normal. if you mean they use an object, show an example how they do it. maybe if this is the normal use for the class you should all its methods and attributes static.

    : It could use, eg. another string, int, etc...

    ???

  • [b][red]This message was edited by the georgiek50 at 2002-7-4 1:16:50[/red][/b][hr]
    Ok, maybe what I said sounded Chinese because I didn't use proper terminology. Here goes:

    : what do you mean "based on a class"? is the type of this variable a class?

    Yes, it is a class declared normally in one file, int the rest 'extern class ...'

    : : which is used by six different files.
    :
    : how do they use it?

    It is basically an ordered linked list and the functions manipulate each string as needed. Almost all functions belong to this one class.

    : : Splitting up the files was the only way to divide the code into modules that would make reading it much easier.
    :
    : u usually do this in the first place. each class 2 files. (if the classes are not too big and work very closely together you can put more together)

    I split them up into different files based on the different aspects of the program that they control. Each file is dependent on the header file that contains the class definition and function prototypes.

    : : The thing is is that the class is used by almost every function but doesn't need to use it exclusively.
    :
    : using a type in many functions isnt the same as using global variables, its normal. if you mean they use an object, show an example how they do it. maybe if this is the normal use for the class you should all its methods and attributes static.

    This statement was stupidity on my part. The functions are tailored exclusively for the class (not very portable). I did write some independent ones for example a small compare function which compares two strings to see if they are the same regardless of uppercase/lowercase letters; basically a modified strcmp function.

    Here is an example of a global variable that I used: If a user presses the specific accelerator eg. Ctrl+X then he is asked for a password. If the password is correct then I have a global variable called PasswordEnabled set to 1, if incorrect to 0. (This occurs in the GUI part of the application, which has WinMain, message processing, and the dialog box functions). In another file which contains the body of the functions of the class, depending on whether or not PasswordEnabled == 1, will print out different strings from the ordered list. Is this bad style or not?



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories