6.11.5: Functions with vector parameters. CODE ++ ONLY Define a function isSumEqual() that takes one integer vector para

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

6.11.5: Functions with vector parameters. CODE ++ ONLY Define a function isSumEqual() that takes one integer vector para

Post by answerhappygod »

6.11.5: Functions with vector parameters. CODE++ ONLY
Define a function isSumEqual() that takes one integer vectorparameter and one integer parameter. The function computes the sumof the vector's elements. Then, the function returns true if thesum is equal to the integer parameter, and returns falseotherwise.
Ex: If the input is 4 6 -3 10 -4 10, the vector has 4 elements{6, -3, 10, -4}, and the integer parameter is 10. Then, the outputis:
False, the sum of all the elements is not equal to 10.
CODE:
#include <iostream>#include <vector>using namespace std;
bool isSumEqual(vector<int> inVector, int x){ int sum=0; for(auto & i : inVector) { if(i>x){ sum+=i; } }
if(sum%2!=0){ return true; }
else{ return false; }}
int main() { vector<int> inVector; int size; int input; int i; int x; bool result;
// Read the vector's size, and then the vector'selements cin >> size; for (i = 0; i < size; ++i) { cin >> input; inVector.push_back(input); }
cin >> x;
result = isSumEqual(inVector, x);
if (result) { cout << "True, the sumof all the elements is equal to " << x << "." <<endl; } else { cout << "False, the sumof all the elements is not equal to " << x << "."<< endl; }
return 0;}
6 11 5 Functions With Vector Parameters Code Only Define A Function Issumequal That Takes One Integer Vector Para 1
6 11 5 Functions With Vector Parameters Code Only Define A Function Issumequal That Takes One Integer Vector Para 1 (35.26 KiB) Viewed 42 times
For input 4 6-3 10-410, the first integer, 4, is the length of the vector. The next four values are the vector's elements, (6,-3, 10,-4}. The last input value, 10, is the integer parameter. The function finds that the sum of all the vector's elements is 9. 9 is not equal to 10, so the function returns false. Thus, the program outputs: False, the sum of all the elements is not equal to 10. Not all tests passed. ✓ 1: Compare output Input Your output X 2: Compare output Input Output differs. See highlights below. Special character legend Your output Expected output X 3: Compare output Input Your output Expected output ✓4: Compare output Input Your output Output differs. See highlights below. Special character legend X 5: Compare output Input Your output Expected output X 6: Compare output 4 6 -3 10 -4 10 False, the sum of all the elements is not equal to 10. Input Your output 6-12 -9 25 3 4 2 3 Expected output True, the sum of all the elements is equal to 3. False, the sum of all the elements is not equal to 3. Output differs. See highlights below. Special character legend 4 6 2 3 1 2 True, the sum of all the elements is equal to 2. False, the sum of all the elements is not equal to 2. 9 -15 22 23 25 -17 -7 -5 -4 -6 -5 False, the sum of all the elements is not equal to -5. 8-17 17 25 -11 18 0 1 -1 0 Output differs. See highlights below. True, the sum of all the elements is equal to 0. False, the sum of all the elements is not equal to 0. Special character legend 10 20 6 12 3 8 -11 24 -5 -4 -6 -5 True, the sum of all the elements is equal to -5. False, the sum of all the elements is not equal to -5.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply