C++. Write a program that copies the contents of one array into another array bu
ID: 674371 • Letter: C
Question
C++. Write a program that copies the contents of one array into another array but in reverse order using pointers.
- in main()
- define 2 arrays of type int, 10 elements each
- initialize one array with numbers 1 through 10
- initialize all elements of the second array to 0
- call function reverseCopy with both arrays as arguments
- display the values of array number 2 (reversed order)
- reverseCopy function
- use pointers ONLY to reference the elements of the arrays
- no array subscripts allowed!!!
- use a loop to copy the elements of array 1 into array 2
- in reverse order
- element 10 of array 1 copied to element 1 of array 2
- element 9 of array 1 copied to element 2 of array 2
- and so on till all elements are copied
Explanation / Answer
#include<stdio.h>
void reverseCopy(int arr1[10],int arr2[10])
{
int j=0;
for(int i=9;i>=0;i--)
{
*(arr2+j) = *(arr1+i);
j++;
}
printf("Content of array 2 : ");
for(int i=0;i<10;i++)
printf("%d ",*(arr2+i));
}
int main()
{
int arr1[10] = {1,2,3,4,5,6,7,8,9,10};
int arr2[10] = {0,0,0,0,0,0,0,0,0,0};
reverseCopy(arr1,arr2);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.