# Big Integers

Bajet $30-250 USD

The Problem

The unsigned int type in C requires 4 bytes of memory storage. With 4 bytes we can store integers as large as 232-1; but what if we need bigger integers, for example ones having hundreds of digits? If we want to do arithmetic with such very large numbers we cannot simply use the unsigned data type. One way of dealing with this is to use a different storage structure for integers, such as an array of digits. If we represent an integer as an array of digits, where each digit is stored in a different array index. Since the integers are allowed to be as large as we like, a dynamically-sized array will prevent the possibility of overflows in representation. However we need new functions to add, subtract, compare, read and write these very large integers.

Write a program that will manipulate such arbitrarily large integers. Each integer should be represented as an array of digits, where the least significant digit is stored in index 0. Your program should we able to read in a string of digits and create a struct that stores the big integer.

Your program should store each decimal digit (0-9) in a separate node of the linked list. In order to perform addition and subtraction more easily, it is better to store the digits in the list in the reverse order. For instance, the value 1234567890 would be stored as:

index 0 1 2 3 4 5 6 7 8 9

array 0 9 8 7 6 5 4 3 2 1

Note: Although this seems counter-intuitive, it makes the code slightly easier, because in all standard mathematical operations, we start with the least significant digits. It also makes sense that the digit at the place 10i is stored in index i.

Your program should include the following functions:

• a function that will read in an integer from the keyboard

• a function that will print an integer.

• a function that will add two integers and return the result.

• a function that compares two integers and returns -1 if the first is less than the second, 0 if they are equal, and 1 if the first is greater than the second.

• a function that will subtract one integer from the other and return the result. Since you’ll be dealing with positive integers only, the result should be a positive number. To ensure that the result is integer you should subtract the smaller number from the larger one.

Input/Output Specification

Your program should prompt the user with the following menu:

1) Add two big integers

2) Subtract to big integers

3) Quit

Continue prompting the user until they quit. If they choose an invalid option, ask them to enter a valid one and continue doing so until they do.

If either of the first two options is chosen, prompt the user with the following two questions?

What is the first number for your operation?

What is the second number for your operation?

Your output should fit one of the two following formats:

X + Y = Z

X – Y = Z

corresponding to which option was chosen. For the first option, always print the first number entered first. For the second option, always print the larger of the two numbers entered first.

You may assume that the user will always enter a big integer that adheres to the following rules:

1) Only contains digits

2) The leading digit is NOT zero.

3) The maximum number of digits in any individual integer entered will be 200.

Implementation Restrictions

You must use the following struct:

struct integer {

int* digits;

int size;

}

Whenever you store or return a big integer, always make sure not to return it with any leading zeros. Namely, make sure that the value stored in index size-1 is NOT zero. The only exception to this rule is if 0 is being stored. 0 should be stored in an array of size 1.

Here are the prototypes of the functions for you to write:

//Preconditions: the first parameter is string that stores

// only contains digits, doesn't start with

// 0, and is 200 or fewer characters long.

//Postconditions: The function will read the digits of the

// large integer character by character,

// convert them into integers and place them in

// nodes of a linked list. The pointer to the // head of the list is returned.

struct integer* read_integer(char* stringInt);

//Preconditions: p is a pointer to a big integer, stored in

// reverse order, least to most significant

// digit, with no leading zeros.

//Postconditions: The big integer pointed to by p is

// printed out.

void print(struct integer *p);

//Preconditions: p and q are pointers to big integers,

// stored in reverse order, least to most

// significant digit, with no leading zeros.

//Postconditions: A new big integer is created that stores

// the sum of the integers pointed to by p

// and q and a pointer to it is returned.

struct integer* add(struct integer *p, struct integer *q);

//Preconditions: p and q are pointers to big integers,

// stored in reverse order, least to most

// significant digit, with no leading zeros.

//Postconditions: A new big integer is created that stores

// the absolute value of the difference

// between the two and a pointer to this is

// returned.

struct integer* subtract(struct integer *p, struct integer *q);

//Preconditions: Both parameters of the function are

// pointers to struct integer.

//Postconditions: The function compares the digits of two

// numbers recursively and returns:

// -1 if the first number is smaller than the second,

// 0 if the first number is equal to the second number,

// 1 if the first number is greater than the second.

int compare(struct integer *p, struct integer *q);

Sample Run

Which of the following choices do you want?

1) Add two big integers

2) Subtract to big integers

3) Quit

1

What is the first number for your operation?

8888888888

What is the second number for your operation?

2222222222

8888888888 + 2222222222 = 11111111110

Which of the following choices do you want?

1) Add two big integers

2) Subtract to big integers

3) Quit

2

What is the first number for your operation?

9999999999

What is the second number for your operation?

10000000000

10000000000 – 9999999999 = 1

Which of the following choices do you want?

1) Add two big integers

2) Subtract to big integers

3) Quit

2

What is the first number for your operation?

10000000000

What is the second number for your operation?

9999999999

10000000000 – 9999999999 = 1

Which of the following choices do you want?

1) Add two big integers

2) Subtract to big integers

3) Quit

3

Thank you for using the big integer calculator!

Deliverables

Turn in a single file, bigint.c, that solves the specified problem. If you decide to make any enhancements to this program, clearly specify them in your header comment so the grader can test your program accordingly.

## Dianugerahkan kepada:

Dear sir, I am very interested in your project, Please see PMB for more details. Thanks. Best Regards.