This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
user:mowens3:portfolio:cprogproject2 [2012/03/16 21:22] – [Reflection] mowens3 | user:mowens3:portfolio:cprogproject2 [2012/03/16 21:25] (current) – [Objectives] mowens3 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ======Project: | ||
+ | A project for COURSENAME by YOUR NAME during the SEMESTER YEAR. | ||
+ | |||
+ | This project was begun on DATE and is anticipated to take TIME UNIT 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? | ||
+ | |||
+ | In this project we are using arrays to an extreme and trying to understand how powerful arrays can be. | ||
+ | =====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 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===== | ||
+ | |||
+ | The multiplication and division don't work, I can't figure them out at all. | ||
+ | |||
+ | <code c> | ||
+ | /* | ||
+ | I need to make a selection statment with a function in each one | ||
+ | */ | ||
+ | # | ||
+ | # | ||
+ | |||
+ | /* | ||
+ | function prototypes here | ||
+ | */ | ||
+ | |||
+ | void addition(); | ||
+ | void subtraction(); | ||
+ | void multiplication(); | ||
+ | void division(); | ||
+ | char selection = 0; | ||
+ | char correct = 0; | ||
+ | char junk; | ||
+ | int main() | ||
+ | { | ||
+ | while (correct == 0) | ||
+ | { | ||
+ | printf(" | ||
+ | printf(" | ||
+ | scanf(" | ||
+ | scanf(" | ||
+ | // check for valid input | ||
+ | if ((selection < ' | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | else if (selection == ' | ||
+ | { | ||
+ | printf(" | ||
+ | exit(0); | ||
+ | } | ||
+ | else if ((selection >= ' | ||
+ | { | ||
+ | correct = 1; | ||
+ | } | ||
+ | } | ||
+ | if (selection == ' | ||
+ | { | ||
+ | addition(); | ||
+ | } | ||
+ | if (selection == ' | ||
+ | { | ||
+ | subtraction(); | ||
+ | } | ||
+ | if (selection == ' | ||
+ | { | ||
+ | multiplication(); | ||
+ | } | ||
+ | if (selection == ' | ||
+ | { | ||
+ | division(); | ||
+ | } | ||
+ | |||
+ | return(0); | ||
+ | } | ||
+ | |||
+ | void addition() | ||
+ | { | ||
+ | int spaces = 0; | ||
+ | char carry = 0; | ||
+ | int firstvar = 0; | ||
+ | int secondvar = 0; | ||
+ | int holder = 0; | ||
+ | int counter1 = 0; | ||
+ | int counter2 = 0; | ||
+ | int endcounter = 0; | ||
+ | printf(" | ||
+ | scanf(" | ||
+ | printf(" | ||
+ | char *variable1, *variable2, *variableholder; | ||
+ | variable1 = (char *)malloc(sizeof(char)* spaces); | ||
+ | variable2 = (char *)malloc(sizeof(char)* spaces); | ||
+ | variableholder = (char *)malloc(sizeof(char)* spaces); | ||
+ | endcounter = spaces - 1; | ||
+ | printf(" | ||
+ | holder = getchar(); | ||
+ | holder = getchar(); | ||
+ | //storing them into a temp holder below | ||
+ | while (holder != ' | ||
+ | { | ||
+ | *(variableholder + firstvar) = holder; | ||
+ | firstvar = firstvar + 1; | ||
+ | holder = getchar(); | ||
+ | } | ||
+ | //pulling them out of the temp holder | ||
+ | counter1 = 0; | ||
+ | firstvar = firstvar - 1; | ||
+ | while (firstvar >= 0) | ||
+ | { | ||
+ | *(variable1+((spaces-1)-counter1)) = (*(variableholder+firstvar)-48); | ||
+ | firstvar = firstvar - 1; | ||
+ | counter1 = counter1 + 1; | ||
+ | } | ||
+ | printf(" | ||
+ | holder = getchar(); | ||
+ | while (holder != ' | ||
+ | { | ||
+ | *(variableholder + secondvar) = holder; | ||
+ | secondvar = secondvar + 1; | ||
+ | holder = getchar(); | ||
+ | } | ||
+ | counter2 = 0; | ||
+ | secondvar = secondvar - 1; | ||
+ | while (secondvar >= 0) | ||
+ | { | ||
+ | *(variable2 + ((spaces -1) - counter2)) = (*(variableholder + secondvar) - 48); | ||
+ | secondvar = secondvar - 1; | ||
+ | counter2 = counter2 + 1; | ||
+ | } | ||
+ | //Adding the variables | ||
+ | printf(" | ||
+ | counter1 = counter1 - 1; | ||
+ | counter2 = counter2 - 1; | ||
+ | while((counter1 >= 0) || (counter2 >=0)) | ||
+ | { | ||
+ | if (counter2 < 0) | ||
+ | { | ||
+ | *(variableholder + endcounter) = *(variable1 + endcounter) + carry; | ||
+ | counter1 = counter1 - 1; | ||
+ | carry = 0; | ||
+ | } | ||
+ | else if (counter1 < 0) | ||
+ | { | ||
+ | *(variableholder + endcounter) = *(variable2 + endcounter) + carry; | ||
+ | counter2 = counter2 - 1; | ||
+ | carry = 0; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | holder = *(variable1 + endcounter) + *(variable2 + endcounter); | ||
+ | if (holder >= 10) | ||
+ | { | ||
+ | holder = holder - 10; | ||
+ | *(variableholder + endcounter) = holder + carry; | ||
+ | carry = 1; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | *(variableholder + endcounter) = holder + carry; | ||
+ | carry = 0; | ||
+ | } | ||
+ | counter1 = counter1 - 1; | ||
+ | counter2 = counter2 - 1; | ||
+ | } | ||
+ | endcounter = endcounter - 1; | ||
+ | } | ||
+ | if (carry > 0) | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | for (holder = 0; holder < spaces; holder ++) | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | printf(" | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | void subtraction() | ||
+ | { | ||
+ | int spaces = 0; | ||
+ | char carry = 0; | ||
+ | int firstvar = 0; | ||
+ | int secondvar = 0; | ||
+ | signed int holder = 0; | ||
+ | int counter1 = 0; | ||
+ | int counter2 = 0; | ||
+ | int endcounter = 0; | ||
+ | char resultnegitive = 0; //Used to print a - sign | ||
+ | printf(" | ||
+ | scanf(" | ||
+ | printf(" | ||
+ | char *variable1, *variable2, *variableholder; | ||
+ | variable1 = (char *)malloc(sizeof(char)* spaces); | ||
+ | variable2 = (char *)malloc(sizeof(char)* spaces); | ||
+ | variableholder = (char *)malloc(sizeof(char)* spaces); | ||
+ | endcounter = spaces - 1; | ||
+ | printf(" | ||
+ | holder = getchar(); | ||
+ | holder = getchar(); | ||
+ | //storing them into a temp holder below | ||
+ | while (holder != ' | ||
+ | { | ||
+ | *(variableholder + firstvar) = holder; | ||
+ | firstvar = firstvar + 1; | ||
+ | holder = getchar(); | ||
+ | } | ||
+ | //pulling them out of the temp holder | ||
+ | counter1 = 0; | ||
+ | firstvar = firstvar - 1; | ||
+ | while (firstvar >= 0) | ||
+ | { | ||
+ | *(variable1+((spaces-1)-counter1)) = (*(variableholder+firstvar)-48); | ||
+ | firstvar = firstvar - 1; | ||
+ | counter1 = counter1 + 1; | ||
+ | } | ||
+ | printf(" | ||
+ | holder = getchar(); | ||
+ | while (holder != ' | ||
+ | { | ||
+ | *(variableholder + secondvar) = holder; | ||
+ | secondvar = secondvar + 1; | ||
+ | holder = getchar(); | ||
+ | } | ||
+ | counter2 = 0; | ||
+ | secondvar = secondvar - 1; | ||
+ | while (secondvar >= 0) | ||
+ | { | ||
+ | *(variable2 + ((spaces -1) - counter2)) = (*(variableholder + secondvar) - 48); | ||
+ | secondvar = secondvar - 1; | ||
+ | counter2 = counter2 + 1; | ||
+ | } | ||
+ | // | ||
+ | printf(" | ||
+ | counter1 = counter1 - 1; | ||
+ | counter2 = counter2 - 1; | ||
+ | while((counter1 >= 0) || (counter2 >=0)) | ||
+ | { | ||
+ | if (carry == 1) | ||
+ | { | ||
+ | *(variable1 + endcounter) = *(variable1 + endcounter) - carry; | ||
+ | carry = 0; | ||
+ | } | ||
+ | if (counter2 < 0) | ||
+ | { | ||
+ | *(variableholder + endcounter) = *(variable1 + endcounter); | ||
+ | counter1 = counter1 - 1; | ||
+ | carry = 0; | ||
+ | } | ||
+ | else if (counter1 < 0) | ||
+ | { | ||
+ | *(variableholder + endcounter) = *(variable2 + endcounter); | ||
+ | counter2 = counter2 - 1; | ||
+ | carry = 0; | ||
+ | resultnegitive = 1; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | holder = *(variable1 + endcounter) - *(variable2 + endcounter); | ||
+ | if (holder < 0) | ||
+ | { | ||
+ | holder = holder + 10; | ||
+ | *(variableholder + endcounter) = holder; | ||
+ | carry = 1; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | *(variableholder + endcounter) = holder; | ||
+ | carry = 0; | ||
+ | } | ||
+ | counter1 = counter1 - 1; | ||
+ | counter2 = counter2 - 1; | ||
+ | } | ||
+ | endcounter = endcounter - 1; | ||
+ | } | ||
+ | if (resultnegitive == 1) | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | for (holder = 0; holder < spaces; holder ++) | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | printf(" | ||
+ | printf(" | ||
+ | } | ||
+ | void multiplication() | ||
+ | { | ||
+ | int spaces = 0; | ||
+ | char carry = 0; | ||
+ | int firstvar = 0; | ||
+ | int secondvar = 0; | ||
+ | int holder = 0; | ||
+ | int counter1 = 0; | ||
+ | int counter2 = 0; | ||
+ | int counter3 = 0; //used to reset counter1 later in code | ||
+ | int loopnumber = 0; | ||
+ | int endcounter = 0; | ||
+ | printf(" | ||
+ | scanf(" | ||
+ | printf(" | ||
+ | char *variable1, *variable2, *variableholder; | ||
+ | variable1 = (char *)malloc(sizeof(char)* spaces); | ||
+ | variable2 = (char *)malloc(sizeof(char)* spaces); | ||
+ | variableholder = (char *)malloc(sizeof(char)* spaces); | ||
+ | endcounter = spaces - 1; | ||
+ | printf(" | ||
+ | holder = getchar(); | ||
+ | holder = getchar(); | ||
+ | //storing them into a temp holder below | ||
+ | while (holder != ' | ||
+ | { | ||
+ | *(variableholder + firstvar) = holder; | ||
+ | firstvar = firstvar + 1; | ||
+ | holder = getchar(); | ||
+ | } | ||
+ | //pulling them out of the temp holder | ||
+ | counter1 = 0; | ||
+ | firstvar = firstvar - 1; | ||
+ | while (firstvar >= 0) | ||
+ | { | ||
+ | *(variable1+((spaces-1)-counter1)) = (*(variableholder+firstvar)-48); | ||
+ | firstvar = firstvar - 1; | ||
+ | counter1 = counter1 + 1; | ||
+ | } | ||
+ | printf(" | ||
+ | holder = getchar(); | ||
+ | while (holder != ' | ||
+ | { | ||
+ | *(variableholder + secondvar) = holder; | ||
+ | secondvar = secondvar + 1; | ||
+ | holder = getchar(); | ||
+ | } | ||
+ | counter2 = 0; | ||
+ | secondvar = secondvar - 1; | ||
+ | while (secondvar >= 0) | ||
+ | { | ||
+ | *(variable2 + ((spaces -1) - counter2)) = (*(variableholder + secondvar) - 48); | ||
+ | secondvar = secondvar - 1; | ||
+ | counter2 = counter2 + 1; | ||
+ | } | ||
+ | // | ||
+ | printf(" | ||
+ | counter1 = counter1 - 1; | ||
+ | counter2 = counter2 - 1; | ||
+ | counter3 = counter1; | ||
+ | while (counter1 >= 0) | ||
+ | { | ||
+ | *(variableholder + counter1) = 0; | ||
+ | counter1 = counter1 - 1; | ||
+ | } | ||
+ | counter1 = counter3; | ||
+ | while(counter2 >=0) | ||
+ | { | ||
+ | holder = *(variable1 + counter1) * *(variable2 + counter2); | ||
+ | if ((counter1 != 0) && (counter2 != 0)) | ||
+ | { | ||
+ | | ||
+ | carry = holder / 10; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | *(variableholder + endcounter) = (holder) + *(variableholder + endcounter - loopnumber) + carry; | ||
+ | } | ||
+ | if (counter1 == 0) | ||
+ | { | ||
+ | *(variableholder + counter1) = *(variableholder + counter1) + carry; | ||
+ | } | ||
+ | if (counter1 > 0) | ||
+ | { | ||
+ | counter1 = counter1 - 1; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | counter2 = counter2 - 1; | ||
+ | counter1 = counter3; | ||
+ | loopnumber = loopnumber + 1; | ||
+ | } | ||
+ | } | ||
+ | for (holder = 0; holder < spaces; holder ++) | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | printf(" | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | void division() | ||
+ | { | ||
+ | int spaces = 0; | ||
+ | char carry = 0; | ||
+ | printf(" | ||
+ | scanf(" | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | =====Execution===== | ||
+ | Here is an example of the Addition running. | ||
+ | |||
+ | <cli> | ||
+ | lab46: | ||
+ | What would you like to do; | ||
+ | 1: Addition, 2: Subtraction, | ||
+ | What is the maximum number of spaces you need for this addition: 3 | ||
+ | You put 3 | ||
+ | Please enter the first number: 123 | ||
+ | Please enter the second number: 23 | ||
+ | Adding the variables now | ||
+ | 146 | ||
+ | Done | ||
+ | |||
+ | </ | ||
+ | =====Reflection===== | ||
+ | I learned that messing with arrays like this can be a lot of typing and thoughts. | ||
+ | =====References===== | ||
+ | In performing this project, the following resources were referenced: | ||
+ | I used what we were taught in class and my own general knowledge, plus some help from the awesome teacher Matt. |