Thread: Can you find the error?

  1. #16
    Registered User
    Join Date
    Apr 2013
    Posts
    10
    I have found the error. The mistake is definitely not a compilation or presentation error, as I had mentioned before, There probably is some input for which I may not be getting the right output..

    I have found that type of input scenario and corrected my program to handle that case well. Here is a modification of my program that now works Thanks a lot guys!!

    Code:
    //110101 The 3n + 1 Problem
    # include <iostream>
    # include <fstream>
    using namespace std;
    int main()
    {
        int i,j,k,x,n1,n,l,h,maxcount,temp=0; /*x=index of counter, n=no fo elements in counter, maxcount is the max element from the counter*/
        while (cin>> i>>j)
        {
            if ((i<1000000) && (i>0) && (j<1000000) && (j>0))
            {
                if(i > j)
                    {l=j;h=i;} //l=low; h=high
                else
                    {l=i;h=j;}
                n=h-l;
                int  counter[n+1];
                for (x=0;x<=n;x++)
                    counter[x]=1;
                x=0;
                for (k=l;k<=h;k++)
                {
                    n1=k;
                    while (n1!=1)
                    {
                        if ((n1%2)==0)
                        {
                            n1= n1/2;
                            counter[x]=counter[x]+1;
                        }
                        else
                        {
                            n1= n1*3; n1++;
                            counter[x]=counter[x]+1;
                        }
                    }
                    x++;
                }
                maxcount=counter[0];
                for (x=1;x<=n;x++)
                if (counter[x]>maxcount)
                    maxcount=counter[x];
                cout <<i<<" "<<j<<" "<<maxcount<<"\n";
            }
            else cout<<"invalid input!";
        }
        return (0);
    }

    Well, yes needless to say, I have rewritten the program with proper functions and definitely much neat appearance (which is not posted here now). But I wanted to find my mistake in this program, hence was behind this program all this while. Thanks a lot for the wonderful help and suggestions!

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You have yet to fix the VLA. Did you actually listen to feedback?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Registered User
    Join Date
    Apr 2013
    Posts
    10
    Yes I have rewritten the program with proper functions, without VLA, which is not posted here now. Thanks.

    Quote Originally Posted by neeha_khan View Post
    Well, yes needless to say, I have rewritten the program with proper functions and definitely much neat appearance (which is not posted here now).

  4. #19
    Registered User
    Join Date
    Apr 2013
    Posts
    10
    Quote Originally Posted by Khabz View Post
    As for the problem itself I don't really undertand why are there 2 inputs. Shouldn't it be just 1 so it would give the cycle length from that number until 1?
    The Problem statement asks us to compute cycle length of EVERY number between two input numbers. Hence, two inputs.


    Quote Originally Posted by Khabz View Post
    I haven't managed to compile any of your codes posted here (but the one you looked up online) nor do I understand why they should work.
    -Arrays must have a constant size. You may not change it throughout your program.
    >int counter[n+1]
    This is wrong.
    The program compiles perfectly my end and at the judge's end(website) with such arrays. Hence wasn't focusing much on that for this particular program.


    Quote Originally Posted by Khabz View Post
    Also, the usage of magic numbers isn't healthy programming. You should avoid them, even though I see you fixed it on your latest version.
    Yes, fixed that in the latest version. Will use vectors next time . Thanks a lot.

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by neeha_khan View Post
    The program compiles perfectly my end and at the judge's end(website) with such arrays. Hence wasn't focusing much on that for this particular program.
    Because you and the website is using a compiler that incorrectly allows non-standard features to compile.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by neeha_khan View Post
    The program compiles perfectly my end and at the judge's end(website) with such arrays. Hence wasn't focusing much on that for this particular program.
    it would literally take you ten seconds to change

    Code:
    int  counter[n+1];
    to

    Code:
    std::vector<int>  counter(n+1);
    the surrounding program would still function exactly the same, and would be compliant with the standard that everyone accepts. I don't understand why you resist.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cannot find my error
    By Genxi in forum C Programming
    Replies: 5
    Last Post: 09-24-2012, 02:32 AM
  2. Can't seem to find the error here.
    By DavidP in forum C++ Programming
    Replies: 5
    Last Post: 12-30-2008, 01:26 PM
  3. one error....can't find it
    By ammanbesaw in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2007, 10:36 AM
  4. STL find error with STL map
    By VirtualAce in forum C++ Programming
    Replies: 3
    Last Post: 08-20-2007, 01:23 PM
  5. can't find the error
    By noor_mirza in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2002, 12:58 AM

Tags for this Thread