Please help me with the error.
The code prints the same output "NO" every time. Please someone help me with what the error can be.
Code:
#include<iostream>
using namespace std;
int main()
{
int n,q,i,j;
int a,b,c,d;
cin >> n >> q;
int ar[n];
for(j=0;j<n;j++)
{
cin >> ar[j];
}
for(i=0;i<q;i++)
{
int l,r;
cin >> l >> r;
ar[l-1]=a;
ar[l]=b;
ar[r-2]=c;
ar[r-1]=d;
if(a<b)
{
if(c>d)
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
}
else
{
if(c<d)
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
}
}
return 0;
}
Re: Please help me with the error.
Quote:
Originally Posted by
gamerrk1004
what the error can be.
Code:
cin >> n >> q;
int ar[n];
I think this is the error. You cannot declare a C array to be of a certain size n by reading n at runtime. Instead n must be available at compile-time already. It means n must be a constant.
As an alternative you can use a dynamic array. If you do, a good choice is std::vector. It can change size at runtime (both grow and shrink).
Re: Please help me with the error.
Quote:
Originally Posted by
gamerrk1004
The code prints the same output "NO" every time. Please someone help me with what the error can be.
...
1. Next time, please use CODE tags while posting code snippets.
2. Are you using plain C? Or C++? If the latter then you should use std classes as wolle already mentioned.
Re: Please help me with the error.
As the program seems to compile and run, what compiler/version are you using? - as:
is not valid (as per Wolle's post #2) standard C++ (and from the first 2 statements it looks like C++ rather than C?)
What is the code supposed to do? We've not going to work through the code to try to understand what it does! Give an input example and the expected output.
Re: Please help me with the error.
Code:
$ g++ -Wall -Wextra -O2 foo.cpp
foo.cpp: In function ‘int main()’:
foo.cpp:18:16: warning: ‘a’ may be used uninitialized in this function [-Wmaybe-uninitialized]
ar[l-1]=a;
^
foo.cpp:19:14: warning: ‘b’ may be used uninitialized in this function [-Wmaybe-uninitialized]
ar[l]=b;
^
foo.cpp:20:16: warning: ‘c’ may be used uninitialized in this function [-Wmaybe-uninitialized]
ar[r-2]=c;
^
foo.cpp:21:16: warning: ‘d’ may be used uninitialized in this function [-Wmaybe-uninitialized]
ar[r-1]=d;
^
One problem would be computing with garbage.
> ar[l-1]=a;
Did you mean
a = ar[l-1]; ?