Question: C++ Every variable a computer program uses has to have a place in the computers main memory. This specific location is called its memory address.

C++
Every variable a computer program uses has to have a place in the computers main memory. This specific location is called its memory address. We can use pointers to store the memory address of variables. This will allow us to do some powerful things in the future, but for now, we will concentrate on just getting the addresses.
Instructions:
Open a new Visual Studio project. Open a new C++ source file. Inside your main function, insert the following code:
int x = 5;
int* px = &x;
int** ppx = &px;
The ampersand operator (&), means address of in this case. The asterisk (*) declares the variable to be a pointer to an integer. Therefore, the second line creates a pointer to an integer called px, and assigns it the address of x. The third line creates a pointer to a pointer to an integer called ppx, and assigns it the address of px. Write a statement to output the actual values of each variable (it will show the value of x, the address of x, and the address of px.)
Note: No output is required for Lab Task 1. When finished, just initial the space on the cover sheet and proceed to Task 2.
Lab Task 2 Asking for (in)Directions
(An exercise in using pointers)
Textbook Reference:
pp. 495-508
Given a pointer, we can actually find the value of the variable at the address stored in the pointer. This process is called indirection. It sounds messy, but C++ gives us one easy indirection operator, the asterisk (*).
Instructions:
Open a new Visual Studio project. Open a new C++ source file. Insert the following code in a main function:
int x = 5; x
int* px = &x;
px x
int** ppx = &px;
ppx px x
As mentioned in class it is possible to use px, to retrieve the value of x using the syntax:
*px
In this case, the asterisk is the indirection operator; it takes the address stored in the pointer, goes to that address, and returns the value stored at that address. Write a statement to output the value of x three ways: once using x, once using px (as above), and once using ppx (Hint: you will have to use the indirection operator more than once.)
Note: No output is required for Lab Task 2. When finished, just initial the space on the cover sheet and proceed to Task 3.
Lab Task 3 Arrays: Pointers in Disguise
(An exercise in using arrays as pointers)
Textbook Reference:
pp. 505-508
Up until now, arrays have always been arrays, and pointers have always been pointers. However, you can also treat the name of an array like a pointer.
An array stores itself in memory in addresses that are physically adjacent. If you use the array name, it gives you the address of the first element of the array.
Adding 1 to a pointer takes you to the next memory address based on the underlying type, so adding 1 to the array name would give you the address of the second element of the array.
Instructions:
Open a new Visual Studio project. Open a new C++ source file. Type the following code into a main function:
int array[10] = {3,8,5,4,6,9,1,7,0,2};
cout << *(array) << endl;
cout << *(array+1) << endl;
cout << *(array+5) << endl;
In the space below predict the output that the 3 cout lines above will produce.
Now run the program and compare the results with your predictions. If any of your predictions were incorrect, explain your misunderstanding and what you learned from the output of the program.
Now add a loop inside your main function (below the lines above) that uses the indirection operator to print the contents of the entire array from index 0 to index 9.
Note: No output is required for Lab Task 3. When finished, just initial the space on the cover sheet, make sure you have answered the questions in the spaces above, and proceed to Task 4.
Lab Task 4 Test Time
(An exercise in using arrays as pointers)
Textbook Reference:
pp. 495-541
Write a program that allows the analysis of exam scores for an undetermined number of students.
Instructions:
Open a new Visual Studio project. Open a new C++ source file. Write a program that does the following using pointer arithmetic notation rather than array notation wherever possible.
1. Dynamically allocate an array large enough to hold a user defined number of test scores. ( Hint: iptr = new int[num]; )
2. Allow the user to enter the scores into the array.
3. Pass the array to a function that sorts them into descending order (large to small).
4. Back in the main pass this sorted array to a second function that calculates the average score.
The program should display the sorted list of scores and the average with appropriate headings. Give special attention to making all output look professional.
Use the following scores as input:
Number of scores: 18
Scores (enter in this order): 87, 88, 100, 96, 72, 34, 56, 99, 81, 77, 62, 58, 84, 93, 98, 65, 90, 89.
Copy your Source code and Output to the end of this packet document. Be sure an easy to read comment at the top of the source code states: Lab Task 4. In addition have your program label the output page at the top with a line: Output Lab Task 4. Always label the output from lab tasks in a similar way.
Note: The text type document you turn in will contain every thing in this packet, with your initials on page 1 for all the tasks you have completed correctly, Lab Task 3 answers filled in, and will have the Task 4 source code and output as the last page(s).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!