# Project 1 - bignum

## CSCI 2021

### Due September 30th at 11:59 pm

100 points total for bignum_math.c

10 points is for clean and runnable code with reasonable effort (students that submit the original file with few changes will receive little credit on this portion). Clean code includes both style and appropriate documentation.

5 points for the valid_input() function

5 points for the bignum_print() function

5 points for the perform_operation() function

15 points is for the implementation of equals_to() function

15 points is for the implementation of less_than() function

15 points is for the implementation of greater_than() function

30 points is for the implementation of multiply() function

### Setup

Download bignum_math.c from Canvas.

You should now have a file named bignum_math.c with all of the function outlines.

### Your task

For this project, you will write a program that can perform comparison (>,

### <, =) and multiplication of large positive integers of any base between 2 and 36 (inclusive). You will have to use arrays instead of primitive data types in order to be able to hold these large numbers.

Because arrays in C are not complex objects, there is no good way to set or compute an array’s length. Thus, we’ll need some way to denote that the number is over. Since we actually have ints, a negative number such as -1 (negative one) will indicate that the number is finished. Thus, if we input the number 1234567ABC, the integer array passed to perform_operation will be represented using the array { 1, 2, 3, 4, 5, 6, 7,

10, 11, 12, -1 }.

If you look at the add method, you will see that this format of array is used there, and you will use it in your methods. The multiply method must also return an array of this form.

You must complete the following functions in the bignum_math.c file. They are already declared in the file, but you must fill in the definition with the correct code.

bool valid_input(char* input, int base);

Returns true if: the given string (char array) only contains digits 0-9, letters A-Z, or letters a-z; the input does not violate the given base (F is a valid digit in base 16, but not in base 10).

void bignum_print(int* num);

Print out the bignum using digits and lower-case characters

The function works as intended right now, except that it prints integers instead of characters.

Right now, bignum_print({9, 10, 16, 5, -1}) will print 910165.

You need to modify it so that it instead prints 9ag5.

bool less_than(int* input1, int* input2)

Determine whether input1 is less than input2. If input1 is the array {5, 8, -1} (representing 58) and input2 is the array {6, 3,

-1} (representing 63) this function should return true.

bool greater_than(int* input1, int* input2)

Determine whether input1 is greater than input2. If input1 and input2 are the same as those given above, this function should return false.

bool equal_to(int* input1, int* input2)

Determine whether input1 is equal to input2. If input1 and input2 are the same as those given above, this function should return false.

int* multiply(int* input1, int* input2, int base)

Compute the product of input1 and input2. If input1 and input2 are the same as given above (assuming base 10), this function should return the value 3654, encoded as {3, 6, 5, 4, -1}.

void perform_operation(int* input1, int* input2, char op, int base)

This is the method that performs computations and comparisons between bignums.

The method currently supports addition, you will need to add support for multiplication as well as all of the comparison operators detailed above.

This method is responsible for printing the results of the computation or comparison.

The other functions are already complete and are there as a reference or for your own use. Be sure to read all included comments carefully in order to understand each function and its purpose.

Here is a sample compilation and run-through: gcc -Wall -o bignum_math bignum_math.c

./bignum_math 16 f4d905 + F

Result: f4d914 (F4D914 will not be considered as correct)

./bignum_math 10 1 + 1

Result: 2

./bignum_math 2 1 + 101

Result: 110

./bignum_math 10 8 "*" 2

Result: 16

./bignum_math 2 10 "*" 101

Result: 1010

./bignum_math 10 1 ">" 2 Result: false

./bignum_math 10 1 "<" 2 Result: true

./bignum_math 10 1 = 2 Result: false

In an attempt to make your life easier, we have provided a function that converts the input array of characters to an array of integers (notice that this is called in main, after the input array of characters has been determined to be well formed by valid_input() ). Therefore perform_operation() will be given two arrays of int. Each index of the array will contain a number (integer) for each character in a given input.

We have also provided an add() function which will correctly add two numbers. You can refer to this and use it for your multiply() function.

### bignum_math.c

This file is where you will write all of your code. Before you start from the main function, understand what it does: It takes arguments from the command line, converts them from char arrays to int arrays and passes

them to perform_operation() which is responsible for performing the requested computation or comparison and printing the result to the terminal.

Please read the advice in the code comments: Add any other functions that you may need, keep code clean, concise and explain where necessary. This may not be a software design course, but that doesn’t mean we won’t take points off for messy code.

### Tips

It is perfectly legal to use any code that we present in discussion or lecture. We may supply you with some helpful code hints. However, code taken from any source other than discussion, lecture, or the course staff will be considered an instance of academic misconduct.

It is very easy to write code that will accidentally run off the end of an array in C. Unlike Java, there will be no compilation error, no warning, not even necessarily a run-time error. The most likely thing you will encounter is strange behavior that doesn’t make any sense. Double-check your array accesses to avoid this problem!

Code will be graded on CSE Labs Machines. While you may use your own machine for this assignment if you wish, make sure that your code compiles and runs properly on these machines, as otherwise you will lose credit.

### IMPORTANT: Submission

At the top of your bignum_math.c file, include a comment with your name and your x500 (Internet ID). A failure to include this comment will be considered a failure to follow instructions and will result in loss of points. When you are finished, submit your bignum_math.c file to Canvas at the link for Project 1.

In accordance with course policy, projects may be submitted to Canvas up to 24 hours after the due date for a 10% penalty to the grade. Submission will not be accepted after that time.