# Vectors Help! :(

• 07-10-2009
xforevertink
Vectors Help! :(
I'm trying to only change the code within the function "partial_sum". but it doesn't seem to return the total. What am i doing wrong? :(

Code:

```#include<iostream> #include<vector> #include<exception> #include<stdexcept> #include<string> using namespace std; int partial_sum(vector<int> v, int n) {         {  int  total = 0;                 for (int i = 0; i < v.size(); i++)                         total += v[i];                 return total;                         }                 return false; } bool compare_vectors(vector<int> v1, vector<int> v2); string test(bool); int partial_sum(vector<int>, int); int main() {             vector<int> v;     v.push_back(4);     v.push_back(20);     v.push_back(-1000);     v.push_back(300);     v.push_back(1);     v.push_back(20);         v.push_back(1);                 bool exception_thrown = false;     cout << "partial_sum() - Test 1: " << test(partial_sum(v, 1) == 4) << endl;     cout << "partial_sum() - Test 2: " << test(partial_sum(v, 2) == 24) << endl;             exception_thrown = false;     try {         partial_sum(v, 8);     }     catch (runtime_error& e) {         exception_thrown = true;     }     cout << "partial_sum() - Test 3: " << test(exception_thrown) << endl;         } bool compare_vectors(vector<int> v1, vector<int> v2) {     if (v1.size() != v2.size()) return false;             for (int i = 0; i < v1.size(); ++i) {         if (v1[i] != v2[i]) return false;     }     return true; } string test(bool expression) {     if (expression) return "Passed";     else return "Failed"; }```
• 07-10-2009
bithub
I don't know what that return false is doing in the function, but that's wrong.

Also, what makes you say that partion_sum is not returning the correct total? You are never printing out the return of partion_sum, so how can you be sure?
• 07-10-2009
xforevertink
Sorry, the code is a bit messy. :[
• 07-10-2009
xforevertink
I'm trying to return the partial sum that the text below in the main function is asking for.
Code:

``` #include<iostream> #include<vector> #include<exception> #include<stdexcept> #include<string> using namespace std; int partial_sum(vector<int> v, int n) {         {  int  total = 0;                 for (int i = 0; i < v.size(); i++)                         total += v[i];                 return total;                         }                 return false; } bool compare_vectors(vector<int> v1, vector<int> v2); string test(bool); int partial_sum(vector<int>, int); int main() {             vector<int> v;     v.push_back(4);     v.push_back(20);     v.push_back(-1000);     v.push_back(300);     v.push_back(1);     v.push_back(20);         v.push_back(1);                 bool exception_thrown = false;     cout << "partial_sum() - Test 1: " << test(partial_sum(v, 1) == 4) << endl;     cout << "partial_sum() - Test 2: " << test(partial_sum(v, 2) == 24) << endl;             exception_thrown = false;     try {         partial_sum(v, 8);     }     catch (runtime_error& e) {         exception_thrown = true;     }     cout << "partial_sum() - Test 3: " << test(exception_thrown) << endl;         } bool compare_vectors(vector<int> v1, vector<int> v2) {     if (v1.size() != v2.size()) return false;             for (int i = 0; i < v1.size(); ++i) {         if (v1[i] != v2[i]) return false;     }     return true; } string test(bool expression) {     if (expression) return "Passed";     else return "Failed"; }```
But it keeps coming out in the terminal as failed....
• 07-10-2009
grumpy
You can remove the "return false;" from partial_sum() for a starter (and eliminate an extraneous pair of curly braces). However, that won't change the results - it's just tidying the code.

Your partial_sum() function loops over the whole vector, and ignores the second argument. With the vector v you have populated in main(), the function will always return -654.

However, the tests you're doing in main() imply that you expect different results than partial_sum() will do. You either need to fix the test cases, or change the function to do whatever it is you expect to happen.
• 07-10-2009
Hussain Hani
also just to point out something, you should pass the vector as a constant reference
• 07-11-2009
iMalc
Your main problem is that you're reinventing the wheel instead of embracing the standard library.
First off, replace your partial_sum with std::accumulate, or at least use that within your partial_sum.
Second off replace compare_vectors with just using bool operator==(const vector&, const vector&), or failing that, at least use std::equal for part of it.

Turn your warning levels up too. You should have already been made aware of your unused parameter 'n' to partial_sum.

Of course it comes through as failed. No exception was thrown so exception_thrown is false, thus test returns "failed". Were you wanting it to return "failed" only if an exception was thrown?