This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
haas:fall2015:cprog:projects:sam0 [2015/10/27 17:57] – [Program] wedge | haas:fall2015:cprog:projects:sam0 [2015/10/29 12:26] (current) – [Sample execution: encode] wedge | ||
---|---|---|---|
Line 26: | Line 26: | ||
* encoding - taking the readable text and making it obfuscated, according to a set process | * encoding - taking the readable text and making it obfuscated, according to a set process | ||
* decoding - taking the obfuscated text and making it readable, by reversing the process | * decoding - taking the obfuscated text and making it readable, by reversing the process | ||
- | =====Task===== | + | =====Program===== |
You are to implement two programs (or at least a program with two fundamental modes of operation): | You are to implement two programs (or at least a program with two fundamental modes of operation): | ||
* encode program: takes plain text message as input, along with a cipher key, and outputs the encoded (obfuscated) result | * encode program: takes plain text message as input, along with a cipher key, and outputs the encoded (obfuscated) result | ||
Line 37: | Line 37: | ||
You can implement this as two separate programs, or as one program that behaves fundamentally different depending on how it is named (ie **encode-sam0** operates in encode mode, **decode-sam0** operates in decode mode). | You can implement this as two separate programs, or as one program that behaves fundamentally different depending on how it is named (ie **encode-sam0** operates in encode mode, **decode-sam0** operates in decode mode). | ||
- | Your program should | + | Your program(s) should: |
+ | | ||
+ | | ||
+ | | ||
+ | * obtain the signed char key value from the command-line, | ||
+ | * if neither command-line nor key file are available, display error and exit | ||
+ | * appropriately " | ||
+ | * for encoding, display the resultant enciphered string to STDOUT | ||
+ | * for decoding, display the resultant deciphered string to STDOUT | ||
====Sample execution: encode==== | ====Sample execution: encode==== | ||
Line 60: | Line 68: | ||
</ | </ | ||
- | ====Function prototype==== | + | Without command-line nor cipher.key file: |
- | Like variables, functions need to be declared. | + | |
- | We can declare them at various scopes (file/global, block/local)... if you wish for the function to be accessible by all functions within a program, you will want to declare it with a global scope. | + | < |
+ | lab46:~/src/cprog/ | ||
+ | ERROR: key not found | ||
+ | lab46: | ||
+ | </ | ||
- | If a particular function is only to be used by a specific function, and no others, you can opt to declare it local scope (ie within | + | With 4 in the cipher.key file: |
- | A function is basically a module or subroutine. It is a mini-program, focusing on the performing of a particular process. | + | < |
+ | lab46: | ||
+ | hello there | ||
+ | lipps xlivi | ||
+ | ^D | ||
+ | lab46: | ||
+ | </ | ||
- | Like a program, it takes input, does processing, and provides output. | + | With 4 in the cipher.key file, decoding previous message: |
- | Unlike a program, its input may not come from the keyboard, but instead from particular variables, and may not send output to the screen, but instead channel output in a way that it can be stored into a variable. | + | <cli> |
- | + | lab46:~/src/cprog/ | |
- | This distinctions aside, a function can in many ways be viewed as a micro- or sub-program/ | + | lipps xlivi |
- | + | hello there | |
- | Keeping everything in ONE file, ONE big function in that one file, is rather monolithic. In time, with sufficiently large programs, such an arrangement would become a tad unwieldy. So functions help to keep our focus short yet attentive. | + | ^D |
- | + | lab46:~/src/cprog/sam0$ | |
- | To create a function we must first declare (or prototype) it. This needs to happen BEFORE said function is ever used (just as with variables- you must declare a variable before it is first used, otherwise the compiler yells). | + | </cli> |
- | + | ||
- | A function, in many ways, is like a programmable variable (or is a variable with programming attached). | + | |
- | + | ||
- | As such, it has a return value of a type (the function' | + | |
- | + | ||
- | We see this with main()... here are two variations of a **main()** function declaration (technically also the start of the definition as well, in the case of **main()**): | + | |
- | + | ||
- | ===Parameterless function=== | + | |
- | + | ||
- | <code c> | + | |
- | int main() | + | |
- | </code> | + | |
- | + | ||
- | In this example, we see the declaration of main() where it has a return value of **int**, meaning, upon completion, main() will return a value corresponding with an int data type (also in main()' | + | |
- | + | ||
- | main(), in this case, takes no parameters (just an empty set of parenthesis)... due to this, we refer to this function as a parameterless function. A function without parameters. Without input. | + | |
- | + | ||
- | Now: this is technically a different form of input and output than you are used to. Input doesn' | + | |
- | + | ||
- | Additionally, | + | |
- | + | ||
- | ===Parametered function=== | + | |
- | + | ||
- | <code c> | + | |
- | int main(int argc, char **argv) | + | |
- | </code> | + | |
- | + | ||
- | In this case, our **main()** function actually takes parameters- two, in fact: | + | |
- | + | ||
- | - an integer, we are calling **argc** | + | |
- | - a double pointer, we are calling **argv** | + | |
- | + | ||
- | This function takes two parameters, two pieces of input, available to us in the form of variables, by those names, of those types. We make use of them as we need to in accomplishing the program at hand. | + | |
- | + | ||
- | So, when we wish to create functions of our own, we need: | + | |
- | + | ||
- | * the return type | + | |
- | * the function name | + | |
- | * 0 or more parameters, identifying their order and type | + | |
- | + | ||
- | For example, let us make a sum() function. Here would be a likely prototype (we'd place it above main()): | + | |
- | + | ||
- | < | + | |
- | int sum(int *, int); | + | |
- | </code> | + | |
- | + | ||
- | A function prototype (vs. its definition) will have a terminating semi-colon, as you see above. | + | |
- | + | ||
- | In our case, our sum() function has the following: | + | |
- | + | ||
- | * a return type of **int** (particular variable name doesn' | + | |
- | * the function' | + | |
- | * a comma-separated list of types corresponding to the parameters (again, variable names do not matter, but the type is important). | + | |
- | + | ||
- | Our sum() function will take an integer array (denoted by the int pointer above), and a size (the second, regular int). | + | |
- | + | ||
- | Now, parameter order very much matters. In our case, an "int *" came first, followed by an " | + | |
- | + | ||
- | + | ||
- | ====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. | + | |
- | + | ||
- | Here would be an example of calling the above-mentioned **sum()** function: | + | |
- | + | ||
- | <code c> | + | |
- | int scores[4]; | + | |
- | int tally = 0; | + | |
- | + | ||
- | scores[0] = 88; | + | |
- | scores[1] = 47; | + | |
- | scores[2] = 96; | + | |
- | scores[3] = 73; | + | |
- | + | ||
- | tally = sum(scores, 4); | + | |
- | </code> | + | |
- | + | ||
- | 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. | + | |
+ | Via positive command-line key, decoding: | ||
+ | <cli> | ||
+ | lab46: | ||
+ | jgnnq | ||
+ | hello | ||
+ | ^D | ||
+ | lab46: | ||
+ | </ | ||
- | =====Execution===== | + | You can also save typing, by providing |
- | An example of your program in action: | + | |
<cli> | <cli> | ||
- | lab46: | + | lab46: |
- | Enter value: 31415926535897 | + | hello |
- | Digits detected: 14 | + | |
- | + | ||
- | | + | |
- | - 31415926535897 | + | |
- | | + | |
- | 68584073464102 | + | |
lab46: | lab46: | ||
</ | </ | ||
- | |||
=====Submission===== | =====Submission===== | ||
To successfully complete this project, the following criteria must be met: | To successfully complete this project, the following criteria must be met: |