Q1. Complete the definition of the average() function below. This function finds
ID: 3815976 • Letter: Q
Question
Q1. Complete the definition of the average() function below. This function finds the average of the five input arguments and returns the result.
double average( int a, int b, int c, int d, int e ){
..... }
Q2. Complete the definition of the maximum() function below. This function finds the largest input argument (the input argument which is larger than other four input arguments)
int maximum( int a, int b, int c, int d, int e ){
..... }
Q3. Complete the definition of the dotproduct() function below. This function finds the dot product of the input vectors v1 and v2.
double dotproduct(vector<double> v1, vector<double> v2){ … }
For instance, in the following main() function would print out 17 since the dot product of {1, -2, 6} and {3, 5, 4} would be the sum of elementwise multiplications: (1)*(3) + (-2)*(5) + (6)(4) = 17:
int main(){
vector<double> v1 {1, -2, 6};
vector<double> v2 {3, 5, 4};
cout << dotproduct( v1, v2 ) << endl;
return 0;
}
Can anyone help me with complete program of these Qs??
Explanation / Answer
Here are the three functions for you:
/*Q1. Complete the definition of the average() function below. This function finds the
average of the five input arguments and returns the result.*/
double average( int a, int b, int c, int d, int e ){
return (a + b + c + d + e) / 5.0;
}
//Q2. Complete the definition of the maximum() function below. This function finds the
//largest input argument (the input argument which is larger than other four input arguments)
int maximum( int a, int b, int c, int d, int e ){
if(a > b && a > c && a > d && a > e)
return a;
else if(b > a && b > c && b > d && b > e)
return b;
else if(c > a && c > b && c > d && c > e)
return c;
else if(d > a && d > b && d > c && d > e)
return d;
else
return e;
}
/*Q3. Complete the definition of the dotproduct() function below. This function finds
the dot product of the input vectors v1 and v2. */
double dotproduct(vector<double> v1, vector<double> v2){
double sum = 0;
for(int i = 0; i < v1.size(); i++)
sum += v1[i] * v2[i];
return sum;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.