78.1k views
5 votes
Write a function copy that takes two arrays A and B, both of size N. The function then copies all N values from A into B and then displays it.

1 Answer

5 votes

Answer:

Written in C++

#include<iostream>

using namespace std;

int main()

{

int N;

cout<<"Length of Array: ";

cin>>N;

int A[N], B[N];

for(int i =0;i<N;i++) {

cin>>A[i];

}

for(int i =0;i<N;i++) {

B[i] = A[i];

}

for(int i =0;i<N;i++) {

cout<<B[i]<<" ";

}

return 0;

}

Step-by-step explanation:

This line declares the length of the Array; N

int N;

This line prompts user for length of array

cout<<"Length of Array: ";

This line gets user input for length of array

cin>>N;

This line declares array A and B

int A[N], B[N];

The following iteration gets input for Array A

for(int i =0;i<N;i++) {

cin>>A[i];

}

The following iteration gets copies element of Array A to Array B

for(int i =0;i<N;i++) {

B[i] = A[i];

}

The following iteration prints elements of Array B

for(int i =0;i<N;i++) {

cout<<B[i]<<" ";

}

User Canvas
by
4.7k points