Write C++ Program 2-1 Motivation: This is a simple project to print the dot prod
ID: 3789842 • Letter: W
Question
Write C++ Program
2-1 Motivation:
This is a simple project to print the dot product of two integer array of size N, where N is provided by user as input and can be very large.
The input format is like this:
4
1 2 3 4
4 5 6 7
where the first number is N, then followed by one line of input with N numbers, which is the first array; it will then read in another line of input with N numbers, which is the second array; The dot product of two arrays A and B is defined as:
For the above example, the dot product is 1 × 4 + 2 × 5 + 3 × 6 + 4 × 7 = 50.
2-2 Requirements
• You code need to be able to run multiple times, until the user input N as a negative number.
• You must create two functions: 1) ReadInput will create an array based on the argument, then read in all elements of an array; 2)
DotProduct to find the dot product of two arrays. All these functions should be declared in a header file, and implemented in a source file.
• Each run, you need to get N from the user, then call ReadInput twice, DotProduct once and print the result.
• You need to turn in three files: makefile, main.C, xArray.C, xArray.h.
2-2 Notes
• xArray.h holds the prototype of the two functions, like: int* ReadInput(int n); // n is the number of elements for an array int DotProduct(int* a, int* b, int length);
• You will gain 6 points if your code is fully functional as described, and with no memory violation and/or memory leaks. Your code should be readable, so please put comments in the code
A. B Alix BliExplanation / Answer
I just write source code.it may help to you.
#include<iostream>
using namespace std;
int main()
{
int n,i;
do
{
cout<<"enter array of size:";
cin>>n;
int A[n];
int B[n],sum=0;
cout<<"enter A elements: ";
for(i=0;i<n;i++)
cin>>A[i];
cout<<"enter B elements: ";
for(i=0;i<n;i++)
cin>>B[i];
for(i=0;i<n;i++)
sum=sum+A[i]*B[i];
cout<<"dot product is:"<<sum<<endl;
}while(n>0);
return 0;}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.