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/25 14:40] – [Multiplying a number (of varying digits) by 11] 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 168: | Line 168: | ||
</ | </ | ||
+ | ====function calling==== | ||
+ | Once we've declared (prototyped) and defined our function, now all we have to do is use it! When you make use of a function, we refer to it as // | ||
- | =====Program===== | + | Here would be an example |
- | It is your task to write an optimized version | + | |
- | Your program should: | + | <code c> |
- | * obtain its input from STDIN. | + | int scores[4]; |
- | * input should be in the form of a single integer value | + | int tally = 0; |
- | * determine the number of digits of the inputted value (store this in a variable) | + | |
- | * perform the correct algorithm against the input | + | |
- | * propagate any carries | + | |
- | * use an array (**digit**) to store individual digits from the number input | + | |
- | * use another array (**result**) to store the digits of the result number, following manipulations | + | |
- | * hint: you will want to make the **result** array one element larger. Why is this? | + | |
- | * Display output showing aspects of the process (see example execution below) | + | |
- | * output the final value (by iterating through the array, displaying one value at a time) | + | |
- | =====Execution===== | + | scores[0] |
- | Several operating behaviors are shown as examples. | + | scores[1] |
+ | scores[2] | ||
+ | scores[3] | ||
- | An eight digit value: | + | tally = sum(scores, 4); |
+ | </ | ||
- | < | + | Note, that it is rather important to match the type and order of parameters. Due to the nature of the array (especially the form of array declaration) used, certain pointer-related details are being hidden from us, giving somewhat of a false impression. Further discussion about pointers will begin to shed light on that. |
- | lab46: | + | |
- | Enter value: 31415926 | + | |
- | Digits detected: 8 | + | |
- | Obtaining unique digits, storing in array... | ||
- | digit[0] = 6 | ||
- | digit[1] = 2 | ||
- | digit[2] = 9 | ||
- | digit[3] = 5 | ||
- | digit[4] = 1 | ||
- | digit[5] = 4 | ||
- | digit[6] = 1 | ||
- | digit[7] = 3 | ||
- | Applying process... | + | =====Program===== |
- | result[0] | + | 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 |
- | result[1] | + | |
- | result[2] | + | |
- | result[3] | + | |
- | result[4] | + | |
- | result[5] | + | |
- | result[6] = 1 + 4 + 0 (sum of 5, carry out of 0) | + | |
- | result[7] = 3 + 1 + 0 (sum of 4, carry out of 0) | + | |
- | result[8] = 3 + 0 + 0 (sum of 3, carry out of 0) | + | |
- | Displaying result... | + | Your program should: |
- | 31415926 x 11 = 345575186 | + | * obtain its input from STDIN. |
- | lab46:~/ | + | * input should be in the form of a single (long) long integer value (you want a 64-bit data type) |
- | </ | + | * determine the number of digits of the inputted value (store this in a variable) |
+ | * process that input long integer into separate array elements- one digit per element. | ||
+ | * 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. | ||
+ | * perform the "all from nine, the last from ten" operation on the array, storing the result in another array. | ||
+ | * display the problem being solved, along with the answer | ||
+ | * use functions to modularize your code: | ||
+ | * have an **longint2array()** function that takes the long int, and returns an array (the function itself handles the processing of splitting up the long int into individual digits). | ||
+ | * have a **printarray()** function, whose responsibility it is to display the indicated array to STDOUT. | ||
+ | * have a **allfromnine()** function that takes the source array, does the processing, and returns ther result array. | ||
- | Next, a four digit value: | + | I might suggest the following function prototypes: |
- | <cli> | + | <code c> |
- | lab46: | + | unsigned char *longint2array(unsigned long int); |
- | Enter value: 7104 | + | void printarray(unsigned char *, unsigned char); |
- | Digits detected: 4 | + | unsigned char *allfromnine(unsigned char *); |
+ | </ | ||
- | Obtaining unique digits, storing in array... | + | Some questions to contemplate: |
- | digit[0] = 4 | + | |
- | digit[1] = 0 | + | |
- | digit[2] = 1 | + | |
- | digit[3] = 7 | + | |
- | Applying process... | + | * Why an array of unsigned chars when we're starting with a long (long) int? |
- | result[0] = 4 + 0 + 0 (sum of 4, carry out of 0) | + | * Why is that the "best fit" size-wise? |
- | result[1] = 0 + 4 + 0 (sum of 4, carry out of 0) | + | * Why will that not result |
- | result[2] = 1 + 0 + 0 (sum of 1, carry out of 0) | + | * Why unsigned? |
- | result[3] = 7 + 1 + 0 (sum of 8, carry out of 0) | + | * What impact will that have on our input value' |
- | result[4] = 7 + 0 + 0 (sum of 7, carry out of 0) | + | * Why represent the size of the usable array as an unsigned char? |
- | + | * Why is this the "best fit" size-wise? | |
- | Displaying result... | + | =====Execution===== |
- | 7104 x 11 = 78144 | + | An example of your program in action: |
- | lab46: | + | |
- | </ | + | |
- | + | ||
- | Finally, a five digit value: | + | |
<cli> | <cli> | ||
- | lab46: | + | lab46: |
- | Enter value: | + | Enter value: |
- | Digits detected: | + | Digits detected: |
- | Obtaining unique digits, storing in array... | + | |
- | digit[0] = 9 | + | - 31415926535897 |
- | digit[1] = 8 | + | --------------- |
- | digit[2] = 7 | + | |
- | digit[3] = 6 | + | |
- | digit[4] = 5 | + | |
- | Applying process... | + | lab46: |
- | 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 290: | 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 |