-
September 27th, 2010, 10:51 AM
#1
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;
}
-
September 27th, 2010, 11:00 AM
#2
Re: Simple question.
Your use of pointers here is obscure. Why do you think you need them?
-
September 27th, 2010, 11:08 AM
#3
Re: Simple question.
Originally Posted by Lindley
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.
-
September 27th, 2010, 11:10 AM
#4
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.)
-
September 27th, 2010, 11:14 AM
#5
Re: Simple question.
Originally Posted by ritika_sharma82
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
-
September 27th, 2010, 11:18 AM
#6
Re: Simple question.
Originally Posted by ritika_sharma82
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.
-
September 27th, 2010, 11:39 AM
#7
Re: Simple question.
Originally Posted by ritika_sharma82
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?
-
September 27th, 2010, 08:18 PM
#8
Re: Simple question.
Originally Posted by jnmacd
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.
-
September 28th, 2010, 03:03 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|