Sourabh
Sourabh

Reputation: 53

The code doesn't gives me any output in Dev-C++

When I try to run this code in Dev-C++. There is no output. But when I run it in any online compiler, the code runs smoothly. Why it is not running in Dev-C++?

I have tried running it in various online compilers, where it is running smoothly.

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        long long int N, K;
        cin>>N>>K;
        long long int arr[N];
        for(long long i=0; i<N; i++)
        {
            cin>>arr[i];
        }

        long long int max = INT_MIN;
        long long int dp[N];

        for(int i=N-1; i>=0; i--)
        {
            if((i+K)>=N)
            {
                dp[i] = arr[i];
            }

            else
            {
                dp[i] = (dp[i+K]+ arr[i]);
            }
        }

        for(int k=0; k<N; k++)
        {
            if (dp[k]>max)
            {
                max = dp[k];
            }
        }
        cout<<max<<endl;
    }
    getchar();
    return 0;
}

Upvotes: 2

Views: 1432

Answers (2)

Alecto
Alecto

Reputation: 10740

Let's take a look at this line of the code:

long long int arr[N]; 

Because N isn't a compile-time constant, you're trying to create what's known asa variable-length array. Variable length arrays are valid in newer versions of C, but not in C++. It does compile under GCC as a non-standard extension, which is probably why it compiled on the online compiler.

We can do what you want just by using a vector (from #include <vector>)

std::vector<long long int> arr(N); //Create a vector of size N 

You can use a vector in the exact same way you'd use an array.

Upvotes: 4

user11031520
user11031520

Reputation:

MAy be due to long long int arr[N]; its not acceptable in DEV C++ Use std::vector<long long> arr(N);instead of it.

In DEV C++ 5.11 version it easily give output

#include<iostream>
#include<stdio.h>
#include<conio.h>


using namespace std;
int main()
{
    int T;


    cin>> T;
    while(T--)
    {
        long long int N, K;
        cin>>N>>K;
        long long int arr[N];
        for(long long i=0; i<N; i++)
        {
            cin>>arr[i];
        }

        long long int max = INT_MIN;
        long long int dp[N];

        for(int i=N-1; i>=0; i--)
        {
            if((i+K)>=N)
            {
                dp[i] = arr[i];
            }

            else
            {
                dp[i] = (dp[i+K]+ arr[i]);
            }
        }

        for(int k=0; k<N; k++)
        {
            if (dp[k]>max)
            {
                max = dp[k];
            }
        }
        cout<<max<<endl;
    }
    getchar();
    return 0;
}

Upvotes: 0

Related Questions