# Large Integers

This project received **32** bids from talented freelancers with an average bid price of **$ USD**.

###### Skills Required

###### Project Budget

N/A###### Total Bids

32###### Project Description

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 array element. In order to perform addition and subtraction more easily, it is better to store the digits in the array 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. If they are equal, 0 should be returned.

Input/Output Specification

Your program should allow the user to do two things:

1) Add two big integers

2) Subtract to big integers

Instead of getting input from the user, you read in input from a file, "bigint.txt". (This will expedite the grading process.)

The input file format is as follows:

The first line will contain a single positive integer, n, representing the number of operations to carry out. The next n lines will contain one problem each. Each line will have three integers separated by white space. The first integer on each of these lines is guaranteed to either be 1 or 2, indicating addition and subtraction, respectively. The next two integers on the line will be the two operands for the problem. You may assume that these two integers are non-negative, are written with no leading zeros (unless the number itself is 0) and that the number of digits in either of the numbers will never exceed 200.

You should generate your output to the screen. In particular, you should generate one line of output per each input case. 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 operand first. For the second option, always print the larger of the two operands first. If the two operands are equal, the same number is printed both times.

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 return a

// pointer to the appropriate struct integer.

struct integer* read_integer(char* stringInt);

//Preconditions: p is a pointer to a big integer.

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

// printed out.

void print(struct integer *p);

//Preconditions: p and q are pointers to struct integers.

//Postconditions: A new struct 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 struct integers.

//Postconditions: A new struct 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 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 Input File

3

1 8888888888 2222222222

2 9999999999 10000000000

2 10000000000 9999999999

Corresponding Output

8888888888 + 2222222222 = 11111111110

10000000000 – 9999999999 = 1

10000000000 – 9999999999 = 1

Deliverables

Turn in a single file, bigint.c, over WebCourses 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

### Looking to make some money?

- Set your budget and the timeframe
- Outline your proposal
- Get paid for your work

## Hire Freelancers who also bid on this project

## Looking for work?

### Work on projects like this and make money from home!

Sign Up Now- Forbes
- The New York Times
- Time
- Wall Street Journal
- Times Online