-2

I was making an algorithm to solve Problem 1310 from the URI Online Judge and at some point i needed to delete an item from an array in an easy way, so i declared a vector, but, besides having no issues running whatsoever, my code doesn´t print anything using , cout doesn´t work at all.

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

using namespace std;

int Lucro(int c, int n, vector<int> r){
  if (n == 0){
    return 0;
  }else if (n == 1){
    return max(r[0] - c, 0);
  }

  int q;
  q = 0;
  for (int k = 0; k < n; k++){
    r.erase(r.begin() + k);
    q = max(q, r[k] - c + Lucro(c, n-1, r));
  }
  return q;
}

int main()
{

  int C, N, items;
  cin >> N >> C;
  vector<int> R;

  for (int i = 0; i < N; i++){
    cin >> items;
    R.push_back (items);
  }

  cout << Lucro(C, N, R) << endl;
  cout << 'test' << endl;
}

As i´m quite new to using vectors in c++, could someone please explain to me what´s going on and how to fix it?

Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42
Mateus Buarque
  • 255
  • 1
  • 2
  • 9
  • @Amadeus still doesn´t print neither the result nor the "test" text. – Mateus Buarque Mar 03 '18 at 14:04
  • @Ron how can i avoid doing that? – Mateus Buarque Mar 03 '18 at 14:06
  • 1
    I ran your code on an online compiler. A segmentation fault is thrown during one of the erase operations. Also, you should use double qoutes on your 'test' string. – Rann Lifshitz Mar 03 '18 at 14:06
  • 1
    Please have a look at this [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) list. – Ron Mar 03 '18 at 14:07
  • 1
    You want to enable compiler warnings so it tells you things like [this warning](http://coliru.stacked-crooked.com/a/b0f58e416e32dd50) so you notice that `cout << 'test'` should have been `cout << "test"`. – nwp Mar 03 '18 at 14:08
  • here is an alternative to erase I saw here : https://stackoverflow.com/questions/875103/how-do-i-erase-an-element-from-stdvector-by-index template< typename TContainer > static bool EraseFromUnorderedByIndex( TContainer& inContainer, size_t inIndex ) { if ( inIndex < inContainer.size() ) { if ( inIndex != inContainer.size() - 1 ) inContainer[inIndex] = inContainer.back(); inContainer.pop_back(); return true; } return false; } – Rann Lifshitz Mar 03 '18 at 14:08
  • @Mateus Buarque In any case the function is wrong. For example when n is equal to 1 then there is used the maximum of two return max(r[0] - c, 0); While when n is greater than 1 then there is used just r[k] - c instead of the maximum. – Vlad from Moscow Mar 03 '18 at 14:19
  • And when you used your debugger to step through your code, one line at a time, as it executes, and examine the values of all variables, and why they're set to what they are, what observations did you make? You do know, of course, that knowing how to use a debugger to analyze a program's logical execution flow, is a mandatory skill for every C++ developer, right? So, if you have not yet debugged your program yourself, why not? – Sam Varshavchik Mar 03 '18 at 14:19
  • @Mateus Buarque Describe in the question what the function should do. – Vlad from Moscow Mar 03 '18 at 14:19

1 Answers1

1

Most likely your program crashes because you are getting out-of-range of your vector by erasing elements and not decrementing the size n:

for (int k = 0; k < n; k++){
    r.erase(r.begin() + k);  //decrement n after this
    q = max(q, r[k] - c + Lucro(c, n-1, r));
}

In any case you shouldn't really have n variable at all, you should use r.size() to always get the current size of the vector.

r3mus n0x
  • 5,954
  • 1
  • 13
  • 34