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

Make the following changes to your program at the indicated spots (you should no

ID: 3816381 • Letter: M

Question

Make the following changes to your program at the indicated spots (you should not use indexes anywhere in the program, only pointers):

1.Assign the address of the last element of the array to last.

2.Add code that prints the addresses of the array elements separated by spaces.

3.Add code that prints the values in the array in reverse order.

4.Add code that prints the every second value of the array.

5.Add code that prints every nth value of the array (where n is given by the user).

Here is the program:

Explanation / Answer

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
// initialize a 1 dimensional array
int arr [10] = {2, 4, 5, 6, 7, 8, 9, 10, 11, 13};
// pointer that moves thru an array to visit elements
int *mover,
*last; // a pointer to the last element of the array
int n; // n is a step size for moving thru the array

last = &arr[9];//pointer last store the address of the last element

cout << "The array in forward direction is: ";
// print array in forward direction
for (mover = arr; mover <= last; mover++)
cout << *mover << " ";//using * deference the pointer to give the value

cout << " The array addresses in forward are: ";
// print the array addresses in forward order
for (mover = arr; mover <= last; mover++)
cout << mover << " ";//mover is a pointer hence printts the address here

cout << " The array in reverse direction is: ";
// print array in reverse direction
for (mover = last; mover >= arr; mover--)
cout << *mover << " ";//printing from the last

cout << " Every second element of the array is: ";
// print every second element of the array
for (mover = arr; mover <= last; mover=mover+2)
cout << *mover << " ";

cout << " Enter an array step: ";
cin >> n;
cout << " Every nth element of the array is: ";
// print every nth element of the array
mover= arr;
cout << *mover + n << " ";// *mover +n gives the nth index value
cout << " ";

return 0;
}
------------output-----------
The array in forward direction is:   
2 4 5 6 7 8 9 10 11 13   

The array addresses in forward are:
0x7ffe9d90c420 0x7ffe9d90c424 0x7ffe9d90c428 0x7ffe9d90c42c 0x7ffe9d90c430 0x7ffe9d90c434 0x7ffe9d90c438 0x7ffe9d90c43c 0x7ffe9d90c440 0x7ffe9d90c444
  

The array in reverse direction is:   
13 11 10 9 8 7 6 5 4 2   

Every second element of the array is:
2 5 7 9 11   

Enter an array step: 3   

Every nth element of the array is:   
5

---------------output-----------

Note: Feel free to discuss about pointers if you have any doubts

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote