473,385 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

one more

Implementation of BigInt
Problem Statement

The int basic data type provided by the C/C++ language to represent
integers has the following limitations, viz., the size of the data
type is machine dependent and also is limited in the smallest and
largest integer that it can represent.

In this assignment you have to implement a class BigInt, which allows
a programmer to declare and use integers of arbitrary length. You have
to support basic arithmetic operations and relational operations
objects of this data type. You should also be able to read and write
BigInts from and to stdin and stdout respectively.

A programmer should be able to use BigInt in a C++ program just like
the type int as given in the example below:

Program
#include <iostream.h>
#include "BigInt.h"
#define SIZE 5

BigInt big_array[SIZE], minint, maxint;

int main() {

BigInt sum(0); // Constructor with integer parameter
BigInt product("1"); // Constructor with string
int i ;

for (i=0; i<SIZE; i++) {
cin >> big_array[i]; // read BIGINT from stdin
if (0==i) {
maxint = big_array[i]); // assignment
minint = big_array[i]);
}
else {
if (maxint < big_array[i]) // relational operation
maxint = big_array[i]);
else if (minint > big_array[i])
minint = big_array[i]);
}
}
sum = maxint + minint;
product = big_array[1] * big_array[4];

cout << sum << endl ;
cout << maxint - minint << endl ;
cout << product << endl ;
cout << big_array[3] / big_array[0] << endl ;
cout << big_array[3] % big_array[0] << endl ;
}



Sample Input:
102378650387630 14 -19876543920 202378650387630 -2134
Sample output:
202358773843710
-202398526931550
-29876
1
100000000000000

Features to be provided by your BigInt implementation

1. Your implementation of BigInt should provide the following
functionality:
a. Constructors to be provided
BigInt( )
BigInt (int)
BigInt (char *)
BigInt (BigInt &) - copy constructor

b. Overload operators
<<
=
+
-
*
/
%
< <==

==
2. BigInts will have digits <= 100.

Deliverables:

Code: You must submit the following files: 1. BigInt.h (containing the
function declarations) 2. BigInt.c (containing the function
definitions - it should not contain any main)

Assessment: Your code will be tested with test programs similar to the
above program
Jul 22 '05 #1
6 1513
On 17 Oct 2004 03:22:03 -0700, su************@yahoo.co.in (sudeep)
wrote:

[another homework assignment snipped...]

If you don't want people to assume that you are a clueless troll, you
should at least write something of your own regarding why you are
having problems with this.

No one here is going to hand you a solution on a silver platter.

--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #2

"sudeep" <su************@yahoo.co.in> wrote in message
news:fa**************************@posting.google.c om...
Implementation of BigInt
Problem Statement


So what's your question(s)?

-Mike
Jul 22 '05 #3
On 17 Oct 2004 03:22:03 -0700, su************@yahoo.co.in (sudeep)
wrote:
Implementation of BigInt
Problem Statement
[snip]

Sudeep,

You don't say what your problem is, but I think that I can see some of
it. The code your instructor supplied does not look very good at all.
#include <iostream.h> Warning: iostream.h is deprecated in the standard and should not be
used for new code. Use iostream (no ".h") instead.
#include "BigInt.h" Style: Put this above the iostream include to be sure that there is no
hidden dependency on iostream in BigInt.h
#define SIZE 5 Warning: As the FAQ says, "Macros are evil". Why use a global macro,
which completely ignores type and scope, when a constant would be much
safer?

BigInt big_array[SIZE], minint, maxint; Warning: Global variables are dangerous. Why use them when they are
not absolutely required?
Style: Better to have one declaration per line.

int main() {

BigInt sum(0); // Constructor with integer parameter
BigInt product("1"); // Constructor with string
int i ; Style: i is only used as a loop variable, better to declare it inside
the for statement.

for (i=0; i<SIZE; i++) { Style: I would use more spaces to make this line more readable.
Style: ++i is never slower and may be faster than i++; same for --i
and i--. Where either would do it is better to get into the habit of
using ++i.
cin >> big_array[i]; // read BIGINT from stdin Style: The comment needs updating to reflect the actual code.
if (0==i) {
maxint = big_array[i]); // assignment Error: Unmatched closing bracket near the end. This code will not
compile as written.
minint = big_array[i]); Error: Another unmatched closing bracket.
}
else {
if (maxint < big_array[i]) // relational operation Style: Using an else if construction would reduce the amount of
indentation needed here.
maxint = big_array[i]); Error: A third unmatched closing bracket.
else if (minint > big_array[i])
minint = big_array[i]); Error: A fourth unmatched bracket.
}
}
sum = maxint + minint;
product = big_array[1] * big_array[4];

cout << sum << endl ; Style: There is no on-screen indication of what number this is, better
to tell the user what it is.
cout << maxint - minint << endl ;
cout << product << endl ;
cout << big_array[3] / big_array[0] << endl ;
cout << big_array[3] % big_array[0] << endl ;
Style: I know that "return" is not required at the end of main(), but
I put it in anyway. This helps by showing that I have finished coding
and hopefully not left anything off the end.
}


Looking at the list of deliverables there is a possible clue to some
of the bad style in the example: "2. BigInt.c". Note that is
"BigInt.c", not BigInt.cpp. This has possibly been converted from an
original in C rather than written from scratch in C++.

A better version of the example code would be:

#include "BigInt.h"
#include <iostream>

int main() {
using std::cout;
using std::endl;

const int size = 5;

BigInt big_array[size];
BigInt minint;
BigInt maxint;

BigInt sum(0); // Constructor with integer parameter
BigInt product("1"); // Constructor with string

for (int i = 0; i < size; ++i) {
std::cin >> big_array[i]; // read BigInt from cin
if (0 == i) {
maxint = big_array[i]; // assignment
minint = big_array[i];
}
else if (maxint < big_array[i]) { // relational operation
maxint = big_array[i];
}
else if (minint > big_array[i]) {
minint = big_array[i];
}
}

sum = maxint + minint;
product = big_array[1] * big_array[4];

cout << "Sum = " << sum << endl ;
cout << "Difference = " << maxint - minint << endl ;
cout << "Product = " << product << endl ;
cout << "Division = " << big_array[3] / big_array[0] << endl ;
cout << "Modulus = " << big_array[3] % big_array[0] << endl ;

return 0;
}

If you get any problems with the rest of the assignment then post what
you have written together with an explanation of your problems. The
more of your own work you show, the more likely you are to get help.
rossum
--

The ultimate truth is that there is no Ultimate Truth
Jul 22 '05 #4
On 17 Oct 2004 03:22:03 -0700, su************@yahoo.co.in (sudeep)
wrote:
Implementation of BigInt
Problem Statement
[snip]

Sudeep,

You don't say what your problem is, but I think that I can see some of
it. The code your instructor supplied does not look very good at all.
#include <iostream.h> Warning: iostream.h is deprecated in the standard and should not be
used for new code. Use iostream (no ".h") instead.
#include "BigInt.h" Style: Put this above the iostream include to be sure that there is no
hidden dependency on iostream in BigInt.h
#define SIZE 5 Warning: As the FAQ says, "Macros are evil". Why use a global macro,
which completely ignores type and scope, when a constant would be much
safer?

BigInt big_array[SIZE], minint, maxint; Warning: Global variables are dangerous. Why use them when they are
not absolutely required?
Style: Better to have one declaration per line.

int main() {

BigInt sum(0); // Constructor with integer parameter
BigInt product("1"); // Constructor with string
int i ; Style: i is only used as a loop variable, better to declare it inside
the for statement.

for (i=0; i<SIZE; i++) { Style: I would use more spaces to make this line more readable.
Style: ++i is never slower and may be faster than i++; same for --i
and i--. Where either would do it is better to get into the habit of
using ++i.
cin >> big_array[i]; // read BIGINT from stdin Style: The comment needs updating to reflect the actual code.
if (0==i) {
maxint = big_array[i]); // assignment Error: Unmatched closing bracket near the end. This code will not
compile as written.
minint = big_array[i]); Error: Another unmatched closing bracket.
}
else {
if (maxint < big_array[i]) // relational operation Style: Using an else if construction would reduce the amount of
indentation needed here.
maxint = big_array[i]); Error: A third unmatched closing bracket.
else if (minint > big_array[i])
minint = big_array[i]); Error: A fourth unmatched bracket.
}
}
sum = maxint + minint;
product = big_array[1] * big_array[4];

cout << sum << endl ; Style: There is no on-screen indication of what number this is, better
to tell the user what it is.
cout << maxint - minint << endl ;
cout << product << endl ;
cout << big_array[3] / big_array[0] << endl ;
cout << big_array[3] % big_array[0] << endl ;
Style: I know that "return" is not required at the end of main(), but
I put it in anyway. This helps by showing that I have finished coding
and hopefully not left anything off the end.
}


Looking at the list of deliverables there is a possible clue to some
of the bad style in the example: "2. BigInt.c". Note that is
"BigInt.c", not BigInt.cpp. This has possibly been converted from an
original in C rather than written from scratch in C++.

A better version of the example code would be:

#include "BigInt.h"
#include <iostream>

int main() {
using std::cout;
using std::endl;

const int size = 5;

BigInt big_array[size];
BigInt minint;
BigInt maxint;

BigInt sum(0); // Constructor with integer parameter
BigInt product("1"); // Constructor with string

for (int i = 0; i < size; ++i) {
std::cin >> big_array[i]; // read BigInt from cin
if (0 == i) {
maxint = big_array[i]; // assignment
minint = big_array[i];
}
else if (maxint < big_array[i]) { // relational operation
maxint = big_array[i];
}
else if (minint > big_array[i]) {
minint = big_array[i];
}
}

sum = maxint + minint;
product = big_array[1] * big_array[4];

cout << "Sum = " << sum << endl ;
cout << "Difference = " << maxint - minint << endl ;
cout << "Product = " << product << endl ;
cout << "Division = " << big_array[3] / big_array[0] << endl ;
cout << "Modulus = " << big_array[3] % big_array[0] << endl ;

return 0;
}

If you get any problems with the rest of the assignment then post what
you have written together with an explanation of your problems. The
more of your own work you show, the more likely you are to get help.
rossum
--

The ultimate truth is that there is no Ultimate Truth
Jul 22 '05 #5
On 17 Oct 2004 03:22:03 -0700, su************@yahoo.co.in (sudeep)
wrote:
Implementation of BigInt
Problem Statement
[snip]

Sudeep,

You don't say what your problem is, but I think that I can see some of
it. The code your instructor supplied does not look very good at all.
#include <iostream.h> Warning: iostream.h is deprecated in the standard and should not be
used for new code. Use iostream (no ".h") instead.
#include "BigInt.h" Style: Put this above the iostream include to be sure that there is no
hidden dependency on iostream in BigInt.h
#define SIZE 5 Warning: As the FAQ says, "Macros are evil". Why use a global macro,
which completely ignores type and scope, when a constant would be much
safer?

BigInt big_array[SIZE], minint, maxint; Warning: Global variables are dangerous. Why use them when they are
not absolutely required?
Style: Better to have one declaration per line.

int main() {

BigInt sum(0); // Constructor with integer parameter
BigInt product("1"); // Constructor with string
int i ; Style: i is only used as a loop variable, better to declare it inside
the for statement.

for (i=0; i<SIZE; i++) { Style: I would use more spaces to make this line more readable.
Style: ++i is never slower and may be faster than i++; same for --i
and i--. Where either would do it is better to get into the habit of
using ++i.
cin >> big_array[i]; // read BIGINT from stdin Style: The comment needs updating to reflect the actual code.
if (0==i) {
maxint = big_array[i]); // assignment Error: Unmatched closing bracket near the end. This code will not
compile as written.
minint = big_array[i]); Error: Another unmatched closing bracket.
}
else {
if (maxint < big_array[i]) // relational operation Style: Using an else if construction would reduce the amount of
indentation needed here.
maxint = big_array[i]); Error: A third unmatched closing bracket.
else if (minint > big_array[i])
minint = big_array[i]); Error: A fourth unmatched bracket.
}
}
sum = maxint + minint;
product = big_array[1] * big_array[4];

cout << sum << endl ; Style: There is no on-screen indication of what number this is, better
to tell the user what it is.
cout << maxint - minint << endl ;
cout << product << endl ;
cout << big_array[3] / big_array[0] << endl ;
cout << big_array[3] % big_array[0] << endl ;
Style: I know that "return" is not required at the end of main(), but
I put it in anyway. This helps by showing that I have finished coding
and hopefully not left anything off the end.
}


Looking at the list of deliverables there is a possible clue to some
of the bad style in the example: "2. BigInt.c". Note that is
"BigInt.c", not BigInt.cpp. This has possibly been converted from an
original in C rather than written from scratch in C++.

A better version of the example code would be:

#include "BigInt.h"
#include <iostream>

int main() {
using std::cout;
using std::endl;

const int size = 5;

BigInt big_array[size];
BigInt minint;
BigInt maxint;

BigInt sum(0); // Constructor with integer parameter
BigInt product("1"); // Constructor with string

for (int i = 0; i < size; ++i) {
std::cin >> big_array[i]; // read BigInt from cin
if (0 == i) {
maxint = big_array[i]; // assignment
minint = big_array[i];
}
else if (maxint < big_array[i]) { // relational operation
maxint = big_array[i];
}
else if (minint > big_array[i]) {
minint = big_array[i];
}
}

sum = maxint + minint;
product = big_array[1] * big_array[4];

cout << "Sum = " << sum << endl ;
cout << "Difference = " << maxint - minint << endl ;
cout << "Product = " << product << endl ;
cout << "Division = " << big_array[3] / big_array[0] << endl ;
cout << "Modulus = " << big_array[3] % big_array[0] << endl ;

return 0;
}

If you get any problems with the rest of the assignment then post what
you have written together with an explanation of your problems. The
more of your own work you show, the more likely you are to get help.
rossum
--

The ultimate truth is that there is no Ultimate Truth
Jul 22 '05 #6
On Mon, 18 Oct 2004 22:42:45 +0100, rossum <ro******@coldmail.com>
wrote:
[snip]

Apologies for the multiple post, my newsreader was telling me that my
reply wasn't being sent, but it was telling fibs.

rossum

--

The ultimate truth is that there is no Ultimate Truth
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
22
by: bearophile | last post by:
Ville Vainio: >It's highly typical for the newbies to suggest improvements to the >language. They will usually learn that they are wrong, but the >discussion that ensues can be fruitfull anyway...
21
by: Rabbit63 | last post by:
Hi: I want to show a set of records in the database table on the clicnt browser. I have two ways to do this (writen in JScript): 1.The first way is: <% var sql = "select firstname from...
6
by: Markus Dehmann | last post by:
I have n sets of elements. I want to find elements that occur more than once in more than one set. Maybe the following example shows what I mean: S1 = {1,2,3,2,4} S2 = {2,2,4,5,4} S2 =...
33
by: Joerg Schuster | last post by:
Hello, Python regular expressions must not have more than 100 capturing groups. The source code responsible for this reads as follows: # XXX: <fl> get rid of this limitation! if...
15
by: Deano | last post by:
I've posted about this subject before but haven't really got anywhere yet. I have now come up with a plan of action that takes into account my strong desire to implement save/discard functionality...
2
by: Suzanne | last post by:
Hi all, I'm reposting this message as I'm experiencing this problem more and more frequently : I really hope someone out there can help me as I've been tearing my hair out on this one for a...
15
by: sparks | last post by:
We get more and more data done in excel and then they want it imported into access. The data is just stupid....values of 1 to 5 we get a lot of 0's ok that alright but 1-jan ? we get colums...
3
by: Water Cooler v2 | last post by:
Questions: 1. Can there be more than a single script block in a given HEAD tag? 2. Can there be more than a single script block in a given BODY tag? To test, I tried the following code. None...
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.