Please do not use any pointers in the program. Thank you. Write a program that m
ID: 3747892 • Letter: P
Question
Please do not use any pointers in the program. Thank you.
Write a program that multiplies two arrays. The arrays are multiplied on an element-by-element basis, e.g. [4, 2, 7] * [3, 5, 8] = [12, 10, 56]. The program should include the following function:
void multiply(int a1[], int n, int a2[], int a3[]);
The function multiplies element by element of an input array a1 of length n with an input array a2 of length n, stores the result to an output array a3.
Assume the input arrays have the same number of elements.
In the main function, declare the input arrays and the output array after reading in the number of element of the arrays, then read in the elements, then call the multiply function. The main function should display the output array.
Example input/output #1:
Enter the length of the arrays: 5
Enter the elements of the array #1: 3 4 7 14 9
Enter the elements of the array #2: 12 8 2 5 3
Output: 36 32 14 70 27
Explanation / Answer
#include<iostream>
using namespace std;
void multiply(int a1[], int n, int a2[], int a3[]);
int main()
{
int n;
cout<<"Enter the length of the arrays:";
cin>>n;
int a1[n],a2[n],a3[n];
cout<<"Enter the elements of the array #1: ";
for(int i=0;i<n;i++)
{
cin>>a1[i];
}
cout<<"Enter the elements of the array #2: ";
for(int i=0;i<n;i++)
{
cin>>a2[i];
}
multiply(a1,n,a2,a3);
cout<<"Output:";
for(int i=0;i<n;i++)
{
cout<<a3[i]<<" ";
}
return 0;
}
void multiply(int a1[],int n,int a2[],int a3[])
{
for(int i=0;i<n;i++)
a3[i]=a1[i]*a2[i];
}