This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
user:rmatsch:portfolio:cprogproject2 [2012/03/19 03:12] – [Project: BIG NUM] rmatsch | user:rmatsch:portfolio:cprogproject2 [2012/04/29 18:25] (current) – [Code] rmatsch | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ======Project: | ||
+ | |||
+ | A project for C++ by Robert Matsch during the spring 2012. | ||
+ | |||
+ | This project was begun on 3/12/2012 and is anticipated to take 5 days to complete. Project was completed on 3/20, 2012. | ||
+ | |||
+ | =====Objectives===== | ||
+ | Create a program that can take two numbers from user that are up to 24 didgits in lenth and allow user to choose from options such as to multiply, | ||
+ | =====Prerequisites===== | ||
+ | In order to successfully accomplish/ | ||
+ | |||
+ | * successful completion of project #1 and solid understanding of pertinent topics | ||
+ | * familiarity with memory allocation via **malloc(3)** | ||
+ | * familiarity with memory, accessing data via pointer dereferencing, | ||
+ | * familiarity with looking up C function parameters/ | ||
+ | * familiarity with functions, their parameters and return types | ||
+ | |||
+ | |||
+ | =====Scope===== | ||
+ | This project will have you implementing code to support the storage and manipulation of numbers outside of the established data types. | ||
+ | |||
+ | In C, from our first project (Project #0), we explored the various established data types, and determined their various sizes and representational ranges. | ||
+ | |||
+ | From that, we should know the largest value we can store in a variable using the biggest data type size (**unsigned long long int**), which is: **18, | ||
+ | |||
+ | That's a 20-digit number. | ||
+ | |||
+ | But this project will have us creating the ability to store and manipulate largers much larger than that. We'll start with a target of 4 and 24 digits (if you write your code effectively, | ||
+ | |||
+ | Why 4? Can't we already easily store values of 4 digits? | ||
+ | |||
+ | Yes, but looking to implement the ability to store and manipulate a 4 digit number will help us to better realize the logic and code necessary to scale our solution to support any number of digits. | ||
+ | |||
+ | While there are many approaches to this problem, follow through this example to get some insight. You don't have to take this approach, but it will cover some important concepts you will need to implement in your solution, whether or not you take this approach. | ||
+ | |||
+ | Let's look at a 4 digit number not as a side-effect of being able to be stored in a quantity of appropriate size, but as 4 literal stored digits in memory. To wit: | ||
+ | |||
+ | <code c> | ||
+ | unsigned char *value; | ||
+ | value = (unsigned char *) malloc (sizeof(unsigned char) * 4); | ||
+ | *(value+0) = *(value+1) = *(value+2) = *(value+3) = 0; | ||
+ | </ | ||
+ | |||
+ | What just happened here? Make sure you understand, or ask questions and get clarification before attempting to continue. | ||
+ | |||
+ | Essentially, | ||
+ | |||
+ | ^ 0 ^ 0 ^ 0 ^ 0 ^ | ||
+ | | *(value+0) | ||
+ | |||
+ | 4 bytes of memory, each containing a single digit of our 4 digit number. Let's assume we are attacking this as a decimal (base 10) value, and we'll maintain our assumption that the left-most value is the **most significant digit**, and the right-most value is the **least significant digit**. | ||
+ | |||
+ | For example, let's say we wanted to store the 4-digit number 8192 in memory using this scheme. The code and resulting " | ||
+ | |||
+ | <code c> | ||
+ | *(value+0) = 8; | ||
+ | *(value+1) = 1; | ||
+ | *(value+2) = 9; | ||
+ | *(value+3) = 2; | ||
+ | </ | ||
+ | |||
+ | ^ 8 ^ 1 ^ 9 ^ 2 ^ | ||
+ | | *(value+0) | ||
+ | |||
+ | Make sense? | ||
+ | |||
+ | Be aware that *(value+0), the first memory address of our sequence, is at the left side of our value... therefore it stores the **most significant digit**. You are free to do it the other way, just make sure that whatever approach you take, you maintain your logic. | ||
+ | |||
+ | Now, what if we wanted to perform an addition? | ||
+ | |||
+ | 8192+4 = 8196 | ||
+ | |||
+ | Pretty easy right? | ||
+ | |||
+ | 4 in our memory scheme would be represented as " | ||
+ | |||
+ | <code c> | ||
+ | *(value+0) = *(value+0) + 0; | ||
+ | *(value+1) = *(value+1) + 0; | ||
+ | *(value+2) = *(value+2) + 0; | ||
+ | *(value+3) = *(value+3) + 4; | ||
+ | </ | ||
+ | |||
+ | As you can see, the value of " | ||
+ | |||
+ | ^ 8 ^ 1 ^ 9 ^ 6 ^ | ||
+ | | *(value+0) | ||
+ | |||
+ | There' | ||
+ | |||
+ | But there' | ||
+ | |||
+ | Let's take our 8196 and add 1024 to it. What do we get? **9220** | ||
+ | |||
+ | Illustrated, | ||
+ | |||
+ | ^Carry: | ||
+ | ^Value: | ||
+ | ^Addend: | ||
+ | ^Sum: ^ 9 ^ 2 ^ 2 ^ 0 ^ | ||
+ | | ^ *(value+0) | ||
+ | |||
+ | So, for this project I'd like for you to write a set of functions and a test program that: | ||
+ | |||
+ | * have a function that will allocate space to store a value of desired length (at least 4 and 24, but feel free to test it with larger numbers: 32, 40, 64, etc.) and return the address (so we can assign it to one of our pointers). | ||
+ | * have a function that will **zero** your value, running through each position and setting it to 0. | ||
+ | * have a function that will accept as a parameter the original number and number to **add**, perform the operation, and place the result in the original number | ||
+ | * implement a function to tackle **subtraction** being mindful of the carry | ||
+ | * implement a function to perform **multiplication** | ||
+ | * implement a function to perform **division** | ||
+ | * implement a function that accepts as two arguments two of our dynamically allocated " | ||
+ | * implement a sample program that: | ||
+ | * prompts the user to enter a the number length (4 digits, 24 digits, 32 digits, etc.) | ||
+ | * prompts the user for actual values (you' | ||
+ | * gives the user a choice (perhaps via a menu) that lets them select from all the available functions (even resetting and starting over with new digit-lengths). | ||
+ | |||
+ | =====Code===== | ||
+ | < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | void sum(unsigned char *n1, | ||
+ | void subtraction(unsigned char *n1, unsigned char *n2, unsigned char *n3); | ||
+ | void multiply(unsigned char *n1, unsigned char *n2, unsigned char *n3); | ||
+ | void divide(unsigned char *n1, unsigned char *n2, unsigned char *n3); | ||
+ | int compare(unsigned char *n2, unsigned char *zero); | ||
+ | |||
+ | int compare(unsigned char *n2, | ||
+ | { | ||
+ | int a; | ||
+ | unsigned char flag; | ||
+ | flag=0; | ||
+ | for (a=23; a>=0; a--){ | ||
+ | if (*(n2+a)==*(zero+a)){ | ||
+ | flag=0; | ||
+ | }else{ | ||
+ | flag=1; | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | return(flag); | ||
+ | } | ||
+ | void sum(unsigned char *n1, | ||
+ | { | ||
+ | int a, z, i, x; | ||
+ | for (a= 23; a>=0; a--){ | ||
+ | *(n3+a) = 0; | ||
+ | } | ||
+ | for(a=23; a >= 0; a--){ | ||
+ | z = *(n3+a) + (*(n1+a)+ *(n2+a)); | ||
+ | if (z>9){ | ||
+ | i = z-10; | ||
+ | n3[a] = i; | ||
+ | n3[a-1] = n3[a-1]+1; | ||
+ | }if (z<9){ | ||
+ | n3[a]=z; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | void subtraction(unsigned char *n1, | ||
+ | { | ||
+ | int a, z, i, x; | ||
+ | for (a= 23; a>=0; a--){ | ||
+ | *(n3+a) = 0; | ||
+ | } | ||
+ | for(a=23; a>=0; a--){ | ||
+ | z = *(n3+a) + (*(n1+a) - *(n2+a)); | ||
+ | if (z>=0){ | ||
+ | n3[a]=z; | ||
+ | }else{ | ||
+ | n3[a-1]=n3[a-1]-1; | ||
+ | n3[a]=z+10; | ||
+ | |||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void multiply(unsigned char *n1, | ||
+ | { | ||
+ | unsigned char *zero, *one, *num1value, *copyn1, | ||
+ | int a, z, i, x; | ||
+ | |||
+ | copyn2=(unsigned char *)malloc(sizeof(unsigned char)*24); | ||
+ | copyn1=(unsigned char *)malloc(sizeof(unsigned char)*24); | ||
+ | zero=(unsigned char *)malloc(sizeof(unsigned char)*24); | ||
+ | one=(unsigned char *)malloc(sizeof(unsigned char)*24); | ||
+ | num1value=(unsigned char *)malloc(sizeof(unsigned char)*24); | ||
+ | num1value2=(unsigned char *)malloc(sizeof(unsigned char)*24); | ||
+ | for (a=23; a>=0; a--){ | ||
+ | *(num1value+a) = *(n1+a); | ||
+ | *(num1value2+a)=*(n1+a); | ||
+ | *(zero+a)=0; | ||
+ | *(n1+a)=0; | ||
+ | *(one+a)= 0; | ||
+ | *(copyn1+a)=*(n1+a); | ||
+ | *(copyn2+a)=*(n2+a); | ||
+ | } | ||
+ | *(one+23)= 1; | ||
+ | while (compare(n2, | ||
+ | sum(n1, | ||
+ | for (a=23; a>=0; a--){ | ||
+ | *(n1+a)=*(n3+a); | ||
+ | *(n3+a)=0; | ||
+ | } | ||
+ | subtraction(n2, | ||
+ | for (a=23; a>=0; a--){ | ||
+ | *(n2+a)=*(n3+a); | ||
+ | *(n3+a)=0; | ||
+ | } | ||
+ | } | ||
+ | for (a=23; a>=0; a--){ | ||
+ | *(n3+a)=*(n1+a); | ||
+ | *(n1+a)=*(num1value2+a); | ||
+ | *(n2+a)=*(copyn2+a); | ||
+ | } | ||
+ | } | ||
+ | void divide(unsigned char *n1, | ||
+ | { | ||
+ | |||
+ | | ||
+ | int a, z, i, x; | ||
+ | copyn2=(unsigned char *)malloc(sizeof(unsigned char)*24); | ||
+ | copyn1=(unsigned char *)malloc(sizeof(unsigned char)*24); | ||
+ | num1value2=(unsigned char *)malloc(sizeof(unsigned char)*24); | ||
+ | zero=(unsigned char *)malloc(sizeof(unsigned char)*24); | ||
+ | one=(unsigned char *)malloc(sizeof(unsigned char)*24); | ||
+ | num1value=(unsigned char *)malloc(sizeof(unsigned char)*24); | ||
+ | for (a=23; a>=0; a--){ | ||
+ | *(num1value+a) = *(n1+a); | ||
+ | *(zero+a)=0; | ||
+ | *(n1+a)=0; | ||
+ | *(one+a)= 0; | ||
+ | *(num1value2+a)=*(n1+a); | ||
+ | *(copyn1+a)=*(n1+a); | ||
+ | *(copyn2+a)=*(n2+a); | ||
+ | |||
+ | |||
+ | } | ||
+ | *(one+23)= 1; | ||
+ | while (compare(num1value, | ||
+ | sum(n1, | ||
+ | for (a=23; a>=0; a--){ | ||
+ | *(n1+a)=*(n3+a); | ||
+ | *(n3+a)=0; | ||
+ | } | ||
+ | subtraction(n2, | ||
+ | for (a=23; a>=0; a--){ | ||
+ | *(n2+a)=*(n3+a); | ||
+ | *(n3+a)=0; | ||
+ | } | ||
+ | } for (a=23; a>=0; a--){ | ||
+ | *(n3+a)=*(n1+a); | ||
+ | *(n1+a)=*(num1value2+a); | ||
+ | *(n2+a)=*(copyn2+a); | ||
+ | } | ||
+ | } | ||
+ | int main() { | ||
+ | unsigned char i, x,counter, operation, *n1, *n2, *n3, z, a, y,choice; | ||
+ | n1=(unsigned char *)malloc(sizeof(unsigned char)*24); | ||
+ | n2=(unsigned char *)malloc(sizeof(unsigned char)*24); | ||
+ | n3=(unsigned char *)malloc(sizeof(unsigned char)*24); | ||
+ | printf(" | ||
+ | printf(" | ||
+ | x = fgetc(stdin); | ||
+ | counter=0; | ||
+ | while(x != ' | ||
+ | x = x - 48; | ||
+ | *(n1+counter)= x; | ||
+ | x = fgetc(stdin); | ||
+ | counter++; | ||
+ | }printf(" | ||
+ | y = fgetc(stdin); | ||
+ | counter=0; | ||
+ | while(y != ' | ||
+ | y=y-48; | ||
+ | *(n2+counter)= y; | ||
+ | y = fgetc(stdin); | ||
+ | counter++; | ||
+ | }printf(" | ||
+ | printf(" | ||
+ | choice=0; | ||
+ | operation = fgetc(stdin); | ||
+ | choice = operation-48; | ||
+ | while (3 >= choice >= 0){ | ||
+ | if (choice ==0){ | ||
+ | sum(n1, | ||
+ | for(a=0; | ||
+ | printf(" | ||
+ | } | ||
+ | }if (choice ==1){ | ||
+ | | ||
+ | for(a=0; | ||
+ | printf(" | ||
+ | } | ||
+ | }if (choice ==2){ | ||
+ | multiply(n1, | ||
+ | for(a=0; | ||
+ | printf(" | ||
+ | } | ||
+ | }if (choice ==3){ | ||
+ | divide(n1, | ||
+ | for(a=0; | ||
+ | printf(" | ||
+ | } | ||
+ | }if (choice ==5){ | ||
+ | exit(1); | ||
+ | } | ||
+ | printf(" | ||
+ | choice=0; | ||
+ | operation = fgetc(stdin); | ||
+ | operation = fgetc(stdin); | ||
+ | choice = operation-48; | ||
+ | } | ||
+ | |||
+ | |||
+ | return(0); | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | =====Execution===== | ||
+ | |||
+ | <cli> | ||
+ | lab46: | ||
+ | please enter a number up to 24 digits long padded with zeros: | ||
+ | example: 000000000000000000000008 = 8 | ||
+ | 000000000000000000000015 | ||
+ | please enter another number up to 24 digits long padded with zeros: | ||
+ | 000000000000000000000003 | ||
+ | Addition, subtraction, | ||
+ | Enter 0 for Addition, 1 for Subtraction, | ||
+ | 0 | ||
+ | 000000000000000000000018 | ||
+ | if you would like to perform other operations: 0 for addition, 1 for subtraction, | ||
+ | 1 | ||
+ | 000000000000000000000012 | ||
+ | if you would like to perform other operations: 0 for addition, 1 for subtraction, | ||
+ | 2 | ||
+ | 000000000000000000000045 | ||
+ | if you would like to perform other operations: 0 for addition, 1 for subtraction, | ||
+ | 3 | ||
+ | 000000000000000000000005 | ||
+ | if you would like to perform other operations: 0 for addition, 1 for subtraction, | ||
+ | 5 | ||
+ | lab46: | ||
+ | |||
+ | |||
+ | </ | ||
+ | =====Reflection===== | ||
+ | upon completion of this project i learned how to use arrays to collect large user input and manipulate the input through simple repeated addition and subtraction. | ||
+ | |||
+ | =====References===== | ||
+ | In performing this project, the following resources were referenced: | ||
+ | |||
+ | C++ class notes | ||
+ | |||