This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
user:cforman:portfolio:cprogproject2 [2012/05/07 14:54] – [Reflection] cforman | user:cforman:portfolio:cprogproject2 [2012/05/07 14:54] (current) – [References] cforman | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ======Project: | ||
+ | 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/ | ||
+ | |||
+ | * 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 | ||
+ | |||
+ | =====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, | ||
+ | |||
+ | 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, | ||
+ | |||
+ | 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===== | ||
+ | |||
+ | The encipher code: | ||
+ | |||
+ | <code c> | ||
+ | # | ||
+ | 2 #include < | ||
+ | 3 int subtraction(int *a1, int *a2,int); | ||
+ | 4 int *add(int *a1, int *a2, | ||
+ | 5 int main() | ||
+ | 6 { | ||
+ | 7 char junk; | ||
+ | 8 int choice; | ||
+ | 9 | ||
+ | 10 | ||
+ | | ||
+ | | ||
+ | 13 | ||
+ | | ||
+ | | ||
+ | | ||
+ | 17 // printf(" | ||
+ | 18 // a=choice+1; | ||
+ | 19 // printf(" | ||
+ | 20 | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | 28 | ||
+ | | ||
+ | | ||
+ | | ||
+ | 32 | ||
+ | 33 | ||
+ | 34 | ||
+ | 35 | ||
+ | | ||
+ | | ||
+ | 38 | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | 47 // if (e=sizea) | ||
+ | 48 // break; | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | 59 | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | 67 | ||
+ | | ||
+ | 69 | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | 74 | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | 79 | ||
+ | | ||
+ | 81 | ||
+ | | ||
+ | | ||
+ | 84 | ||
+ | | ||
+ | 86 | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | 97 } | ||
+ | | ||
+ | | ||
+ | 100 | ||
+ | 101 | ||
+ | 102 | ||
+ | 103 } | ||
+ | 104 | ||
+ | 105 } | ||
+ | 106 if( choice == 2) | ||
+ | 107 { | ||
+ | 108 | ||
+ | 109 char junk; | ||
+ | 110 int *arr1, | ||
+ | 111 | ||
+ | 112 | ||
+ | 113 | ||
+ | 114 | ||
+ | 115 | ||
+ | 116 | ||
+ | 117 arr1 = (int *)malloc( sizeof(int) * (sizea)); | ||
+ | 118 | ||
+ | 119 arr2 = (int *)malloc( sizeof(int) * (sizea)); | ||
+ | 120 | ||
+ | 121 | ||
+ | 122 | ||
+ | 123 g=0; | ||
+ | 124 | ||
+ | 125 { | ||
+ | 126 g = fgetc(stdin); | ||
+ | 127 g = g - 48; | ||
+ | 128 | ||
+ | 129 e = e +1; | ||
+ | 130 | ||
+ | 131 } | ||
+ | 132 g=0; | ||
+ | 133 | ||
+ | 134 | ||
+ | 135 { | ||
+ | 136 g = fgetc(stdin); | ||
+ | 137 g = g -48; | ||
+ | 138 | ||
+ | 139 f = f +1; | ||
+ | 140 } | ||
+ | 141 | ||
+ | 142 | ||
+ | 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 | ||
+ | 159 a = a + 10; | ||
+ | 160 a = a - b; | ||
+ | 161 } | ||
+ | 162 | ||
+ | 163 | ||
+ | 164 } | ||
+ | 165 | ||
+ | 166 | ||
+ | 167 { | ||
+ | 168 | ||
+ | 169 | ||
+ | 170 | ||
+ | 171 } | ||
+ | 172 | ||
+ | 173 | ||
+ | 174 | ||
+ | 175 | ||
+ | 176 | ||
+ | 177 } | ||
+ | 178 if( choice == 3) | ||
+ | 179 { | ||
+ | 180 | ||
+ | 181 char junk; | ||
+ | 182 int *arr1, *arr2, sizea, | ||
+ | 183 | ||
+ | 184 | ||
+ | 185 | ||
+ | 186 | ||
+ | 187 | ||
+ | 188 | ||
+ | 189 | ||
+ | 190 | ||
+ | 191 | ||
+ | 192 arr1 = (int *)malloc( sizeof(int) * (sizea)); | ||
+ | 193 arr2 = (int *)malloc( sizeof(int) * (sizea)); | ||
+ | 194 | ||
+ | 195 | ||
+ | 196 g=0; | ||
+ | 197 | ||
+ | 198 { | ||
+ | 199 g = fgetc(stdin); | ||
+ | 200 g = g-48; | ||
+ | 201 | ||
+ | 202 e = e+1; | ||
+ | 203 } | ||
+ | 204 | ||
+ | 205 g=0; | ||
+ | 206 | ||
+ | 207 | ||
+ | 208 { | ||
+ | 209 g = fgetc(stdin); | ||
+ | 210 g = g -48; | ||
+ | 211 | ||
+ | 212 f = f +1; | ||
+ | 213 } | ||
+ | 214 | ||
+ | 215 | ||
+ | 216 } | ||
+ | 217 if( choice == 4) | ||
+ | 218 { | ||
+ | 219 | ||
+ | 220 | ||
+ | 221 char junk; | ||
+ | 222 int *arr1, | ||
+ | 223 | ||
+ | 224 | ||
+ | 225 | ||
+ | 226 | ||
+ | 227 | ||
+ | 228 | ||
+ | 229 arr1 = (int *)malloc( sizeof(int) * (sizea)); | ||
+ | 230 | ||
+ | 231 arr2 = (int *)malloc( sizeof(int) * (sizea)); | ||
+ | 232 | ||
+ | 233 | ||
+ | 234 | ||
+ | 235 g=0; | ||
+ | 236 | ||
+ | 237 { | ||
+ | 238 g = fgetc(stdin); | ||
+ | 239 g = g - 48; | ||
+ | 240 | ||
+ | 241 | ||
+ | 242 e = e +1; | ||
+ | 243 } | ||
+ | 244 | ||
+ | 245 | ||
+ | 246 | ||
+ | 247 | ||
+ | 248 | ||
+ | 249 | ||
+ | 250 | ||
+ | 251 | ||
+ | 252 { | ||
+ | 253 | ||
+ | 254 } | ||
+ | 255 | ||
+ | 256 } | ||
+ | 257 if( (choice != 1) && (choice !=2) && (choice != 3) && (choice != 4)); { | ||
+ | 258 | ||
+ | 259 | ||
+ | 260 } | ||
+ | 261 | ||
+ | 262 | ||
+ | 263 } | ||
+ | 264 | ||
+ | 265 | ||
+ | 266 int subtraction(int *arr1, int *arr2,int sizea) | ||
+ | 267 { | ||
+ | 268 int l, | ||
+ | 269 | ||
+ | 270 | ||
+ | 271 { | ||
+ | 272 | ||
+ | 273 | ||
+ | 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)&& | ||
+ | 283 { | ||
+ | 284 c = arr1[i-1]; | ||
+ | 285 c = c - 1; | ||
+ | 286 | ||
+ | 287 a = a + 10; | ||
+ | 288 a = a - b; | ||
+ | 289 } | ||
+ | 290 | ||
+ | 291 | ||
+ | 292 } | ||
+ | 293 | ||
+ | 294 } | ||
+ | 295 | ||
+ | 296 } | ||
+ | 297 | ||
+ | 298 int *add(int *arr1, int *arr2,int times, int sizea) | ||
+ | 299 { | ||
+ | 300 int l, | ||
+ | 301 | ||
+ | 302 p=1; | ||
+ | 303 | ||
+ | 304 { | ||
+ | 305 | ||
+ | 306 | ||
+ | 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 | ||
+ | 324 a = a + b; | ||
+ | 325 | ||
+ | 326 a = a - 10; | ||
+ | 327 | ||
+ | 328 } | ||
+ | 329 | ||
+ | 330 | ||
+ | 331 } | ||
+ | 332 p = p + 1; | ||
+ | 333 } | ||
+ | 334 | ||
+ | 335 } | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | =====Execution===== | ||
+ | |||
+ | ADDITION | ||
+ | <cli> | ||
+ | lab46: | ||
+ | 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, | ||
+ | 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 | ||
+ | |||
+ | <cli> | ||
+ | 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 | ||
+ | |||
+ | <cli> | ||
+ | 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 | ||
+ | |||
+ | <cli> | ||
+ | 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 |