Vectors Help! :(

This is a discussion on Vectors Help! :( within the C++ Programming forums, part of the General Programming Boards category; I'm trying to only change the code within the function "partial_sum". but it doesn't seem to return the total. What ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    13

    Unhappy 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";
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    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?

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    13
    Sorry, the code is a bit messy. :[

  4. #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";
    }
    But it keeps coming out in the terminal as failed....

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,299
    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.

  6. #6
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    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
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    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

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 11:13 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 01:29 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21