C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-10-2009, 04:12 PM   #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";
}
xforevertink is offline   Reply With Quote
Old 07-10-2009, 04:16 PM   #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   Reply With Quote
Old 07-10-2009, 04:17 PM   #3
Registered User
 
Join Date: Jul 2009
Posts: 13
Sorry, the code is a bit messy. :[
xforevertink is offline   Reply With Quote
Old 07-10-2009, 04:20 PM   #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....
xforevertink is offline   Reply With Quote
Old 07-10-2009, 04:22 PM   #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   Reply With Quote
Old 07-10-2009, 06:20 PM   #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   Reply With Quote
Old 07-11-2009, 03:20 PM   #7
Algorithm Dissector
 
iMalc's Avatar
 
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   Reply With Quote
Reply

Tags
sum, vector

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:53 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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