-1

I am making a program in which the program takes 3 numbers as input: "l", "r" and "a". I get all the values of "x" between l and r, (l and r inclusive). example, l = 1, r = 3, x values are 1, 2, 3. so now I have a function, f(n) = ((x/a) + (x % a)),(note: [x/a] is rounded down to an integer). so I have implemented this in c++ and my code is below.

#include<iostream>
using namespace std; 

int main()
{
    int l; 
    int r; 
    int a; 
    cin>>l>>r>>a; 
    int nums[(r-l)+2]; 
    int answers[(r-l)+2]; 
    for (int i = 1; i < (r-l)+2; i++)
    {
        nums[i] = i; 
    }
    for (int i = 1; i < sizeof(nums)/sizeof(nums[0])-1; i++)
    {
        answers[i] = ((nums[i]/a) + (nums[i] % a)); 
    }
    int j = 0; 
    j = answers[0];
  for (int i = 0; i < sizeof(answers); i++)
    {
       if (j < answers[i])
       {
            j = answers[i];
        }
   }
    cout<<j; 
}

but whenever I run this code, I get huge random numbers like 230984084 and all.So please point out what's wrong with my Code. Thanks in advance.

  • 5
    [Note, that this isn't valid c++.](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – πάντα ῥεῖ May 24 '22 at 10:48
  • Use `std::vector` instead of arrays, can make things convinient. – kiner_shah May 24 '22 at 10:51
  • Also at `j = answers[0];` - `answers[0]` is never assigned a value. – Richard Critten May 24 '22 at 10:52
  • Also, not sure why you need to store values in `num`, why not just have a loop that runs from l to r. – kiner_shah May 24 '22 at 10:53
  • You might ask yourself why one time `sizeof(nums)/sizeof(nums[0])-1` and one time `sizeof(answers)` is used. Check [range based for loop](https://en.cppreference.com/w/cpp/language/range-for) – stefaanv May 24 '22 at 11:11
  • sorry, I just started learning c++ a few days ago. Thanks for the tips and help guys – Levi Israel May 24 '22 at 11:18
  • 2
    @LeviIsrael The point is that one of the successes of C++ is that it started from C, but it moved on quite a bit and the code in your question is still mostly C-code. Here is an example how it could be handled in C++ with more knowledge needed but less chance for errors: https://coliru.stacked-crooked.com/a/fdadf81f488bf44f – stefaanv May 24 '22 at 12:58
  • And use `at()` instead of `[]` with `vector`. – Goswin von Brederlow May 24 '22 at 16:27
  • Okay @stefaanv, would you mind posting your comment as an answer, so that i can mark it as an answer and everyone knows it – Levi Israel May 27 '22 at 06:01

1 Answers1

1

Request from Levi to post my comment as answer:

The point is that one of the successes of C++ is that it started from C, but it moved on quite a bit and the code in your question is still mostly C-code. Here is an example how it could be handled in C++ with more knowledge needed but less chance for errors:

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <numeric>

int main()
{
    std::cout << "Give 'left', 'right' and 'process' values: \n";
    int l, r, a; 
    std::cin >> l >> r >> a; 

    std::vector<int> nums((r-l)+2); 
    std::vector<int> answers; 
    // fill container with ascending numbers
    std::iota(nums.begin(), nums.end(), 1);
    // transform as needed
    std::transform(nums.begin(), nums.end(), std::back_inserter(answers), [a](int i) { return i/a + i%a; });
    // find maximum element in container (returns iterator to element)
    auto maxv = std::max_element(answers.begin(), answers.end()); 
    std::cout << *maxv; 
}
stefaanv
  • 14,072
  • 2
  • 31
  • 53
  • okay thanks, but 1 doubt, can I just use #include instead of the first five lines? – Levi Israel May 30 '22 at 08:12
  • @LeviIsrael technically, you can use `#include` but it is not the best habit when you start making larger programs and you want to be more explicit about dependencies. – stefaanv May 30 '22 at 08:55