In this assignment, you are going to turn in a single cpp file. You are going to
ID: 3860796 • 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). 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. 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
Task 1: Function which will accept an array and size of an array,n and a number K. Returns true if all number in an array is strictly less than number K.
bool ArrayCompare(int *arr, int n, int k) {
for (int i = 0;i< n;i++){
if(*(arr+i)<k) {
return false;
}
}
return true;
}
Task 2:
Below function return the dot product, which array (int *a and its size n) and (int *b and its size m). If m and n are not equal then print "dimension error" and return dotProduct as -32768
int DotProduct(int *a, int n, int *b int m) {
int dotProduct = 0;
if( n != m ) { // error check
int dotProduct = -32768;
cout<<"dimesnion error"<<error;
} else {
for (int i = 0; i<n;i++) {
dotProduct += a[i]*b[i];
}
}
retrun dotProduct
}