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

Write a program that performs the following tasks. 1) Declare an int variable nu

ID: 3568657 • Letter: W

Question

Write a program that performs the following tasks.

1) Declare an int variable num and initialize it to 1000. Declare a pointer variable ptrNum of type int and let it point to num. Print out the address of ptrNum, the value stored in ptrNum, and the value stored in the memory location pointed to by ptrNum.

2) Declare an array of type int called arrayA with five elements, and initialize the elements to the odd integers from 1 to 9. Declare an array of type int called arrayB with five elements, and initialize the elements to the even integers from 2 to 10.

3) Declare a pointer variable ptrA that points to array arrayA. Declare a pointer ptrB that points to array arrayB.

4) Use a for statement to print out the elements of arrayA using array subscript notation (for example, arrayA[1] = 4) .

5) Use a for statement to print out the elements of arrayB using pointer offset notation

(for example, *(ptrA + 1) = 4).

6) Write a function swap to swap two arrays arrayA and arrayB. Two pointer variables are passed to the function swap by value or by reference.

7) Call functions swap and output the elements of two arrays after function call.

9) Practice on dynamic arrays. Ask user to specify the size of an array, create an integer array arrayD of that size and initialize it with user input data. Print out the elements of dynamic array arrayD.

10) Ask user to enter an integer and append that input data to the end of the array arrayD. Print out the elements of the new array.

Explanation / Answer

#include<iostream>
#include<stdio.h>

using namespace std;

void swap(int *a, int *b){
int temp;
for(int i=0; i<5; i++){
temp = *a;
*a = *b;
*b = temp;
*a++;
*b++;
}
}

int main(){
int num = 1000;
int *ptrNum = &num;
cout << "Address of ptrNum = " << &ptrNum << endl;
cout << "Value stored in ptrNum = " << ptrNum << endl;
cout << "Value stored at the memory location pointd by ptrNum = " << *ptrNum << endl;

int arrayA[5] = {1,3,5,7,9};
int arrayB[5] = {2,4,6,8,10};

int *ptrA = arrayA;
int *ptrB = arrayB;

for(int i=0; i< 5; i++){
cout << arrayA[i] << " ";
}
cout << endl;

for(int i=0; i< 5; i++){
cout << *ptrB << " ";
*ptrB++;
}
cout << endl;

swap(arrayA, arrayB);

cout << "After Swapping : ";
for(int i=0; i< 5; i++){
cout << arrayA[i] << " ";
}
cout << endl;

for(int i=0; i< 5; i++){
cout << arrayB[i] << " ";
}
cout << endl;

cout << "Enter size of array: ";
int size;
cin >> size;

int *arrayD = new int[size];
cout << "Enter values in array: ";
for(int i=0; i<size; i++){
cin >> arrayD[i];
}

for(int i=0; i< size; i++){
cout << arrayD[i] << " ";
}
cout << endl;

return 0;
}

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