dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Please help me with the error.

  1. #1
    Join Date
    Jan 2020
    Posts
    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

  2. #2
    Join Date
    Feb 2017
    Posts
    506

    Re: Please help me with the error.

    Quote Originally Posted by gamerrk1004 View Post
    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.

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,580

    Re: Please help me with the error.

    Quote Originally Posted by gamerrk1004 View Post
    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

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,972

    Re: Please help me with the error.

    As the program seems to compile and run, what compiler/version are you using? - as:

    Code:
    int ar[n];
    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++17 Compiler: Microsoft VS2019 (16.4.2)

  5. #5
    Join Date
    Nov 2018
    Posts
    28

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)