-
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
-
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
-
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;
}
-
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.
-
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.
-
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)
-
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.
-
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--;
}
{
-
Is that inside main() or what? Post your whole code...I can't make sense of that...
-
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--;
}
}
-
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?
-
actually since hes using references it would be
if he were passing a BigNum pointer (BigNum* value) then you would use the -> operator
-
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;
}