User Tools

Site Tools


haas:fall2015:cprog:projects:sam0

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:fall2015:cprog:projects:sam0 [2015/10/27 17:57] – [function calling] wedgehaas: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 accept input from **STDIN** until the **EOF** character is encountered.+Your program(s) should
 +  load the provided input (via STDIN) into an array for processing (you may want to check for an EOF character to terminate input) 
 +    you may assume a maximum input size of 4096 bytes 
 +    use the **fgetc()** function instead of **fscanf()** for reading input from STDIN 
 +  * obtain the signed char key value from the command-line, if present, or read from the 'cipher.key' file 
 +    * if neither command-line nor key file are available, display error and exit 
 +  * appropriately "shift" the alphabet according to the key (magnitude and direction and mode), storing the results in an array 
 +  * 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:
 </cli> </cli>
  
-====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.+<cli> 
 +lab46:~/src/cprog/sam0$ ./encode-sam0 
 +ERROR: key not found 
 +lab46:~/src/cprog/sam0$  
 +</cli>
  
-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 the function that will be calling it).+With 4 in the cipher.key file:
  
-A function is basically a module or subroutineIt is a mini-program, focusing on the performing of a particular process.+<cli> 
 +lab46:~/src/cprog/sam0$ ./encode-sam0 
 +hello there 
 +lipps xlivi 
 +^D 
 +lab46:~/src/cprog/sam0$  
 +</cli>
  
-Like a programit takes input, does processing, and provides output.+With 4 in the cipher.key filedecoding 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/sam0$ ./decode-sam0 
 +lipps xlivi 
 +hello there 
 +^D 
 +lab46:~/src/cprog/sam0$  
 +</cli>
  
-This distinctions aside, a function can in many ways be viewed as a microor sub-program/routine. We use functions to assist us in making our code more readable/organized/navigable. +Via positive command-line keydecoding:
- +
-Keeping everything in ONE fileONE 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. +
- +
-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). +
- +
-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's output), a name, and parameters (input). +
- +
-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()'s case, being the first function run, we tend to return a status code to the operating system-- 0 for success, non-zero for some sort of error or deferred success). +
- +
-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't ALWAYS have to come from the keyboard, nor does output ALWAYS have to go to the screen. Input instead is desired informating being acquired for the process at hand, and output is the byproduct of performing the operation. Sometimes this means keyboard input and screen output- but not always. +
- +
-Additionally, with or without parameters, we can always perform additional input (and output) within a given function, through the use of various input and output methods (like **fprintf()/fscanf()**). +
- +
-===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()): +
- +
-<code> +
-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't matter, type does) +
-  * the function's name (sum) +
-  * 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 "int"... we need to be mindful of this order to successfully call and use the function.+
  
 +<cli>
 +lab46:~/src/cprog/sam0$ ./decode-sam0 2
 +jgnnq 
 +hello
 +^D
 +lab46:~/src/cprog/sam0$ 
 +</cli>
  
 +You can also save typing, by providing your input via a here string (also a nice way to check for EOF):
  
 +<cli>
 +lab46:~/src/cprog/sam0$ ./decode-sam0 2 <<< "jgnnq" 
 +hello
 +lab46:~/src/cprog/sam0$ 
 +</cli>
 =====Submission===== =====Submission=====
 To successfully complete this project, the following criteria must be met: To successfully complete this project, the following criteria must be met:
haas/fall2015/cprog/projects/sam0.1445968653.txt.gz · Last modified: 2015/10/27 17:57 by wedge