User Tools

Site Tools


haas:spring2015:cprog:projects:afn0

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
haas:spring2015:cprog:projects:afn0 [2015/02/25 14:40] – [Multiplying a number (of varying digits) by 11] wedgehaas: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/process to doing it for any length number (2-digit through 24-digdt)+  * understand the pattern/process to doing it for any length number (2-digit through 18-digdt)
   * 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:
 </code> </code>
  
 +====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 //calling//. We call the function, by name, providing and required parameters, and capturing any return value as we see fit.
  
-=====Program===== +Here would be an example of calling the above-mentioned **sum()** function:
-It is your task to write an optimized version of your multiply by eleven program that will use arrays and loops to enable you to enhance and expand the functional capabilities of your program. No longer will you be limited by 1-, 2-, or 3-digit numbers, but you will be able to input up to 8-digit numbers and have your program successfully determine the result (and 8 is merely an arbitrary value I picked, you should easily be able to up it to the tens of thousands and experience no change in functionality-- actually, our 8-digit limit is considering a data type limitation... the maximum size of an int: **signed int**s can have a maximum value of 2.4 billion, so unless we change to a different data type (or different method of inputting the source number), this will be our limitation.+
  
-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] 88; 
-Several operating behaviors are shown as examples.+scores[1] 47; 
 +scores[2] 96; 
 +scores[3] 73;
  
-An eight digit value:+tally = sum(scores, 4); 
 +</code>
  
-<cli> +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:~/src/cprog/mbe1$ ./mbe1 +
-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] 6 + 0 + 0 (sum of 6, carry out of 0) +It is your task to write a program that obtains a long integer value from the userand processes that single value into separate array elements (one digit per array element). Determining the number of digitsyou are to perform this "all from nine, last from ten" subtraction method on the number using array transactions, displaying a visual representation of the problem being solved to STDOUT.
-result[1] 2 + 6 + 0 (sum of 8, carry out of 0) +
-result[2] 9 + 2 + 0 (sum of 1, carry out of 1) +
-result[3] 5 + 9 + 1 (sum of 5, carry out of 1) +
-result[4] 1 + 5 + 1 (sum of 7, carry out of 0) +
-result[5] 4 + 1 + 0 (sum of 5, carry out of 0) +
-result[6] = 1 + 4 + 0 (sum of 5carry out of 0) +
-result[7] = 3 + 1 + 0 (sum of 4, carry out of 0) +
-result[8] = 3 + 0 + 0 (sum of 3carry out of 0)+
  
-Displaying result... +Your program should: 
-31415926 x 11 = 345575186 +  * obtain its input from STDIN. 
-lab46:~/src/cprog/mbe1$  +    * input should be in the form of a single (long) long integer value (you want a 64-bit data type) 
-</cli>+  * 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:~/src/cprog/mbe1$ ./mbe1 +unsigned char *longint2array(unsigned long int); 
-Enter value: 7104 +void printarray(unsigned char *, unsigned char); 
-Digits detected: 4+unsigned char *allfromnine(unsigned char *); 
 +</code>
  
-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 (longint? 
-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 in lost data? 
-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's upper bound? 
-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:~/src/cprog/mbe1$  +
-</cli> +
- +
-Finally, a five digit value:+
  
 <cli> <cli>
-lab46:~/src/cprog/mbe1$ ./mbe1 +lab46:~/src/cprog/afn0$ ./afn0 
-Enter value: 56789 +Enter value: 31415926535897 
-Digits detected: 5+Digits detected: 14
  
-Obtaining unique digits, storing in array... + 100000000000000 
-digit[0] = 9 +- 31415926535897 
-digit[1] = 8 + --------------- 
-digit[2] = 7 +  68584073464102
-digit[3] = 6 +
-digit[4] = 5+
  
-Applying process... +lab46:~/src/cprog/afn0
-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:~/src/cprog/mbe1+
 </cli> </cli>
- 
-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
haas/spring2015/cprog/projects/afn0.1424875213.txt.gz · Last modified: 2015/02/25 14:40 by wedge