473,782 Members | 2,623 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1528
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**********@Ho me.com
Jul 22 '05 #2

"sudeep" <su************ @yahoo.co.in> wrote in message
news:fa******** *************** ***@posting.goo gle.com...
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:
Implementati on 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:
Implementati on 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:
Implementati on 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******@coldm ail.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
17776
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. Yahoo store was originally written in Lisp. c. Emacs The issues with these will probably come up, so I might as well mention them myself (which will also make this a more balanced
22
2334
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 :-). Few more notes on the language. I don't know if I can really suggest improvements to the language... but I hope to learn something :-) I think some things are better in Delphi/Pascal (like the := for assignments instead of = and = for...
21
3922
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 table1"; var obj=new ActiveXObject("ADODB.Recordset");
6
1706
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 = {2,5,2} The algorithm should find that the "2" occurs more than once in S1, S2, and
33
5646
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 p.pattern.groups > 100: raise AssertionError( "sorry, but this version only supports 100 named groups"
15
1660
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 on all key forms. The first thing to do is to successfully split the database. I then rewrite to support multiple users. To allow the save/discard feature I create copies of the key tables and append the word Final to each one. For example I...
2
2596
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 good while and I'm getting really frustrated now! My problem is this - my custom controls periodically disappear from my
15
2436
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 that are formatted for number and then half way down they are changed to text. OR the famous ok now everything in red is ---- and everything in blue is---------. WTF are these people thinking?
3
2959
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 of the script gets executed. Can someone please give me a direction as to what I may be missing? Thanks.
7
7818
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}" As far as I know yet -- hence this question -- there is no 'one solution fits all', but instead there are several parts that have to be put together to check. What I have so far is, and would like as much feedback as possible to ensure I've...
0
9639
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10143
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8964
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7486
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5375
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5507
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4040
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2870
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.