Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab 6: Pointer Exploration Key Concepts: Pointers (Problem 10.1.1 from Knowlton\

ID: 3687062 • Letter: L

Question

Lab 6: Pointer Exploration

Key Concepts: Pointers

(Problem 10.1.1 from Knowlton's Introduction to Computer Science Using C++, modified)

Write a program that declares two variables of type double and two pointers of type double.

Do these steps:

1. First initialize the first pointer to point to the first double variable. Use the dereferencing operator to initialize the variable to 1.25.

2. Next initialize the second double variable to 2.5. Point the first pointer to this variable.

3. Now make the second pointer point to a different location in memory (unnamed) and make its pointee be 10.

4.Change the second pointer so that it now points to yet another different location in memory (unnamed) and make its pointee by twice the pointee of the other pointer.

5. Add a line of code to print the product of the two pointees that remain.

Before each step, give a comment that takes up a whole line that tells what step you're doing, e.g. "//STEP 1."

After each step,

Make a drawing on paper of memory. Show all variables and pointers (and links to pointees) that have been declared.

Print out the values of all variables that have been initialized. Print out all pointees that have been initialized. Label each of these outputs with what step they go with.

Leave a line of whitespace before the next step.

Explanation / Answer

Answer for Question :

// declared double var1, var2
double var1 , var2;
// declared double pointer var1_ptr, var2_ptr
double *var1_ptr, *var2_ptr;

// Initialised var1 with 1.25
var1 = 1.25;

// Initialised var1_ptr with dereference var1
var1_ptr = &var1;

// Initialised var2 with 2.5
var2 = 2.5;

// Initialised var1_ptr with var2
var1_ptr = &var2;

// Initialised var2_ptr point un named memory
// location point 10
*var2_ptr = 10;

// Initialised var2_ptr point un named memory
// location point 10
*var2_ptr = 20;

double result = *var1_ptr * *var2_ptr;


step 1:
var1_ptr is holding the var1 variable

Step 2:
var1_ptr is holding the var2 variable

Step 3:
var2_ptr is holding the unnamed 10

Step 4:
var2_ptr is holding the unnamed 20

Step 5:
result is holding the product of var1_ptr and var2_ptr