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....