Problem tracing -Code Output

Dear C++ Community

I do not seem to understand, the following code`s output even after tracing it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  void f(int* &c, int* d){
     *c =16; 
     c = c+2;
     *d = (*c)%3;
     d = c;
     *d = 2*(*(d-1));
 }

 int main(){
    int a[5] = {5, 10, 15, 20, 25};
    int* p = a;
    int* q = &a[4];
    f(p,q);
    for (int i=0; i<5; i++)
       cout << a[i] << " ";
       cout << "\n" << *p;
       cout << "\n" << *q;
    return 0;
}


Code output:
============

16 10 20 20 0
20
0

-----------------------------------------------------------------------------
My confusion:
=============

When tracing the code I got 16, but my answer I kept leaving a line due to "\n". I am not sure why the answer only leaves a line after printing five values (as the last two cout, seem to be included in the for loop ).

My answer:
==========

16
16
1
10
16
1
15
16
1
20
16
1
1
16
1

Last edited on
Line 17 will give an error, I assume you copied the code by hand you meant "<<" not ",,"

Either way, the reason it'll print out 5 numbers before starting a new line is because your for-loop is only looping through line 15 I suspect. Check on your code that the bracket on line 14 actually exists and ends somewhere.
Last edited on
@NovaPrimeVeera, did you mean this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;
 
void f(int* &c, int* d){  // on entry, c points to the [0] (i.e. first) and d to the [4] (i.e. last) element
    *c =16;               // array becomes { 16, 10, 15, 20, 25 }
    c = c+2;              // c now points to the [2] element (i.e. the third)    
    *d = (*c)%3;          // the last element becomes 15 % 3, which is 0; array now { 16, 10, 15, 20, 0 }
    d = c;                // both pointers are now pointing to the middle element
    *d = 2*(*(d-1));      // the middle element becomes twice the [1] (i.e. second ) element; array now { 16, 10, 20, 20, 0 }    
}

int main(){
   int a[5] = {5, 10, 15, 20, 25};
   int* p = a;
   int* q = &a[4];
   f(p,q);                // after this, array is { 16, 10, 20, 20, 0 };
                          // p passed by reference: points to [2] element on return; q passed by value: still points to last element
   for (int i=0; i<5; i++)
      cout << a[i] << " ";      // The loop ends HERE
       
   cout << "\n" << *p;    // the [2] (i.e. third) element is 20
   cout << "\n" << *q;    // the last element is 0
   return 0;
}


16 10 20 20 0 
20
0


Other than as a work-up to assembling flat-pack furniture, is there any particular purpose to this code?
Last edited on
Nope, our lecturer designed that code for us to trace it, but I did not understand his answer for the tracing. Thank you for the clarification.
Rather than get lost in alphabet soup, rename variables to be something meaningful (or at least as meaningful as you understand). If you get a better idea, then rename them again.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;
 
void f(int* &startOfArrayRef, int* endOfArray){ // on entry, startOfArrayRef points to the [0] (i.e. first) and endOfArray to the [4] (i.e. last) element
    *startOfArrayRef = 16;                      // array becomes { 16, 10, 15, 20, 25 }
    startOfArrayRef = startOfArrayRef+2;        // startOfArrayRef now points to the [2] element (i.e. the third)    
    *endOfArray = (*startOfArrayRef)%3;         // the last element becomes 15 % 3, which is 0; array now { 16, 10, 15, 20, 0 }
    endOfArray = startOfArrayRef;               // both pointers are now pointing to the middle element
    *endOfArray = 2*(*(endOfArray-1));          // the middle element becomes twice the [1] (i.e. second ) element; array now { 16, 10, 20, 20, 0 }    
}

int main(){
   int a[5] = {5, 10, 15, 20, 25};
   int* startOfArray = a;
   int* endOfArray = &a[4];
   f(startOfArray,endOfArray);                  // after this, array is { 16, 10, 20, 20, 0 };
                                                // startOfArray passed by reference: points to [2] element on return; 
                                                // endOfArray passed by value: still points to last element
   for (int i=0; i<5; i++)
      cout << a[i] << " ";      // The loop ends HERE
       
   cout << "\n" << *startOfArray;               // the [2] (i.e. third) element is 20
   cout << "\n" << *endOfArray;                 // the last element is 0
   return 0;
}
Topic archived. No new replies allowed.