Explain dynamic memory management in detail

Assignment Help Basic Computer Science
Reference no: EM13988341

In this assignment, you will use several advanced object-oriented features in C++ like copy constructor, convert constructor, and dynamic memory management.

Problem description

Built-in integer types in C++ cannot store very large values. For example, the maximal value of the (64 bit) unsigned long long int type is 2^64 - 1 = 18,446,744,073,709,551,615 i.e. having around 20 digits.

In this assignment you will define class BigInteger for large integer values with unlimited number of digits. Partial declaration and implementation code for class BigInteger has been provided in two files BigInteger.h and BigInteger.cpp Link (Links to an external site.). Reading the code, you can see that:

1. Class BigInteger has three fields. 'digits' is a dynamic array of characters to store the digits. Its current size is stored in field 'size' while 'nDigits' is the actual number of non-trivial digits.

For example, to store the number 1234, you can allocate the dynamic array of 4 bytes: 'digits' = {4, 3, 2, 1}. In this case 'size' = 4 and 'nDigits' = 4. It should be noted that 'digits' stores the digits from right to left, i.e. the last digit 4 is stored as position 0 while the first digit 1 is stored as position 3. This simplifies the arthimetic operations (e.g. addition and multiplication). In addition, 'size' and 'nDigits' might not be the same. For example, we can use a dynamic array of 8 bytes to store 1234, i.e. 'digits' = {4, 3, 2, 1, 0, 0, 0, 0}. Thus, 'size' = 8 while 'nDigits' = 4.

2. Class BigInteger has two public methods 'getDigit' and 'setDigit' to read or write a digit at a given position 'pos'. It should be noted that when pos >= nDigits, 'getDigit' will return 0. More important, when pos >= size, i.e. we are trying to write a position exceeding the current size, we will re-allocate 'digits' with a larger size (a power of 2 bigger than pos).

For example, assume 'size' = 4, 'digits' = {4,3,2,1}, and 'nDigits' = 4. If we call getDigit(3), we will get 1 (i.e. the digit at position 3). However, calling getDigit(8) will return 0 because the digit at position 8 is zero. Now, we call setDigit(6, 5) to assign 5 to the digit at position 6. Because 'digits' currently can store only 4 digits, we re-allocate it as a new dynamic array of 8 digits (8 is the smallest power of 2 bigger than 6). After this re-allocation and assignment, we have 'size' = 8, 'digits' = {4,3,2,1,0,0,5,0} and 'nDigits' = 7.

3. Because class BigInteger uses a dynamic array ('digits' is declared as a char* pointer), constructing, destroying, and copying BigInteger objects requires processing of such arrays. Therefore, we have to define the destructor, copy constructor, and copy assignment operator.

Programming tasks

Task 1 . Implement method 'init(int size)' to allocate memory for 'digits' and assign the given value for 'size'. The allocated array should be filled with zeros.

Task 2 . Implement two constructors to initialize a BigInteger object from an int value  or a string storing an integer value . Hint: You can use method setDigit

Task 3 . Overload operators << for writing a BigInteger object to the screen. This operator can use method 'print' in class BigInteger (without debug information).

Task 4 . In main.cpp, complete the code to compute 999! You could use function multiply, which multiplies a BigInteger x to an int value y and shift the result p digits to the right, i.e. performing x * y * 10^p. The default value of p is 0 (i.e. no shifting for typical multiplication).

PART II

This assignment continues the task of implementing BigInteger in Assignment 5. In this assignment, you will overload several operators for BigInteger objects. Your submission for Assignment 5 should be used as the starter code.

Programming tasks

Task 1 . Overload operator + for adding two BigInteger objects. You can do this similar to function multiply, which multiplies a BigInteger x to an int value y and shift the result p digits to the right, i.e. it computes x*y*10p.

Task 2. Overload operator * for multiplying two BigInteger objects. You can use function multiply as a helper function. That is, if y0, y1..., yn is the digits of y, y=&Sum;i=0nyi*10i thus x*y=&Sum;i=0nx*yi*10i.

Task 3 . In main.cpp, complete the code to compute the 1000th Fibonacci number. The Fibonacci sequence is defined as the following rules: Fibo[0] = 1, Fibo[1] = 1, Fibo[n] = Fibo[n-1] + Fibo[n-2].

Task 4 . Overload operators == , != , <, and > (1 point) for comparing two BigInteger objects. Note that != can be defined via == and > is defined via <.

Task 5  . In main.cpp, compute 2^1000, 2^1001, and 2^2001 and verify your comparison operators using the following tests:

2^1000 < 2^1001

2^1000 * 2^1001 == 2^2001

Reference no: EM13988341

Questions Cloud

Present income statement and balance sheet : Present Income Statement and Balance Sheet for at least 2 years. Provide Industry ratios for at least 3 different ratios. Would you consider working for this company? Why or why not?
Contractual relationships impact the project initiation : How do contractual relationships impact the project initiation? What are factors to be consider in a project contract? Why does the project team need to be aware of these contracts before they begin the project?
Why women can not drive in saudi arabia : Write a five pages research on "Why Women Can't Drive in Saudi Arabia" with work cited . What is the solution for the problem?
Examples of the types of hospitals : Discuss what a hospital is and describe the different types of hospitals. Give examples of the types of hospitals in your respective community, and how these hospitals impact community member’s ability to receive care
Explain dynamic memory management in detail : In this assignment you will define class BigInteger for large integer values with unlimited number of digits. Partial declaration and implementation code for class BigInteger has been provided in two files BigInteger.h and BigInteger.cpp Link (Lin..
Understanding about services marketing : According to your understanding about Services Marketing, in the future if you would like to engage the service industry, (1) Which service industry do you like? Why? (2) In this industry, what is the top one company in this industry?
Design the parement thickness required using gi method : A soil subgrade has following data, Percent lines = 60%, passing through 0.074mm sieve, Liquid limit = 46%, Plastic limit = 21%, Design the parement thickness required using G.I. method.
How is the presentation of a child with ptsd different : How is the presentation of a child with PTSD different from that of an adult with the same disorder
Relationship between training program design-capabilities : Explain the relationship between training program design and capabilities. Identify the steps you would take to design a training to address high priority capabilities in your selected organization.

Reviews

Write a Review

Basic Computer Science Questions & Answers

  Determine whether or not the two conditions are equivalent

Determine whether or not the two conditions are equivalent - that is, whether they will be true or false for exactly the same values of the variables appearing in them.

  Contrast the network connectivity

Compare and contrast the network connectivity in the following two diagrams.

  Design an enhanced entity relationship

Develop, document and design an Enhanced Entity Relationship (EER) model for this situation.  You are working for country club with thousands of members. You have been tasked with designing a database to keep track of the members and their guests.

  Complete the requirements using dbdl

Complete the requirements using DBDL and create Diagram. Make sure all tables are normalized to 3NF and exhibit proper relationships.

  How virtual memory relies on locality of reference principle

TLB entries are different than page table entries. Describehow they differ and why they differ. Virtual memory relies on a locality of reference principle. Describe.

  Review the car-caravan analogy

Review the car-caravan analogy

  Prompt the user for the length and width of a lawn

Prompt the user for the length and width of a lawn

  Is snort an hids or an nids

What kind of file is Snort output (logging info) stored in? 7. Can you use Snort with network switches? With hubs? 8. What operating system(s) does Snort run on?

  Convert decimal mumbers into bit binary number

Convert the following decimal mumbers into 8-bit binary numbers a required for 2's complement math, and perform the indicated operations.

  What are some presentation-tools options available

What are some presentation-tools options available in the market place? Pick 3 and research them. What are the advantages and disadvantages

  Comma-delimited text file

This is based on a comma-delimited text file that has already been created containing a 3 digit ID # and a first and last name. The last part of the excercise is as follows.

  Transposition technique to encrypt a message

You have been hired by a local company to consult with its security staff on encryption techniques. The staff has not been formally trained, so you will be demonstrating the principles of encryption through some simple exercises.Use a 2-stage transpo..

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd