In this assignment, you are going to turn in a single cpp file. You are going to
ID: 3858567 • Letter: I
Question
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 elements 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
Below is your program containing both the functions: -
#include<iostream>
using namespace std;
bool isStriclyLess(int *a,int n,int k) {
for(int i = 0;i < n; i++) {
if(a[i] >= k) {
return false;
}
}
return true;
}
int dot_product(int *a,int n,int *b,int n1)
{
int product = 0;
if(n != n1) {
cout<<" Dimention error"<<endl;
return product;
}
for (int i = 0; i <= n-1; i++)
product = product + (a[i])*(b[i]);
return product;
}
int main() {
int arr[] = {2,3,4,7,8,7,8};
int size = sizeof(arr) / sizeof(arr[0]);
int arr1[] = {2,3,4,5,6,7,8};
int size1 = sizeof(arr1) / sizeof(arr1[0]);
int k1 = 6;
int k2 = 9;
cout<<"Is array strictly less than 6 ? "<<std::boolalpha<<isStriclyLess(arr,size,k1)<<endl;
cout<<"Is array strictly less than 9 ? "<<std::boolalpha<<isStriclyLess(arr,size,k2)<<endl;
cout<<"Dot product of array 1 and array 2 is "<<dot_product(arr,size,arr1,size1);
}
Smaple Run: -
Is array strictly less than 6 ? false
Is array strictly less than 9 ? true
Dot product of array 1 and array 2 is 225