357mag 3 Posted 13 hours ago It seems I'm always off by one. This program changes the contents of an array using a function and a pointer. The problem is the while loop in the function. At some point the value of num has to become 0 for the loop to fail and stop printing numbers. But on paper when I plot this thing out, I have already printed all 10 squares 1 through 10, but the value in num is still 1. Shouldn't at that point the value of num be 0? But I keep getting 1. So it's like all 10 squares have been printed but the condition in my while loop looks like this: while(1) instead of... while(0) Here is the code. Sorry I have not changed the namespace stuff yet: void square(int *n, int num); int main() { int i, nums[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; cout << "Original array values: "; for(i = 0; i < 10; i++) cout << nums[i] << " "; cout << endl; square(nums, 10); cout << "Altered array values: "; for(i = 0; i < 10; i++) cout << nums[i] << " "; cout << endl << endl; system("pause"); return 0; } void square(int *n, int num) { while(num) { *n = *n * *n; num--; n++; } } Share this post Link to post
357mag 3 Posted 13 hours ago (edited) Plus I was trying to use the debugger so I could watch the value of num in the while loop but there is no step into command. Just step over and trace into. Actually, I've never used a debugger, so I'm new to that. I'm pretty close on this maybe my paperwork is just a bit off. Edited 12 hours ago by 357mag Share this post Link to post
Anders Melander 1840 Posted 7 hours ago 5 hours ago, 357mag said: Just step over and trace into. Trace Into = Step Into But you don't need to trace into. You can just place a breakpoint inside your square function. This is a very simple problem so I suggest you try to single-step through it "in your head" first; What happens when you call square(0), square(1), etc. Share this post Link to post
David Heffernan 2361 Posted 6 hours ago 6 hours ago, 357mag said: Plus I was trying to use the debugger so I could watch the value of num in the while loop but there is no step into command. Just step over and trace into. Actually, I've never used a debugger, so I'm new to that. I'm pretty close on this maybe my paperwork is just a bit off. Even if you don't use a debugger, get your program to print out the intermediate values in the function square. Then compare it to your expectation. Share this post Link to post
Remy Lebeau 1465 Posted 1 hour ago @357mag I don't understand what you are describing. Of course num reaches 0 or else the function would never exit. Proof: https://onlinegdb.com/bxMEvMyoc Share this post Link to post
357mag 3 Posted 1 hour ago Yes num reaches 0. I'm just saying when I put all this stuff on paper, it seems after the last square has been printed, there still is one iteration left to go. So I end up with while(1) instead of while(0). But it's possible that my paperwork was just off a little. I understand how the program works. Share this post Link to post
357mag 3 Posted 51 minutes ago I can't use Trace Into because this is what I see when I use it: Share this post Link to post
Remy Lebeau 1465 Posted 50 minutes ago (edited) 9 minutes ago, 357mag said: I'm just saying when I put all this stuff on paper, it seems after the last square has been printed, there still is one iteration left to go. So I end up with while(1) instead of while(0). That makes no sense. You should show your paper logic. Quote But it's possible that my paperwork was just off a little. Entry into function: num=10 while (10) square 1 num=9 while (9) square 2 num=8 while (8) square 3 num=7 while (7) square 4 num=6 while (6) square 5 num=5 while (5) square 6 num=4 while (4) square 7 num=3 while (3) square 8 num=2 while (2) square 9 num=1 while (1) square 10 num=0 while (0) break 10 iterations total, and num is 0 after the last iteration. It can't be 1. Edited 47 minutes ago by Remy Lebeau Share this post Link to post
Remy Lebeau 1465 Posted 28 minutes ago 17 minutes ago, 357mag said: I can't use Trace Into because this is what I see when I use it: Are you saying you get that when you put a breakpoint on the call to square() and then step into it? It should not be stepping into std code, let alone ntdll code. Are you sure you are not maybe stepping into the std::cout calls instead? Share this post Link to post