3. [Programming] [40 points] Given two arrays a and b with the same size, write
ID: 3594357 • Letter: 3
Question
3. [Programming] [40 points] Given two arrays a and b with the same size, write two (1) The 1 " function computes the dot product of the two arrays, le, the dot product (2) The 2" function computes the element-wise product of the two arrays. That is, the functions: result is an array with the same size as a or b. its elements are given by Hint: the function has prototype vold elementwise productfoat oll, float bl. float cl, size, t sire In the main function, compute the dot product and the element-wise product of the following two arrays float alSizEl-12.01, 4.0f, 1.0f,3.of, 5.or float bSzi|s { 40, 2.0f, 8 ot,-7.0t, 1.0 MacBook Air 0 F7Explanation / Answer
// this code is in cpp
#include<bits/stdc++.h>
using namespace std;
float dot_product(float a[],float b[],size_t size)
{
float ans=0.0f;
for(int i=0;i<size;i++)
{
ans+=(a[i]*b[i]);
}
return ans;
}
void elementwise_product(float a[],float b[],float c[],size_t size)
{
for(int i=0;i<size;i++)
{
c[i]=(a[i]*b[i]);
}
}
int main()
{
size_t size=5;
float a[size]={2.0f,4.0f,1.0f,3.0f,5.0f};
float b[size]={-4.0f,2.0f,8.0f,-7.0f,1.0f};
float c[size];
//here compute dot product and print
printf( "%lf ", dot_product(a,b,size));
//here compute elementwise product and print
elementwise_product(a,b,c,size);
for(int i=0;i<size;i++)
{
printf( "%lf ", c[i]);
}
cout<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.