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:09] – [Submission] 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 | ||
+ | I might suggest the following function prototypes: | ||
+ | |||
+ | <code c> | ||
+ | unsigned char *longint2array(unsigned long int); | ||
+ | void printarray(unsigned char *, unsigned char); | ||
+ | unsigned char *allfromnine(unsigned char *); | ||
+ | </ | ||
+ | |||
+ | Some questions to contemplate: | ||
+ | |||
+ | * Why an array of unsigned chars when we're starting with a long (long) int? | ||
+ | * Why is that the "best fit" size-wise? | ||
+ | * Why will that not result in lost data? | ||
+ | * Why unsigned? | ||
+ | * What impact will that have on our input value' | ||
+ | * Why represent the size of the usable array as an unsigned char? | ||
+ | * Why is this the "best fit" size-wise? | ||
=====Execution===== | =====Execution===== | ||
An example of your program in action: | An example of your program in action: |