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

Thread: Wierd output

  1. #1
    Join Date
    Feb 2008
    Posts
    28

    Wierd output

    Hi , i got a task , that was for the olympiad of my country in informatics and i wanted to solve a few tasks , and i started with one , it was fairly easy to figure it out , and i easily coded it , and it works , its great , yeah but the fun ends anfte the enterd number is 88 or more than i got no output and i dont knw the reason so i would like to find it out but its allready 36 hours sice i started asking and searchy why the heck... so the input is a simple two line input
    the number n
    and the number m wich is 1 or 2.
    my code looks like this
    Code:
    #include <fstream>
    using namespace std;
    int main()
    {
        long int n,m,rj,l,x;
        bool used[n];
        ifstream in("koder.in");
        ofstream out("koder.out");
        in>>n;
        in>>m;
        for (int i=1;i<=n;i++)
        {used[i]=false;}
        
        
        switch (m){
        case 1:
        l=n%3;
        rj=n/2;
        l=1-l;
    while (rj!=1)
    {
          for (int j=2;j<=n;j+=2)
          {
           
           if (used[j]==true)
           {continue;}
           
               else
               {
                   if(rj==1)
                   {break;}
                      if(l==0)
                      {
                      rj-=1;
                      used[j]=true;
                      l=1;
                      }
                      else
                      {
                         l=0;
                         continue;
                      }
               }
          }
    }
    for (int j=2;j<=n;j+=2)
    {
        if (used[j]==false)
        {out<<j<<endl;
        }
    }
    break;
    case 2:
        l=n%3;
        rj=n/2;
        l=1-l;
    while (rj!=1)
    {
          for (int j=1;j<=n;j+=2)
          {
           
           if (used[j]==true)
           {continue;}
           
               else
               {
                   if(rj==1)
                   {break;}
                      if(l==1)
                      {
                      rj-=1;
                      used[j]=true;
                      l=0;
                      }
                      else
                      {
                         l=1;
                         continue;
                      }
               }
          }
    }
    for (int j=1;j<=n;j+=2)
    {
        if (used[j]==false)
        {out<<j<<endl;
        }
    }
    break;
    }
    in.close();
    out.close();
    return 0;
    }
    I also tried it with vectors but no result from it either.I would appriciate youre help and thanks.

  2. #2
    Join Date
    Jun 2005
    Posts
    315

    Re: Wierd output

    Code:
     long int n,m,rj,l,x;
        bool used[n];                    //n is zero at this point
        ifstream in("koder.in");
        ofstream out("koder.out");
        in>>n;
        in>>m;
        for (int i=1;i<=n;i++)        //should be i < n
        {used[i]=false;}
    The array 'used' is declared with 0 entries, n was declared in the previous line and initialized by the compiler I think to 0. However, even if it was declared with some other value, the FOR loops used to access the array are looping too far. For an array of size n, the largest index can be (n-1).
    If you want a dynamically sized array, vectors are probably your best bet.

  3. #3
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Wierd output

    Quote Originally Posted by jeron
    Code:
     long int n,m,rj,l,x;
        bool used[n];                    //n is zero at this point
        ifstream in("koder.in");
        ofstream out("koder.out");
        in>>n;
        in>>m;
        for (int i=1;i<=n;i++)        //should be i < n
        {used[i]=false;}
    The array 'used' is declared with 0 entries, n was declared in the previous line and initialized by the compiler I think to 0. However, even if it was declared with some other value, the FOR loops used to access the array are looping too far. For an array of size n, the largest index can be (n-1).
    If you want a dynamically sized array, vectors are probably your best bet.
    Actually, the compiler will not initialize any of the variables to any value. So, if uninitialized, they will contain garbage.

    However, this is invalid, and shouldn't compile:
    Code:
    bool used[n];
    You cannot create a dynamic array in this manner.

    Viggy

  4. #4
    Join Date
    Feb 2008
    Posts
    28

    Re: Wierd output

    Quote Originally Posted by MrViggy
    Actually, the compiler will not initialize any of the variables to any value. So, if uninitialized, they will contain garbage.

    However, this is invalid, and shouldn't compile:
    Code:
    bool used[n];
    You cannot create a dynamic array in this manner.

    Viggy


    well the boolean part does compile , it actualy does go to n and not n-1 dunno why but i tested it and it printed out the right values but ill try it.ill post the results...



    result : nothing changed;
    Last edited by KRUNCH; March 5th, 2008 at 09:36 AM.

  5. #5
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Wierd output

    Quote Originally Posted by KRUNCH
    well the boolean part does compile , it actualy does go to n and not n-1 dunno why but i tested it and it printed out the right values but ill try it.ill post the results...



    result : nothing changed;
    No, it does not compile:
    Code:
    Comeau C/C++ 4.3.9 (Mar 27 2007 17:24:47) for ONLINE_EVALUATION_BETA1
    Copyright 1988-2007 Comeau Computing.  All rights reserved.
    MODE:strict errors C++ C++0x_extensions
    
    "ComeauTest.c", line 6: error: expression must have a constant value
          bool used[n];
                    ^
    
    "ComeauTest.c", line 5: warning: variable "x" was declared but never referenced
          long int n,m,rj,l,x;
                            ^
    
    1 error detected in the compilation of "ComeauTest.c".
    That's an error.

    Viggy

  6. #6
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Wierd output

    Hello KRUNCH,

    Code:
    long int n,m,rj,l,x;
        bool used[n];
    Believe it or not, this does compile with Dev-C++.
    This is a shocking to me because like Mr.Viggy said, I thought the array dimension must be the constant expression.

    but after thinking about it, I think it only compiled because C++ is statiscally typed language. (I'd really appreciate if someone could educatte me further on this issue).
    What I mean by this is that, as far as I could tell, those 2 lines of statements do not violate the syntax rules.
    on top of these,
    you are not doing anything with them even though they are in error.
    and you did give them proper values from ifstream.
    but if you do a quick test in between there, for example
    Code:
    bool used[n];
        cout << used[0]; << endl;
        // same as before
    you will clearly see that you did something wrong and it is a run-time error.

    so my advice to you is that listen to Mr.Viggy.

    hope this helped.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Wierd output

    Quote Originally Posted by potatoCode
    Hello KRUNCH,

    Code:
    long int n,m,rj,l,x;
        bool used[n];
    Believe it or not, this does compile with Dev-C++.
    It will not compile if you use the proper compiler switches. Dev-C++ uses the g++ compiler, and setting the ANSI switch to "on" will not make that code compile.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Wierd output

    I believe my compiler does gives out the error,
    it's been doing that up until I tried this.

    I checked the compiler setting, and searched the documentations about ANSI setting,
    so far the only ANSI setting I could tell is under C compiler option.
    is this what you're talking about?
    if not,
    how do I turn it on?

    Thanks.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Wierd output

    Quote Originally Posted by potatoCode
    I believe my compiler does gives out the error,
    it's been doing that up until I tried this.

    I checked the compiler setting, and searched the documentations about ANSI setting,
    so far the only ANSI setting I could tell is under C compiler option.
    is this what you're talking about?
    I would think so. Unless you're compiling using C99 language enabled, declaring an array using a variable as the index is illegal in both C and C++.

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Feb 2008
    Posts
    28

    Re: Wierd output

    well yes i solved the problem a bit earlier but i didnt have the time so didnt post , but yes i added a constant value (like 2 milions) but it only works for that far , after that it puts out a runtime error , but well it works , but i need it to go to like numbers like 50 ciphers so ill have to get a better algorithm.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured