0

The code below is showing two errors. They are the one belows: C4552 '>>' operator has no effect; expected operator with side-effect expression must have constant value; expected operator with side-effect (related to the variable 'size'

The code is the one below:

#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;


int main()
{
    int size;
    cin >> size;
    int lectures[size][2];
    for (int i = 0; i < size; i++) {
        cin >> lectures[i][0] >> lectures[i][1];
    }

    int n = sizeof(lectures) / sizeof(lectures[0]);

    int prefix_sum[size] = { 0 };


    for (int i = 0; i < n; i++) {
        prefix_sum[lectures[i][0]]++;
        prefix_sum[lectures[i][1] + 1]--;
    }

    int ans = prefix_sum[0];

    for (int i = 1; i < size; i++) {
        prefix_sum[i] += prefix_sum[i - 1];
        ans = ans < prefix_sum[i] ? prefix_sum[i] : ans;
        
    }

    cout << ans;

    return 0;
}

Can someone please help me to understand what is wrong here? Any help is welcome! Thank you! :)

rioV8
  • 24,506
  • 3
  • 32
  • 49
  • 1
    `int lectures[size][2];` is a variable-length array, and C++ [doesn't have them](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). Use `std::vector` instead. Same with `prefix_sum`. – Some programmer dude Dec 11 '21 at 11:44
  • And if you have the size of the arrays in `size`, why do you need `n`? – Some programmer dude Dec 11 '21 at 11:46
  • Could you indicate which line of your code is causing the error? – Christian Halaszovich Dec 11 '21 at 11:52
  • 4
    VLAs (see 1st comment) and remove `using namespace std;` as it's causing a clash between `size` and `std::size`. Have a read of [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Richard Critten Dec 11 '21 at 11:53

0 Answers0