![]() |
| | #1 |
| Registered User Join Date: Jul 2009
Posts: 13
| ![]()
|
| xforevertink is offline | |
| | #2 |
| Registered User Join Date: Sep 2004 Location: California
Posts: 2,845
| 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? |
| bithub is offline | |
| | #3 |
| Registered User Join Date: Jul 2009
Posts: 13
| Sorry, the code is a bit messy. :[ |
| xforevertink is offline | |
| | #4 |
| Registered User Join Date: Jul 2009
Posts: 13
| 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";
}
|
| xforevertink is offline | |
| | #5 |
| Registered User Join Date: Jun 2005
Posts: 1,343
| 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. |
| grumpy is offline | |
| | #6 |
| "Why use dynamic memory?" Join Date: Aug 2006
Posts: 179
| also just to point out something, you should pass the vector as a constant reference
__________________ "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup OS: Microsoft XP Media Center IDE: Microsoft Visual Studio 2005 pro edition Current Porject: Developing a 2D card game... |
| Hussain Hani is offline | |
| | #7 |
| Algorithm Dissector Join Date: Dec 2005 Location: New Zealand
Posts: 2,475
| 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?
__________________ My homepage Advice: Take only as directed - If symptoms persist, please see your debugger |
| iMalc is online now | |
![]() |
| Tags |
| sum, vector |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Vectors | naseerhaider | C++ Programming | 11 | 05-09-2008 08:21 AM |
| How can i made vectors measuring program in DevC++ | flame82 | C Programming | 1 | 05-07-2008 02:05 PM |
| How properly get data out of vectors of templates? | 6tr6tr | C++ Programming | 4 | 04-15-2008 10:35 AM |
| How to use Vector's in C++ !?! | IndioDoido | C++ Programming | 3 | 10-14-2007 11:13 AM |
| Points, vectors, matrices | subnet_rx | Game Programming | 17 | 01-11-2002 02:29 PM |