Please help cpp code In this assignment, you are going to turn in a single cpp f
ID: 3860946 • Letter: P
Question
Please help cpp code
In this assignment, you are going to turn in
a single
cpp file. You are going to complete the
following two tasks (unrelated).
Task 1:
In this task, you are going to write a boolean function that determines whether all ele-
ments in an array are strictly less than a value. In particular, the function takes inputs an integer
point to an array int *
a
, an integer int
n
as the array’s dimension/size, and an integer int
K
. The
function returns true if all elements of the array are strictly less than
K
, and otherwise false.
Task 2:
In this task, you are going to write a function that computes inner product of two vectors
(represented by arrays). In particular, the function takes four inputs: int *
a
, int
n
, int *
b
, int
m
, and the function returns an integer which is supposed to be their inner product. Here
a
is a
pointer to the first array (vector), and
n
is its dimension;
b
is another pointer to the second array
(vector), and
m
is its dimension. Here you need to consider the case where
n
and
m
do not match.
In this case, the inner product is not well-defined, and the function should print “dimension error”
on screen and return any arbitrary value.
For those who don’t know inner products, look at
“
http://en.wikipedia.org/wiki/Dot_product
Explanation / Answer
vector.cpp
#include <iostream>
using namespace std;
//function declaration
bool findLessThan(int *a,int n,int k);
void innerProduct(int *a,int n,int *b, int m);
int main()
{
int n,k,m;
int a[20],b[20];
//get input for boolean function
//get array size and array elements and the integer
cout << "Enter the array size: ";
cin>>n;
cout<<"Enter the array element: ";
for(int i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the number: ";
cin>>k;
if(findLessThan(a,n,k)) //call the boolean function
cout<<"All the elements in the array are less than the "<<k<<endl;
else
cout<<"All the elements in the array are not less than the "<<k<<endl;
//get input to find inner product
cout<<" To find the inner product: "<<endl;
//get first array size and array elements
cout<<"Enter the first array size: ";
cin>>n;
cout<<"Enter the array element: ";
for(int i=0;i<n;i++)
cin>>a[i];
//get second array size and array elements
cout<<"Enter the second array size: ";
cin>>m;
cout<<"Enter the array element: ";
for(int i=0;i<m;i++)
cin>>b[i];
innerProduct(a,n,b,m); //call the inner product function
return 0;
}
//boolean function to find the array element is less than given integer
bool findLessThan(int *a,int n,int k)
{
int flag=0;
for(int i=0;i<n;i++){
if(a[i]>k)
flag=1;
}
if(flag)
return false;
else
return true;
}
//function to find the inner product
void innerProduct(int *a,int n,int *b, int m)
{
int product=0;
if(m==n)
{
for(int i=0;i<m;i++)
product+=(a[i]*b[i]);
cout<<" The inner product of two arrays: "<<product;
}
else
cout<<" The array size are not same";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.