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

    Simple question.

    Hi all, I am VERY NEW in C++ and programming. I just wanna print w and i, such that when w is equal to 16, i increases by 1 and w restarts from 0 until w reaches 1024. But the result doesn't come as I expected. Pls. guide me. Here is the code I have written.

    void count(int, int);
    int calcW(int *);
    int main()
    {
    int w=16, i=0;
    count(w, i);
    return 0;
    }

    void count(int i, int i)
    {
    int *wPtr;
    wPtr = &w;
    while (i<=2048)
    {
    for (int n=0; n<=*wPtr; n++)
    {
    cout<<"value of i = "<<i<<endl;
    cout<<"value of n = "<<n<<endl;
    if (n==*wPtr)
    {
    calcW(wPtr);
    cout<<"value of *wPtr in 2nd function"<<*wPtr<<endl;
    cout<<"reset w to 0 here"<<endl;
    if (i<=7){
    ++i;
    count(w, i);
    }
    }
    }
    }
    }

    int calcW(int *wP)
    {
    return *wP = (*wP) * 2;

    }

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Simple question.

    Your use of pointers here is obscure. Why do you think you need them?

  3. #3
    Join Date
    Sep 2010
    Posts
    15

    Re: Simple question.

    Quote Originally Posted by Lindley View Post
    Your use of pointers here is obscure. Why do you think you need them?
    I used the pointer to reset the w's value to zero.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Simple question.

    I don't see you doing that anywhere. Furthermore, calcW() is the only place where a pointer is used to gain access to a variable in a different scope, and that function is so trivial I wonder why it exists. (Not to mention you could use a reference rather than a pointer there to simplify syntax.)

  5. #5
    Join Date
    Sep 2010
    Posts
    15

    Re: Simple question.

    Quote Originally Posted by ritika_sharma82 View Post
    I used the pointer to reset the w's value to zero.
    Hi Lindley,
    Thank you for reply. I used pointer to reset the value to zero.
    I want to print like :

    0, 0
    1, 0
    2, 0
    ..
    ..
    16,0

    0, 1
    1, 1
    2, 1
    ....
    ....
    16, 1

    0, 2
    1, 2
    .....
    .....
    .....
    .....
    1024, 6

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Simple question.

    Quote Originally Posted by ritika_sharma82 View Post
    I used pointer to reset the value to zero.
    Still not seeing it. Point out the exact line where you do this.

    I want to print like :

    0, 0
    1, 0
    2, 0
    ..
    ..
    16,0

    0, 1
    1, 1
    2, 1
    ....
    ....
    16, 1

    0, 2
    1, 2
    .....
    .....
    .....
    .....
    1024, 6
    First of all, if you're resetting the left value to 0 each time it reaches 16, how do you expect it to ever get to 1024?

    Second, what output are you getting now.

  7. #7
    Join Date
    Jun 2009
    Location
    oklahoma
    Posts
    199

    Re: Simple question.

    Quote Originally Posted by ritika_sharma82 View Post
    w is equal to 16, i increases by 1 and w restarts from 0 until w reaches 1024.
    Yes.. how is this possible?? w should never get past 16, but it should somehow reach 1024??
    Do you mean when i reaches 1024?

  8. #8
    Join Date
    Sep 2010
    Posts
    15

    Re: Simple question.

    Quote Originally Posted by jnmacd View Post
    Yes.. how is this possible?? w should never get past 16, but it should somehow reach 1024??
    Do you mean when i reaches 1024?
    Hi Lindley and jnmacd,
    My apology for the mistake in my post (and reply as well). Thank you for the reply.
    I tried to get the result like this:
    (w, i)
    0, 0
    1, 0
    2, 0
    ..
    ..
    16,0 -----------------// reset here when n is 16
    -----------------//Double *w (i.e. 16*2)
    -----------------//increase i by one
    0, 1
    1, 1
    2, 1
    ....
    ....
    32, 1 -----------------// reset here when n is 32
    -----------------//Double *w (i.e. 32*2)
    -----------------//increase i by one
    0, 2
    1, 2
    2, 2
    ....
    ....
    64, 2 -----------------// reset here when n is 64
    -----------------//Double *w
    -----------------//increase i by one
    ……
    ……

    0, 6
    1, 6
    .....
    .....
    .....
    .....
    1024, 6 ...............//end the program here


    ******//And program is as: //******

    void count(int, int);
    int main()
    {
    int w=16, i=0;
    count(w, i);
    return 0;
    }

    void count(int w, int i)
    {
    int *wPtr;
    wPtr = &w;
    while (*wPtr<=1024)
    {
    for (int n=0; n<=*wPtr; n++)
    {
    cout<<" i = "<<i<<endl;
    cout<<" n = "<<n<<endl;
    if (n==*wPtr)
    {
    *wPtr = (*wPtr) * 2;
    cout<<"reset w here"<<endl;
    if (i<=7){
    ++i;
    count(w, i);
    }
    }
    }
    }
    }

    //When I run this program the value of i increases even after it reaches 7 and *wPtr is 1024.

    What is the mistake here?
    Thank you in advance and sorry once again for my mistake in the previous post.

  9. #9
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Simple question.

    First you should do a proper indentation and put your code between [code] ... [/code] tags.

    Code:
    void count(int, int);
    int main()
    {
        int w=16, i=0;
        count(w, i);
        return 0;
    }
    
    void count(int w, int i)
    {
        int *wPtr;
        wPtr = &w;
        while (*wPtr<=1024)
        {
            for (int n=0; n<=*wPtr; n++)
            {
                cout<<" i = "<<i<<endl;
                cout<<" n = "<<n<<endl;
                if (n==*wPtr) 
                {
                    *wPtr = (*wPtr) * 2;
                    cout<<"reset w here"<<endl;
                    if (i<=6){
                        ++i;
                        count(w, i);
                    }
                    else
                        break;
                }
            } 
        }
    }

    If done so far you should consider to drop the recursive call to count function. Instead you should see that your task can be made with 2 nested loops: the most outer loop uses i for loop counter and runs from 0 to 6 (inclusively). In the step of the outer loop you also can calculate next w by w=w*2, so that w = 16 to 1024 in the outer loop.The inner loop uses n for loop counter and runs from 0 to w.

    Are you aware that the number of outputs are 17 in the first cycle (0, 1, ... 16), 33 in the next and so on?

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