======Project: BIG NUM====== A project for C/C++ by Corey Forman during the spring 2012. This project was begun on 3/15/12 and is anticipated to take 3/16/12 to complete. Project was completed on MONTH DAY, YEAR. =====Objectives===== State the purpose of this project. What is the point of this project? What do we hope to accomplish by undertaking it? =====Prerequisites===== In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved: * 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, and address calculation * familiarity with looking up C function parameters/information in the manual * familiarity with functions, their parameters and return types =====Background===== State the idea or purpose of the project. What are you attempting to pursue? Upon approval, you'll want to fill this section out with more detailed background information. DO NOT JUST PROVIDE A LINK. Providing any links to original source material, such as from a project page, is a good idea. You'll want to give a general overview of what is going to be accomplished (for example, if your project is about installing a web server, do a little write-up on web servers. What is it, why do we need one, how does it work, etc.) =====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,446,744,073,709,551,615** That's a 20-digit number. But this project will have us creating the ability to store and manipulate numbers much larger than that. We'll start with a target of 4 and 24 digits (if you write your code effectively, the number of digits should ultimately not matter). 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: 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, we have just allocated 4 bytes of memory (of type **unsigned char**), which are located consecutively in memory. To draw a picture, we'd have this: ^ 0 ^ 0 ^ 0 ^ 0 ^ | *(value+0) | *(value+1) | *(value+2) | *(value+3) | 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 "picture" would be as follows: *(value+0) = 8; *(value+1) = 1; *(value+2) = 9; *(value+3) = 2; ^ 8 ^ 1 ^ 9 ^ 2 ^ | *(value+0) | *(value+1) | *(value+2) | *(value+3) | 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 "0004", and we'd accomplish the addition as follows: *(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 "4" was added only to the last (**least significant**) digit stored in our value. Displaying it should should the expected answer: ^ 8 ^ 1 ^ 9 ^ 6 ^ | *(value+0) | *(value+1) | *(value+2) | *(value+3) | There's actually two situations that occur with adding... what we just saw was the straight "sum". In this case, the sum was the only meaningful result generated. But there's also another situation we can have, and that is a carry. A carry is when the result is too big to be stored in a single digit (ie a 2 digit number). So we react by storing the least significant digit and **carry**ing the most significant digit to the next placevalue. Let's take our 8196 and add 1024 to it. What do we get? **9220** Illustrated, we have: ^Carry: | 0 | 1 | 1 | 0 | ^Value: | 8 | 1 | 9 | 6 | ^Addend: | 1 | 0 | 2 | 4 | ^Sum: ^ 9 ^ 2 ^ 2 ^ 0 ^ | ^ *(value+0) ^ *(value+1) ^ *(value+2) ^ *(value+3) ^ 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 "numbers", compares them, and returns a -1 if the left parameter is greater, 0 if they are equal, and 1 if the right parameter is greater. * 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'll have to rig up a way to convert the user's input into the appropriate values to place in your managed data type * 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===== The encipher code: #include 2 #include 3 int subtraction(int *a1, int *a2,int); 4 int *add(int *a1, int *a2,int, int); 5 int main() 6 { 7 char junk; 8 int choice; 9 choice=0; 10 11 printf("Thank you for using this program. There are some rules. First no decimals. Second put spaces between each number ... not like this 123 butlike this 1 2 3 for des ired results. third no letters, the program will error out. please note 0 is a number so if you say you want 4 digits then put 4 0's then your digits it will not see those d igits. thank you for your time.\n\n\n"); 12 printf("please input which you would like to perform. [1] for adding,[2] for subtraction, [3] for devision, and [4] for multiplication: "); 13 14 // above is asking which action they wish to take. bellow will be the unfortunate if statements to see what action they selected. 15 fscanf(stdin, "%d", &choice); 16 scanf("%c", &junk); 17 // printf("%d is what you chose.\n", choice); 18 // a=choice+1; 19 // printf("%d is testing to see if it is a number by adding 1 and becomming %d\n",choice,a); 20 21 if( choice == 1 ) 22 { 23 printf(" you have chosen addition\n"); 24 char junk; 25 int *arr1, *arr2, sizea,sizeb,i,l,a,b,c,e,f,g,k; 26 i=-1;e=k=f=0; 27 a=b=c=sizea=sizeb=0; 28 29 printf("please enter the size of the first digit: "); 30 fscanf(stdin, "%d", &sizea); 31 scanf("%c", &junk); 32 33 34 35 36 arr1 = (int *)malloc( sizeof(int) * (sizea)); 37 arr2 = (int *)malloc( sizeof(int) * (sizea)); 38 39 printf("now enter your first number: "); 40 g=0; 41 while(g!='\n'-48) 42 { 43 g = fgetc(stdin); 44 g = g-48; 45 arr1[e] = g; 46 e = e+1; 47 // if (e=sizea) 48 // break; 49 } 50 g=0; 51 printf("now enter your second number, note that you may have to have 0's infront of your second number to make them both the same size: "); 52 while(g!='\n'-48) 53 { 54 g = fgetc(stdin); 55 g = g-48; 56 arr2[f] = g; 57 f = f + 1; 58 } 59 60 printf("your answere is: "); 61 for(l=sizea-1;l>=0;l--) 62 { 63 if( i == -1) 64 { 65 i = sizea - 1; 66 } 67 68 a = arr1[i]; 69 70 b = arr2[l]; 71 if( (a+b) < 10 ) 72 { 73 a = a + b; 74 75 } 76 else if( ( a + b ) >= 10 ) 77 { 78 c = arr1[i-1]; 79 80 c = c + 1; 81 82 arr1[i-1] = c; 83 a = a + b; 84 85 a = a - 10; 86 87 } 88 arr1[i] = a; 89 i=i-1; 90 } 91 if(arr1[0] >= 10) 92 { 93 c = arr1[0]; 94 c = c - 10; 95 arr1[0] = c; 96 printf("1"); 97 } 98 for(i=0;i=0;l--) 143 { 144 if(i == -1) 145 { 146 i = sizea -1; 147 } 148 a = arr1[i]; 149 b = arr2[l]; 150 if( a >= b ) 151 { 152 a = a - b; 153 } 154 else if( a < b) 155 { 156 c = arr1[i-1]; 157 c = c - 1; 158 arr1[i-1] = c; 159 a = a + 10; 160 a = a - b; 161 } 162 arr1[i] = a; 163 i=i-1; 164 } 165 166 for(i=0;i=0;l--) 274 { 275 276 a = arr1[i]; 277 b = arr2[l]; 278 if( a >= b ) 279 { 280 a = a - b; 281 } 282 else if( (a < b)&&(arr1[i-1]>0)) 283 { 284 c = arr1[i-1]; 285 c = c - 1; 286 arr1[i-1] = c; 287 a = a + 10; 288 a = a - b; 289 } 290 arr1[i] = a; 291 i=i-1; 292 } 293 p=p+1; 294 } 295 return(p); 296 } 297 298 int *add(int *arr1, int *arr2,int times, int sizea) 299 { 300 int l,i,a,b,c,p; 301 l=i=a=b=c=0; 302 p=1; 303 while( p != times) 304 { 305 i=sizea-1; 306 for(l=sizea-1;l>=0;l--) 307 { 308 309 a = arr1[i]; 310 311 b = arr2[l]; 312 if( (a+b) < 10 ) 313 { 314 a = a + b; 315 316 } 317 else if( ( a + b ) >= 10 ) 318 { 319 c = arr1[i-1]; 320 321 c = c + 1; 322 323 arr1[i-1] = c; 324 a = a + b; 325 326 a = a - 10; 327 328 } 329 arr1[i] = a; 330 i=i-1; 331 } 332 p = p + 1; 333 } 334 return(arr1); 335 } =====Execution===== ADDITION lab46:~/src/cprog/c++/project2$ ./bignum Thank you for using this program. There are some rules. First no decimals. Second put spaces between each number ... not like this 123 butlike this 1 2 3 for desired results. third no letters, the program will error out. please note 0 is a number so if you say you want 4 digits then put 4 0's then your digits it will not see those digits. thank you for your time. please input which you would like to perform. [1] for adding,[2] for subtraction, [3] for devision, and [4] for multiplication: 1 you have chosen addition please enter the size of the first digit: 4 now enter your first number: 4321 now enter your second number, note that you may have to have 0's infront of your second number to make them both the same size: 0043 your answere is: 4364 SUBTRACTION you have chosen subtraction please enter how many digets the first number consists of: 5 please enter now your first number: 64326 please enter your second number set please cushion your number to match the length of the first by adding 0's to the beginning of it: 03452 your answere is: 60874 DEVISION you have chosen devision please enter the size of your first digit: 3 now enter your first number: 555 now enter your second number. please cushion it with 0's to insure they are the same size ... 0's before the number you wish to enter: 055 your answere is 9 MULTIPLICATION you have chosen multiplication please enter how many digets the first number consists of: 3 please enter now your number: 025 now enter what you are multiplying it by: 005 your answer is 125 =====Reflection===== This was a super difficult but super fun to do program. It took many hours and much help but it was also a great learning experience. One key thing i learned is to read every letter of my programs because more often then not my mistakes lay in the fact that i put one letter into a variable or something just wrong so my program decides to have a spazz attack on me. Other then that issue it was amazing and im glad it is finished. =====References===== In performing this project, the following resources were referenced: * class