C++ Programming arrays, dot matrix Assume the following 2 arrays A[3]=[1,2,3} B[
ID: 643138 • Letter: C
Question
C++ Programming arrays, dot matrix
Assume the following 2 arrays
A[3]=[1,2,3}
B[3]={4,5,6}
Dot product of array A and B is
a[0] *b[0} + a[1]*b[1] + a[2]*b[2]
in this case, 1*4 + 2*5 + 3*16 = 30
The program will have one function
dot - The function requires 2 integer arrays and an integer for size of the array
It will return an integer
Declare and initialize a constant integer SIZE = 4
Declare and initialize 2 arrays in the main.
int A[SIZE]={1,2,3,4};
int B[SIZE]={5,6,7,8};
Call the function dot and show the following output in main - DO NOT HARDCODE THE OUTPUT. SIZE AND ELEMENTS SHOULD BE ABLE TO BE CHANGED
What it should look like:
C++ Programming arrays, dot matrix Assume the following 2 arrays A[3]=[1,2,3} B[3]={4,5,6} Dot product of array A and B is a[0] *b[0} + a[1]*b[1] + a[2]*b[2] in this case, 1*4 + 2*5 + 3*16 = 30 The program will have one function dot - The function requires 2 integer arrays and an integer for size of the array It will return an integer Declare and initialize a constant integer SIZE = 4 Declare and initialize 2 arrays in the main. int A[SIZE]={1,2,3,4}; int B[SIZE]={5,6,7,8}; Call the function dot and show the following output in main - DO NOT HARDCODE THE OUTPUT. SIZE AND ELEMENTS SHOULD BE ABLE TO BE CHANGED What it should look like:Explanation / Answer
#include <iostream>
using namespace std;
int dot(int A[], int B[], int size){
int sum = 0;
for(int i = 0; i < size; ++i){
sum += A[i] * B[i];
}
return sum;
}
void print(int arr[], int size){
cout << "{";
for(int i = 0; i < size; ++i){
cout << arr[i] << " ";
}
cout << "}";
}
int main(){
const int SIZE = 4;
int A[SIZE] = {1,2,3,4};
int B[SIZE] = {5,6,7,8};
cout << "The dot product of ";
print(A, SIZE);
cout << " ";
print(B, SIZE);
cout << " is " << dot(A, B, SIZE) << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.