This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
haas:spring2015:cprog:projects:afn0 [2015/02/26 13:06] – [Function definition] wedge | haas:spring2015:cprog:projects:afn0 [2015/03/20 20:45] (current) – [Prerequisites/Corequisites] wedge | ||
---|---|---|---|
Line 15: | Line 15: | ||
* can perform this trick in your head/by hand (if you can't do it on your own, you have no business trying to tell the computer how to do it) | * can perform this trick in your head/by hand (if you can't do it on your own, you have no business trying to tell the computer how to do it) | ||
- | * understand the pattern/ | + | * understand the pattern/ |
* ability to deploy loops to simplify your process | * ability to deploy loops to simplify your process | ||
* ability to use arrays to facilitate the storage of your processed values | * ability to use arrays to facilitate the storage of your processed values | ||
Line 189: | Line 189: | ||
=====Program===== | =====Program===== | ||
- | It is your task to write an optimized version of your multiply by eleven | + | It is your task to write a program that obtains a long integer value from the user, and processes that single value into separate array elements |
Your program should: | Your program should: | ||
* obtain its input from STDIN. | * obtain its input from STDIN. | ||
- | * input should be in the form of a single integer value | + | * input should be in the form of a single |
* determine the number of digits of the inputted value (store this in a variable) | * determine the number of digits of the inputted value (store this in a variable) | ||
- | * perform the correct algorithm against | + | |
- | * propagate any carries | + | * you may assume a maximum array size of the maximum number of digits you're theoretically able to input that can be stored in a 64-bit value. |
- | * use an array (**digit**) to store individual digits from the number input | + | |
- | * use another | + | * display the problem being solved, along with the answer |
- | * hint: you will want to make the **result** array one element larger. Why is this? | + | * use functions to modularize your code: |
- | * Display output showing aspects of the process | + | * have an **longint2array()** function that takes the long int, and returns an array (the function itself handles the processing |
- | | + | * have a **printarray()** function, whose responsibility it is to display the indicated |
+ | * have a **allfromnine()** function that takes the source array, does the processing, and returns ther result | ||
- | =====Execution===== | + | I might suggest the following function prototypes: |
- | Several operating behaviors are shown as examples. | + | |
- | An eight digit value: | + | <code c> |
+ | unsigned char *longint2array(unsigned long int); | ||
+ | void printarray(unsigned char *, unsigned char); | ||
+ | unsigned char *allfromnine(unsigned char *); | ||
+ | </ | ||
- | < | + | Some questions to contemplate: |
- | lab46:~/ | + | |
- | Enter value: 31415926 | + | |
- | Digits detected: 8 | + | |
- | Obtaining unique digits, storing in array... | + | * Why an array of unsigned chars when we're starting with a long (long) int? |
- | digit[0] = 6 | + | * Why is that the "best fit" size-wise? |
- | digit[1] = 2 | + | * Why will that not result in lost data? |
- | digit[2] = 9 | + | * Why unsigned? |
- | digit[3] = 5 | + | * What impact will that have on our input value' |
- | digit[4] = 1 | + | * Why represent the size of the usable array as an unsigned char? |
- | digit[5] = 4 | + | * Why is this the "best fit" size-wise? |
- | digit[6] | + | =====Execution===== |
- | digit[7] | + | An example |
- | + | ||
- | Applying process... | + | |
- | result[0] | + | |
- | result[1] | + | |
- | result[2] | + | |
- | result[3] | + | |
- | result[4] | + | |
- | result[5] | + | |
- | result[6] | + | |
- | result[7] | + | |
- | result[8] = 3 + 0 + 0 (sum of 3, carry out of 0) | + | |
- | + | ||
- | Displaying result... | + | |
- | 31415926 x 11 = 345575186 | + | |
- | lab46: | + | |
- | </ | + | |
- | + | ||
- | Next, a four digit value: | + | |
<cli> | <cli> | ||
- | lab46: | + | lab46: |
- | Enter value: | + | Enter value: |
- | Digits detected: | + | Digits detected: |
- | Obtaining unique digits, storing in array... | + | |
- | digit[0] = 4 | + | - 31415926535897 |
- | digit[1] = 0 | + | --------------- |
- | digit[2] = 1 | + | |
- | digit[3] = 7 | + | |
- | Applying process... | + | lab46: |
- | result[0] = 4 + 0 + 0 (sum of 4, carry out of 0) | + | |
- | result[1] = 0 + 4 + 0 (sum of 4, carry out of 0) | + | |
- | result[2] = 1 + 0 + 0 (sum of 1, carry out of 0) | + | |
- | result[3] = 7 + 1 + 0 (sum of 8, carry out of 0) | + | |
- | result[4] = 7 + 0 + 0 (sum of 7, carry out of 0) | + | |
- | + | ||
- | Displaying result... | + | |
- | 7104 x 11 = 78144 | + | |
- | lab46: | + | |
</ | </ | ||
- | |||
- | Finally, a five digit value: | ||
- | |||
- | <cli> | ||
- | lab46: | ||
- | Enter value: 56789 | ||
- | Digits detected: 5 | ||
- | |||
- | Obtaining unique digits, storing in array... | ||
- | digit[0] = 9 | ||
- | digit[1] = 8 | ||
- | digit[2] = 7 | ||
- | digit[3] = 6 | ||
- | digit[4] = 5 | ||
- | |||
- | Applying process... | ||
- | result[0] = 9 + 0 + 0 (sum of 9, carry out of 0) | ||
- | result[1] = 8 + 9 + 0 (sum of 7, carry out of 1) | ||
- | result[2] = 7 + 8 + 1 (sum of 6, carry out of 1) | ||
- | result[3] = 6 + 7 + 1 (sum of 4, carry out of 1) | ||
- | result[4] = 5 + 6 + 1 (sum of 2, carry out of 1) | ||
- | result[5] = 5 + 1 + 0 (sum of 6, carry out of 0) | ||
- | |||
- | Displaying result... | ||
- | 56789 x 11 = 624679 | ||
- | lab46: | ||
- | </ | ||
- | |||
- | The execution of the program is short and simple- obtain the input, do the processing, produce the output, and then terminate. | ||
=====Submission===== | =====Submission===== | ||
Line 309: | Line 253: | ||
<cli> | <cli> | ||
- | $ submit cprog mbe1 mbe1.c | + | $ submit cprog afn0 afn0.c |
- | Submitting cprog project "mbe1": | + | Submitting cprog project "afn0": |
- | -> mbe1.c(OK) | + | -> afn0.c(OK) |
SUCCESSFULLY SUBMITTED | SUCCESSFULLY SUBMITTED |