CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2002
    Posts
    4

    Help with some exercises

    Hello, I have this worksheet to do for my CSCI class. I just need some help on a few questions. I have my own answers to them but I would like some expert help.

    1. string *str;
    *str= “12345”;

    will the above code segment work as expected?
    Why?

    2. int *k;
    char c;
    k=&c;

    will the above code segment work? Why?

    3.class Base{
    public:
    void public_foo();
    private:
    void private_foo();
    protected:
    void protected_foo();
    };

    class Derived{
    public:
    void der_public_foo();
    void process();
    private:
    void der_private_foo();
    protected:
    void der_protected_foo();
    };

    Base b1, *b2;
    Derived d1, *d2;

    For each of the following statements, indicate
    valid/invalid and if invalid,
    explain clearly why.

    a) b2 = &d1;

    b) d2 = &b1;

    c) d2 = &d1;
    d2->der_protected_foo();

    d) d2 = &d1;
    d2->public_foo();

    e) b2 = &d1;
    b2->der_public_foo();

    f) The following is an implementation of
    Derived:rocess()
    void Derived:rocess()
    {
    Base:rotected_foo();
    Base:rivate_foo();
    }

    4. Write a recursive function to print a decimal
    number in reverse order. E.G.: 12345 would be printed as 54321. Note that the input is an integer, not a string. Hint: Use the / and % operators.


    Enhance the trance,
    April

  2. #2
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Help with some exercises

    you give what you think the answers are and we'll tell you if you are correct, and, if you are wrong, will help to explain to you why.

    but we're not going to do this exercise for you. not even for ratings (well some might).



    The best things come to those who rate

  3. #3
    Join Date
    Apr 2002
    Posts
    4

    Re: Help with some exercises

    OK, here it goes

    1. string *str;
    *str= “12345”;

    will the above code segment work as expected?
    Why? Yes it will work

    2. int *k;
    char c;
    k=&c;

    will the above code segment work? Why?
    No, you cannot assign a int to a char

    3.class Base{
    public:
    void public_foo();
    private:
    void private_foo();
    protected:
    void protected_foo();
    };

    class Derived{
    public:
    void der_public_foo();
    void process();
    private:
    void der_private_foo();
    protected:
    void der_protected_foo();
    };

    Base b1, *b2;
    Derived d1, *d2;

    For each of the following statements, indicate
    valid/invalid and if invalid,
    explain clearly why.

    a) b2 = &d1;
    Valid
    b) d2 = &b1;
    Valid
    c) d2 = &d1;
    d2->der_protected_foo();
    Invalid, cannot access protected members

    d) d2 = &d1;
    d2->public_foo();
    Valid

    e) b2 = &d1;
    b2->der_public_foo();
    Invalid, cannot access derived members through
    base class

    f) The following is an implementation of
    Derived:rocess()
    void Derived:rocess()
    {
    Base:rotected_foo();
    Base:rivate_foo();
    }
    Invalid, cannot access private and protected
    members.

    4. Write a recursive function to print a decimal
    number in reverse order. E.G.: 12345 would be printed as 54321. Note that the input is an integer, not a string. Hint: Use the / and % operators.
    For this one I have no clue!

    Enhance the trance,
    April

  4. #4
    Join Date
    Oct 2001
    Location
    Chicago, USA
    Posts
    222

    Re: Help with some exercises

    its actually very simple to check all these things, why dont you write small small test programs for each of you questions and see if it works and try and understand, atleast thats how use to do it..its quite simple.

    eg.


    int main()
    {
    int* k;
    char c = 'a';
    k = &c;

    return 0;
    }




    this gives an error and it tells you it can not convert char* to int*..similarly the other...

    have fun..

    cheers,
    Ankur


  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Help with some exercises

    OK, here it goes
    1
    string *str;
    *str= "12345";



    In reply to:


    will the above code segment work as expected?
    Why? Yes it will work




    string *str creates a pointer to a string, but does not allocate a string. The pointer is unassigned. Therefore you cannot expect the next statement to work.

    2
    int *k;
    char c;
    k=&c;


    In reply to:


    will the above code segment work? Why?
    No, you cannot assign a int to a char




    correct, you need to cast.

    3 (the comments below are my own)

    class Base
    {
    public:
    void public_foo();
    private:
    void private_foo();
    protected:
    void protected_foo();
    };
    //
    class Derived // derived from what?
    {
    public:
    void der_public_foo(); // are you serious?
    void process();
    private:
    void der_private_foo();
    protected:
    void der_protected_foo();
    };
    //
    Base b1, *b2;
    Derived d1, *d2;



    Can't be bothered continuing after that. Derived does not derive from Base in your example, and none of the functions in Base is virtual, and you cannot override a function by sticking der_ in front of it.




    The best things come to those who rate

  6. #6
    Join Date
    Apr 2002
    Posts
    4

    Re: Help with some exercises

    What about the reverse numeric string question? Can someone help me with that??


    Enhance the trance,
    April

  7. #7
    Join Date
    Apr 2002
    Posts
    4

    Re: Help with some exercises

    class Base{
    public:
    void public_foo();

    private:
    void private_foo();

    protected:
    void protected_foo();

    };
    class Derived{
    public:

    void der_public_foo();
    void process();

    private:
    void der_private_foo();

    protected:
    void der_protected_foo();

    };

    Base b1, *b2;
    Derived d1, *d2;

    Can't be bothered continuing after that. Derived does not derive from Base in your example, and none of the functions in Base is virtual, and you cannot override a function by sticking der_ in front of it.

    Whoops I am sorry about that. It's supposed to read:
    class Derivedublic Base{

    Enhance the trance,
    April

  8. #8
    Join Date
    Oct 2001
    Location
    Chicago, USA
    Posts
    222

    Re: Help with some exercises

    hey!
    this might work ok, not the best of way, but should work, havent tested it, but if u want more efficient algo, there might be someone else who might give u one, but as far as i go this works ok for now..

    l8r
    Ankur

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int val = 8901;
    char value1[20] = {0};
    itoa(val, value1, 10); //converts int to char array
    int sl = strlen(value1); //finds the length of the char array just created.
    char x; //this is used for temp storage for swaping

    for(int i =0, j=sl-1; i < sl/2; i++, j--)
    //we run the loop from 0 to 1/2 of the strlen becuase by then we would have swaped the string.
    {
    //general swaping
    x = value1[i];
    value1[i] = value1[j];
    value1[j] = x;
    }
    int val2 = atoi(value1);
    //converting the reversed string back to int.
    printf("%d", val2);
    //printint it




    output : 1098


  9. #9
    Join Date
    Oct 2001
    Location
    Chicago, USA
    Posts
    222

    Re: Help with some exercises

    hey this might work 2, if u or ur teacher doesnt want to use strings..

    Ankur


    int val = 98363456;
    int rem = 0;
    int newval = 0;
    for(int i=0; i<10; i++) //10 becuase thats the max number length of an int. i am assuming 32 bit int.
    {
    rem = val % 10; //get the last digit.
    newval = newval*10 + rem; //add it to the newval after multiplying the new val by 10 to **** its position forward.
    val = val / 10; //get the remainig int value
    if(val == 0) //if val becomes 0 then break out.
    break;
    }

    printf("\nnewval = %d\n", newval);






Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured