Thread: Usage of the faulty number type

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    2

    Usage of the faulty number type

    I have the following problem that is quite complex and don't have a clue how to solve it:

    ---

    Let's assume that we have got a class that defines the new type called FaultyUnsignedLongLong, which is similar to standard unsigned long long, but in which some arithmetic operations on numbers in this type are returning false values or are causing errors.

    The problem is to create a function FaultyUnsignedLongLong cprod(const int &fromInt, const int &toInt) that is calculating the product (fromInt)*(fromInt+1)*...*(toInt-1)*(toInt), where fromInt and toInt are integers such that 0 <= fromInt <= toInt <= 600000.

    The type FaultyUnsignedLongLong is implementing some arithmetic and logical operators. Unfortunately there is always a risk that these operators will returne false results. Arithmetic operations can cause the following exceptions:

    - class ErrorCalculation {}; - operation caused an error and will not return the result,
    - class ErrorPrevCalculation {}; - previous arithmetic operation was calculated in the incorrect way, so the result that was returned by this operations has been incorrect,
    - class ErrorPrevCalculaionsIncreasedByOne : public ErrorPrevCalculation {}; - means that the previous arithmetic operation returned a result that was larger by one than it's true solution,
    - class SystemError {}; - means that operation has cause an overall reset (values of all variables that were defined as FaultyUnsignedLongLong are change to 0). Such reset could be also cause on purpose - you have to use a static method reset().

    All of the exceptions can occur in a random manner.

    The file in which we are implementing the function FaultyUnsignedLongLong cprod(const int &fromInt, const int &toInt) must have the following properties:

    -it should include only the file FaultyUnsignedLongLong.h,
    -using #define inside the file is forbidden,
    -inside the file one can't use or declare any variables of the type other then FaultyUnsignedLongLong (particullary one can't useconst_cast, type_of, class, struct or any other keywords like bool, char, long, float etc.; the int keyword can be only used twice in the definition of the function FaultyUnsignedLongLong cprod(const int &fromInt, const int &toInt),
    -there should be only a definition of one function: FaultyUnsignedLongLong cprod(const int &fromInt, const int &toInt) that is calculating the product (fromInt)*(fromInt+1)*...*(toInt-1)*(toInt),
    -each of the variables fromInt, toInt could be used only 4 times in the file.

    A declaration of the class FaultyUnsignedLongLong looks like that:
    Code:
    class FaultyUnsignedLongLong {
     private:
      unsigned long long int value;
     public:
      FaultyUnsignedLongLong(const unsigned long long int &i = 0);
      FaultyUnsignedLongLong(const FaultyUnsignedLongLong &i);
      ~FaultyUnsignedLongLong();
    
      static void reset();
    
      friend FaultyUnsignedLongLong operator+(const FaultyUnsignedLongLong &i, const FaultyUnsignedLongLong &j) 
    		throw(ErrorCalculation, ErrorPrevCalculation, SystemError);
      friend FaultyUnsignedLongLong operator-(const FaultyUnsignedLongLong &i, const FaultyUnsignedLongLong &j) 
    		throw(ErrorCalculation, ErrorPrevCalculation, SystemError);
      friend FaultyUnsignedLongLong operator*(const FaultyUnsignedLongLong &i, const FaultyUnsignedLongLong &j) 
    		throw(ErrorCalculation, ErrorPrevCalculation, SystemError);
      friend FaultyUnsignedLongLong operator/(const FaultyUnsignedLongLong &i, const FaultyUnsignedLongLong &j) 
    		throw(ErrorCalculation, ErrorPrevCalculation, SystemError);
      friend FaultyUnsignedLongLong operator%(const FaultyUnsignedLongLong &i, const FaultyUnsignedLongLong &j) 
    		throw(ErrorCalculation, ErrorPrevCalculation, SystemError);
    
      friend bool operator==(const FaultyUnsignedLongLong &i, const FaultyUnsignedLongLong &j);
      friend bool operator!=(const FaultyUnsignedLongLong &i, const FaultyUnsignedLongLong &j);
      friend bool operator<=(const FaultyUnsignedLongLong &i, const FaultyUnsignedLongLong &j);
      friend bool operator>=(const FaultyUnsignedLongLong &i, const FaultyUnsignedLongLong &j);
    };
    and it's source code has the following includes:

    Code:
    #include <map>
    #include <set>
    #include <iostream>
    #include <cstdlib>
    #include <climits>
    #include <iosfwd>
    ---

    I will be very glad if any of you could give me some hints or possible solutions of this problem.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It's not that hard.

    As to the various properties ..... they're coding constraints. All you need to do is identify some method (eg an algorithm) that will compute the required result, and take recovery action whenever an exception is thrown. Once you have such an algorithm described, implementing it within the coding constraints should be fairly simple.

    And, no, I'm not going to show sample code ..... I assume the whole point of the exercise is for you to learn by thinking through and doing it yourself.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    2
    I have an algorithm that is using lists of elements of the type FaultyUnsignedLongLong and it can skip the problems of all of the exceptions except for the SystemError thing.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    When a SystemError occurs, restart from the beginning.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM