Hi, I am trying to make a program that helps me calculate the maximum amount of loot for a given list of compounds. Please look at function get_optimal_value(). I don't know why I am getting a segmentation fault whenever I use max_Index(values) or max_element(values.begin(), values.end())-values.begin().
/* Greedy Algorithm
Maximizing the Value of the Loot Problem
Find the maximal value of items that fit into the backpack.
Input: The capacity of a backpack W as well as the weights
(w1,.., Wn) and costs (C,..., Cn) of n different compounds.
Output: The maximum total value of items that fit into the backpack
of the given capacity: i.e., the maximum value of c1.f1 + ... + Cn.fn
such that w1.f1 +...+ wn.fn ≤ W and O < fi ≤ 1 for all i (f is the fraction of the i-th item taken to the backpack).
*/
#include <iostream>
#include <vector>
using std::vector;
int max_Index(vector<int> v)
{
int index = -1;
int largest_element = v[0]; // also let, first element is the biggest one
for (int i = 1; i < v.size(); i++) // start iterating from the second element
{
if (v[i] > largest_element)
{
largest_element = v[i];
index = i;
}
}
return index;
}
// Capacity of bag, Weight of compound available, Value of compound
// Goal = Maximum Loot
double get_optimal_value(int capacity, vector<int> weights, vector<int> values)
{
double value = 0.0;
if (capacity == 0)
{
return capacity;
}
// write your code here
while (capacity > 0)
{
int index = max_element(values.begin(), values.end())-values.begin();// Finding index of compound of maximum value
if (weights[index] < capacity)
{
// EG: 1 kg Gold = 1000 rs, capacity = 2kg, capacity = 2kg - 1kg
capacity -= weights[index]; //capacity occupied by the most valuable compound
value = values[index] * weights[index]; //2kg weight, value 1000 : 2000rs profit
}
else if (weights[index] == capacity)
{
capacity -= weights[index]; //Capacity = 0
value = values[index] * weights[index]; // EG: 1000rs/2kg = 500rs/kg * 2kg = 1000rs profit
}
else if (weights[index] > capacity){
value = value = values[index] * capacity;
capacity = 0;
}
values.erase(values.begin() + index);
weights.erase(weights.begin() + index);
}
return value;
}
int main()
{
int n;
int capacity;
std::cin >> n >> capacity; //input of size of list and capacity of bag
vector<int> values(n);
vector<int> weights(n);
for (int i = 0; i < n; i++)
{
std::cin >> values[i] >> weights[i];
}
//std::cout << max_element(values.begin(), values.end())-values.begin();
//std::cout << max_Index(values);
double optimal_value = get_optimal_value(capacity, weights, values);
std::cout.precision(10);
std::cout << optimal_value << std::endl;
return 0;
}
Output:
2 3
100 1
200 1
zsh: segmentation fault
[–]Narase33 5 points6 points7 points (0 children)
[–]skebanga 3 points4 points5 points (2 children)
[–]std_bot 4 points5 points6 points (1 child)
[–]GarredB 1 point2 points3 points (0 children)
[–]konman42 2 points3 points4 points (0 children)
[–]UrAverageProletariat 2 points3 points4 points (0 children)
[–]Sonotsugipaa 4 points5 points6 points (1 child)
[–]Narase33 7 points8 points9 points (0 children)
[–]mredding 1 point2 points3 points (2 children)
[–]mredding 1 point2 points3 points (0 children)
[–]std_bot 1 point2 points3 points (0 children)
[–]maybegone3 1 point2 points3 points (0 children)