Thread: BigNum Quick Question?

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    82

    BigNum Quick Question?

    design a struct data type that will hold a positive numeric value containing up to 100 digits.

    Code:
    struct BigNum
     {
      int digits [100]
     };
    
     bool init(BigNum & value)
      {
       value = 0 // initialize value to 0
      }
    
     bool read (istream &ins, BigNum & value)
      { // function should initalize value to 0.  it should then read   
        // a string of numberic characters from the input stream; a
        // a white space terminates the string.
    
       init(value);
      char temp[256]
       
      ins >> temp;
      
      int t =99;
       for(int i =0; i <strlen(temp); i++)
        {                                              // not sure if im doign this right
        digit[t] = temp[i] - '0';
        t--
       }
    
    }
    I'm not sure if im doing this write..any help will be appreaciated
    Last edited by gqchynaboy; 09-09-2003 at 08:00 PM.

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Well, you missed a few semi-colons. Your loop has the problem that if someone enters a large enough digit, t will become negative, and so you will be assigning to memory that you have no right to access, testing for (t >= 0) would be a better idea, but may chop off user input. As for the struct...

    As this is C++, structs are allowed to have constructors (which is what I would use in place of init). Also, init( ) is trying to assign an integer to a structure (which has no + operator for integral types defined), and so will not work. What would be better is to loop over the array, setting each element to 0.

    An example working off of what you already have:
    Code:
    struct BigNum
    {
      BigNum( ) {
        for(int i = 0; i < 100; ++i)
          digits[i] = 0;
      }
    
      int digits[100];
    };
    I'd actually try to avoid granting direct access to the 'digits' variable. Give it accessors (assign a char* to it, or print it, for example).

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    Instructions : Design a struct data type that will hold a positive numeric value containing up to 100 digits. Call this data type BigNum.
    Teacher won't e-mail me back so im acking.

    Does that mean 4234545352343445 <---contain 100 numbers
    or 2 3 5 342 213 5 3 4323 <--containg 100 diff numbers?

    Also does it matter if im reading the digits from a file or from the input screen?

    here are the function protoytpes
    [CODE]bool init (BigNum & value);

    bool read (istream & ins, BigNum & value);

    void write (ostream & outs, const BigNum & value);

    bool equal (const BigNum & value1, const BigNum & value2);

    void add (const BigNum & value1, const BigNum & value2, BigNum & sum);
    CODE]

    And here is a minimal test of the BigNum that the teacher gave us


    Code:
    #include "bignum.h"
    
    int main ()
    {
            BigNum a, b, c;
            init (a);
            init (b);
            init (c);
            read (cin, a);
            read (cin, b);
            if (equal (a, b))
                    cout << "Values are equal\n";
            else
                    cout << "Values are NOT equal\n";
            write (cout, a);
            cout << endl;
            write (cout, b);
            cout << endl;
            add (a, b, c);
            write (cout, c);
            cout << endl;
            return 0;
    }

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I'd take it to mean a number that can contain 100 digits (as you are doing).

    The init( ) can be essentially the constructor, the read( ) you are close to, but beware of the overflow/underflow error I mentioned ealier.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Digit = an integral value from 0 to 9. For example, 5 is a digit. 55 is 2 digits. To hold 100 digits, you can certainly use an array of 100 ints, but you only really need an array of unsigned chars (or even just chars), as they take 1 byte as opposed to an int's (usually) 4 bytes.

    And, it would appear that it doesn't matter if you're using cin or a file for input, since std::ifstream is derived from std::istream.

    As a side note, I don't know how well that init() function will work, as I don't think you can simply assign an integer value to a structure. I could be wrong though. But what I would do is loop through the array of integers and set each one to 0.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    quick question on the compare and add then? if there was a digit say like 43524. I would add each digit 4+3+5+2+4? And with compare i would compare 4 and 3 then 3 and 5 then 5 and 2 then 2 and 4?

    bool equal (const BigNum & value1, const BigNum & value2);

    bool add (const BigNum & value1, const BigNum & value2, BigNum & sum)

  7. #7
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    No, you would be adding two different bignums. So if BigNum1="123456789" and BigNum2="987654321" then calling the function add(BigNum1, BigNum2, sum) would have sum="1000000000". Equal will say if BigNum1 and BigNum2 are equal.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    for some reason im getting an error

    Code:
    struct BigNum
      {
      unsigned int digit[MAX];
      };
    
    bool read(istream & ins, BigNum & value);
    
    init(value);      // <==error
    
    char temp[256];
    
    ins >> temp;  // <==error
    
    int t=99;
    
    for(int i=0; i < strlen(temp); i++)
       {
       digit[t] = temp[i] - '0';         // <==error
       t--;
       }
    {
    Last edited by gqchynaboy; 09-09-2003 at 09:56 PM.

  9. #9
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Is that inside main() or what? Post your whole code...I can't make sense of that...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    Code:
    i kinda cleaned up but get an error " digit[t] = temp[i] - '0';" saying its undeclared..im compling this in unix to
    
    #include <iostream>
    #include <cctype>
    #include <fstream>
    
    using namespace std;
    
    const int MAX = 100;
    
    struct BigNum
      {
      unsigned int digit[MAX];
      };
    
    
    void init(BigNum & value);
    bool read(istream & ins, BigNum & value);
    
    void init(BigNum & value)
    {
    }
    
    
    bool read(istream & ins, BigNum & value)
    {
    init(value);
    
    char temp[256];
    
    ins >> temp;
    
    int t=99;
    
    for(int i=0; i < strlen(temp); i++)
       {
       digit[t] = temp[i] - '0';
       t--;
       }
     }

  11. #11
    Registered User
    Join Date
    Aug 2002
    Posts
    14
    I haven't used struct's before, but shouldn't it be used like this:

    value->digit[t] = temp[i] - '0';

    Since there is no digit variable, it is part of the struct you created?

  12. #12
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    actually since hes using references it would be
    Code:
    value.digit[t]
    if he were passing a BigNum pointer (BigNum* value) then you would use the -> operator
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    If i enter 5 6 i or any numbers I get 0 0 0, but if i enter 0 0 i get
    4294967258000000000000000000000
    42949672580000000000000000000000


    do you guys see any errors in my read or write funcions?
    Code:
    bool read(istream & ins, BigNum & value)
    {
    init(value);
    
    char temp[256];
    
    ins >> temp[0];
    
    
    int t=99;
    
    if(ins.fail())
      return false;
    
    else if(!isdigit(temp[0]))
      {
      ins.putback(temp[0]);
      return false;
      }
    
    while(temp[0] == '0') ins.get(temp[0]);
    
     if (isspace(temp[0]))
      {
    for(int i=0; i < strlen(temp); i++)
       {
       value.digit[t] = temp[i] - '0';
       t--;
       }
     }
    }
    
    
    bool write (ostream & outs, const BigNum & value)
    {
    int temp =0;
    for (int i = MAX; i >=0; i--)
       {
      if (value.digit[i] !=0) temp =1;
      if (temp) outs << value.digit[i];
      }
    if (!temp) outs << 0;
    return true;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM