0

This is a problem that's part of a Coursera Algorithms course I am enrolled in. This is not to ask for an answer to the question, but I have been getting nowhere with solving it at all because my code (in the function) runs to the line before the for loop line and then it exits. This is the code:

#include <iostream>
#include <vector>
#include <algorithm>

using std::vector;
using namespace std;

double get_optimal_value(int capacity, vector<int> weights, vector<int> values) {
  double value = 0.0;
  vector<int> rates;
  int len = end(weights) - begin(weights);
  cout << "pre-loop (values loop)" << endl; //the last line the code executes
  for (int i = 0; i < len; i++){
    rates[i] = values[i] / weights[i];
    cout << values[i] / weights[i] << endl; 
  }
  std::sort(begin(rates), end(rates), greater<int>());
  return value;
}

int main() {
  int n;
  int capacity;
  std::cin >> n >> capacity;
  vector<int> values(n);
  vector<int> weights(n);
  for (int i = 0; i < n; i++) {
    std::cin >> values[i] >> weights[i];
  }

  double optimal_value = get_optimal_value(capacity, weights, values);

  std::cout.precision(10);
  std::cout << optimal_value << std::endl;
  return 0;
}

The file itself is provided by the instructors, and we only have to work with the function that we're working on, not the main function.

  • 1
    It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [Debugging Guide](http://idownvotedbecau.se/nodebugging/) – NathanOliver Oct 26 '20 at 16:05
  • 3
    `rates[i] = values[i] / weights[i];` your `rates` has a size of 0 - you cannot access any of its indices – UnholySheep Oct 26 '20 at 16:05
  • @UnholySheep Oh yes sorry. I feel stupid now: I knew push_back exists and now I realise I should've used that. Thanks for the help! – hardbodybrain Oct 26 '20 at 16:10
  • 1
    Also you can just use `.size()` to find the length of a vector. – Anonymous1847 Oct 26 '20 at 16:31

2 Answers2

0
#include <iostream>
#include <vector>
#include <algorithm>

//using std::vector; // why this and you're using std in the next line
using namespace std;

double get_optimal_value(int capacity, vector<int> weights, vector<int> values) {//capacity is unused
    double value = 0.0, temp{}; 
    vector<double> rates;
    //int len = end(weights) - begin(weights); //using .size() instead
    cout << "pre-loop (values loop)" << endl; 
    for (int i = 0; i <weights.size(); i++) 
    {
        temp = static_cast<double>(values[i]) / static_cast<double>(weights[i]);
        rates.push_back(temp);
        cout << static_cast<double>(values[i]) / static_cast<double>(weights[i]) << endl; //to get the floating number because you're dividing
    }
    std::sort(begin(rates), end(rates), greater<double>());
    return value;
}

int main() {
    int n;
    int capacity;
    std::cin >> n >> capacity;
    vector<int> values(n);
    vector<int> weights(n);
    for (int i = 0; i < n; i++) 
    {
        std::cin >> values[i] >> weights[i];
    }

    double optimal_value = get_optimal_value(capacity, weights, values);

    std::cout.precision(10);
    std::cout << optimal_value << std::endl;
    return 0;
}

you can't add a value to a vector like you did because it's size it null, you have to grow it up so you can add values. I made some correction to your code (comments)

but you will still get always 0 as a return from your function because value doesn't change during the execution of get_optimal_value().

you have to be more specific so we can help you

ic_Engineer
  • 313
  • 3
  • 12
0

Explicitly using push_back is one solution; just for variation though, I'll note that in this case, you could just as easily initialize the new vector from one of your existing vectors (type conversions from int to double work just fine), and then do your math in roughly the same way, rather than building element by element:

double get_optimal_value(int capacity, const vector<int>& weights, const vector<int>& values) {  // Arguments made into const references to avoid pointless copies
  double value = 0.0;
  vector<double> rates(std::begin(values), std::end(values));  // Initialize to copy of values converted to double
  const auto len = weights.size(); // No need for iterator arithmetic; size tells you the size
  cout << "pre-loop (values loop)" << endl; //the last line the code executes
  for (size_t i = 0; i < len; i++){
    rates[i] /= weights[i]; // No need to use values anymore
    cout << rates[i] << endl; 
  }
  std::sort(begin(rates), end(rates), greater<int>());
  return value;
}
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271