-
January 10th, 2020, 03:03 AM
#1
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;
}
Last edited by VictorN; January 10th, 2020 at 04:33 AM.
Reason: added code tags
-
January 10th, 2020, 04:20 AM
#2
Re: Please help me with the error.
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).
Last edited by wolle; January 10th, 2020 at 07:08 AM.
-
January 10th, 2020, 04:37 AM
#3
Re: Please help me with the error.
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.
Victor Nijegorodov
-
January 10th, 2020, 04:50 AM
#4
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 11th, 2020, 03:04 AM
#5
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]; ?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|