how to solve problem"Huge Integer Class"?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • khajeddin
    New Member
    • Nov 2006
    • 51

    how to solve problem"Huge Integer Class"?

    the problem is:
    Create a class HugeInteger which use 40-element array of digits to store integers as larg as 40 digits each.Provide methods input, output, add, subtract
    how to add to arrays ?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by khajeddin
    the problem is:
    Create a class HugeInteger which use 40-element array of digits to store integers as larg as 40 digits each.Provide methods input, output, add, subtract
    how to add to arrays ?
    And what have done so far?

    Comment

    • khajeddin
      New Member
      • Nov 2006
      • 51

      #3
      well i thought i can add each digit(character ) of the string1 and string2 with eachother and put it in the same position of the third string as shown below:
      Code:
      int[] a1=new int[40];
      int[] a2=new int[40];
      int[] a3=new int[40]; 
      .....
      a3[0]=a1[0]+a2[0];
      and ofcourse have some rule too do this correctly in the program...and do the same thing for subtract...
      but my professor didn't accept this algorithm,he said we should get two string of 40-digit number and add or subtract them without convert each digit (each character) into integer and do the same thing as i showd above!
      well i do'nt know what to do so?!!!

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by khajeddin
        the problem is:
        Create a class HugeInteger which use 40-element array of digits to store integers as larg as 40 digits each.Provide methods input, output, add, subtract
        how to add to arrays ?
        What type is the 40-element array?

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by khajeddin
          well i thought i can add each digit(character ) of the string1 and string2 with eachother and put it in the same position of the third string as shown below:
          Code:
          int[] a1=new int[40];
          int[] a2=new int[40];
          int[] a3=new int[40]; 
          .....
          a3[0]=a1[0]+a2[0];
          and ofcourse have some rule too do this correctly in the program...and do the same thing for subtract...
          but my professor didn't accept this algorithm,he said we should get two string of 40-digit number and add or subtract them without convert each digit (each character) into integer and do the same thing as i showd above!
          well i do'nt know what to do so?!!!
          Please don't write in ambiguous terms. Does your assignment say that you
          have to use a fourty element char array or do you have to use Strings?

          If you decide to use the char type for every single digit you do need a bit of
          conversion because the numerical value of a digit char is not equal to the digit
          itself.

          kind regards,

          Jos

          Comment

          • khajeddin
            New Member
            • Nov 2006
            • 51

            #6
            let me describe from the begining as my professor want:
            i have to get two array of 40-digit number(num1 & num2),then convert these two into strings of 40-digit number( array---->string) and then do add and subtract on these two strings.
            ( NOTE: what not to do : i shouldn't convert every digit(character in strings) into integer and i have do some kind of character add or character subtract on them).
            and i have no sugesstion how to do this.please tell some about this!

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by khajeddin
              let me describe from the begining as my professor want:
              i have to get two array of 40-digit number(num1 & num2),then convert these two into strings of 40-digit number( array---->string) and then do add and subtract on these two strings.
              ( NOTE: what not to do : i shouldn't convert every digit(character in strings) into integer and i have do some kind of character add or character subtract on them).
              and i have no sugesstion how to do this.please tell some about this!
              So your professor doesn't want you to convert that String to an int; it can't be
              done anyways because a fourty digit number is way too large for an int. You're
              not allowed to convert it to a BigInteger either (my guess) and you have to add
              those 40 digit numbers yourself. Well here goes:

              If you have to add two characters (representing a single decimal digit) plus a
              carry value (also representing a single decimal digit), you have to do this:
              Code:
              char a; 
              char b;
              char c; // the carry value
              char result= (char)(a+b+c-('0'+'0'));
              if (result > '9') { 
                 // determine new carry and adjust result
              }
              else
                 c= '0'; // no carry
              If your professor doesn't like this, tell him/her to give me a call ;-)

              kind regards,

              Jos

              Comment

              • khajeddin
                New Member
                • Nov 2006
                • 51

                #8
                please let me tell you what i understand from this way:
                we know that to convert every character to an integer we have to subtract 48 from that character and because charachter 0 is equal to -48 you do that to convert a & b into integers!!!!
                thank you very much
                iwill write code of this program right away and send this to know your what you think about that...:)

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by khajeddin
                  please let me tell you what i understand from this way:
                  we know that to convert every character to an integer we have to subtract 48 from that character and because charachter 0 is equal to -48 you do that to convert a & b into integers!!!!
                  thank you very much
                  iwill write code of this program right away and send this to know your what you think about that...:)
                  Yep, you got it. Best of luck with writing your program code.

                  kind regards,

                  Jos

                  Comment

                  • eng200
                    New Member
                    • Oct 2007
                    • 5

                    #10
                    Originally posted by JosAH
                    Yep, you got it. Best of luck with writing your program code.

                    kind regards,

                    Jos

                    hey josah!
                    i didn't understand wat u meant by the "carry" thing,i mean i understand what it means it's just that i dnt know where to put it in my code i dnt even know where to start from,,,,our professor gave us this question as a bonus so would u please write the whole code>? if ur free,no obligations but i would be very grateful if u sent it ;)


                    thanks

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by eng200
                      hey josah!
                      i didn't understand wat u meant by the "carry" thing,i mean i understand what it means it's just that i dnt know where to put it in my code i dnt even know where to start from,,,,our professor gave us this question as a bonus so would u please write the whole code>? if ur free,no obligations but i would be very grateful if u sent it ;)


                      thanks
                      Sorry to disappoint you but that's not the way we handle things overhere. You
                      give it a reasonable try yourself first. If you're stuck come and ask here and we'll
                      try to help you out but it's *you* who has to do the work, not us. It's your homework
                      or assignment, not ours. Go and give it a try.

                      kind regards,

                      Jos

                      Comment

                      • eng200
                        New Member
                        • Oct 2007
                        • 5

                        #12
                        Originally posted by JosAH
                        Sorry to disappoint you but that's not the way we handle things overhere. You
                        give it a reasonable try yourself first. If you're stuck come and ask here and we'll
                        try to help you out but it's *you* who has to do the work, not us. It's your homework
                        or assignment, not ours. Go and give it a try.

                        kind regards,

                        Jos
                        sorry about thatb,,, this is what i came up with but i get errors and i dnt know how to fix it :




                        class HugeInteger {

                        public :

                        HugeInteger();
                        HugeInteger(int someArray[]);

                        int getarray() const;

                        void input(int inputArray[]);
                        void output(int []);
                        void add(int []);
                        void substract(int []);



                        bool isEqualto(int *[]);
                        bool isNotEqualto(in t *[]);
                        bool isGreaterThan(i nt *[]);
                        bool isLessThan(int *[]);
                        bool isGreaterThanOr EqualTo();
                        bool isLessThanOrEqu alTo();

                        bool isZero();

                        private :

                        int a[40];

                        };
                        HugeInteger();
                        HugeInteger(int someArray[]);
                        HugeInteger(int arraySize);
                        virtual ~HugeInteger();


                        HugeInteger::Hu geInteger()
                        {
                        pNumberArray = null;
                        }

                        HugeInteger::Hu geInteger(int arraySize)
                        {
                        pNumberArray = new int[arraySize];
                        }

                        HugeInteger::~H ugeInteger()
                        {
                        if ( pNumberArray != null )
                        {
                        delete [] pNumberArray;
                        pNumberArray = null;
                        }
                        }


                        #include <iostream>
                        using namespace std;

                        #include "H1.cpp"
                        #include "h1.h"



                        void main ()


                        {

                        HugeInteger A();
                        A.input();
                        A.output();
                        A.add();
                        A.substract();
                        A.isEqualto();
                        A.isGreaterThan ();
                        A.isLessThan();
                        A.isGreaterThan OrEqualTo();
                        A.isLessThanOrE qualTo();
                        A.isZero();


                        }


                        when i compile it ,,it says that i have to put class/struct/union type before " .(name of function)" in the main ,,i really dnt know how to fix it i tried to write this much from my lab papers that i have we work on classes but i find them hard to understand,,,,p lease help me understand,,,

                        thnx,,

                        Comment

                        • eng200
                          New Member
                          • Oct 2007
                          • 5

                          #13
                          Originally posted by eng200
                          sorry about thatb,,, this is what i came up with but i get errors ...

                          It's not working because I didn't write the actual functions :$,,, I also wanted to ask if there is another way to solve this problem other than using arrays.
                          Last edited by Killer42; Oct 21 '07, 09:09 PM. Reason: Trimmed excessive quote block

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #14
                            Originally posted by eng200
                            It's not working because I didn't write the actual functions :$,,, I also wanted to ask if there is another way to solve this problem other than using arrays.
                            You do realize that you posted C++ source code in a Java forum do you?

                            kind regards,

                            Jos

                            Comment

                            • eng200
                              New Member
                              • Oct 2007
                              • 5

                              #15
                              Originally posted by JosAH
                              You do realize that you posted C++ source code in a Java forum do you?

                              kind regards,

                              Jos

                              yeah i do realize that but the question was already asked in the java forum so i ddnt want to ask again in the c++ forum and i thought that since u know java then u also know c++ so i posted it here...if u dnt want to help me just tell me it's ok but tell me asap so i can ask someone else coz i really want to know where to fix my code ,,,and i really thought that this site is helpful that's why i joined anyway am sorry again next time i have a question i'll post it in the write forum

                              Comment

                              Working...