This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
user:asowers:end_of_year_review [2012/12/04 19:56] – [Data type project] asowers | user:asowers:end_of_year_review [2012/12/07 15:55] (current) – [Cipher project] asowers | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====Welcome==== | ||
+ | ====Reason for independent study instead of traditional lecture==== | ||
+ | It is my firm belief that success correlates with strong internal motivation. What does that mean in this instance? My reason in opting for this independent study was to see if I'm capable of grasping these fundamental concepts with my own devices. I've been extremely pleased with the results. | ||
+ | |||
+ | ====Scope of experience==== | ||
+ | Over the last three months, I've explored various concepts of C programming. In my studies I explored syntactical structures, various logical operations, and principals that make up a C program. The following projects demonstrate what I've explored. | ||
+ | ====Fun and general projects==== | ||
+ | |||
+ | I've developed two projects that solve simplex problems in a very fun way. The first is a drinking game called "Power Hour" that works with loop incrementing that adds up to one hour. The loop wait 60 seconds using the sleep function via time.h: | ||
+ | <code c> | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | printf(" | ||
+ | int minute; | ||
+ | for(minute = 0; minute <= 60; ++minute) | ||
+ | { | ||
+ | printf(" | ||
+ | sleep(60); | ||
+ | printf(" | ||
+ | } | ||
+ | printf(" | ||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The second fun project involves using a raspberry pi's General Purpose I/O to connect to solid state relay switch that powers a coffee machine. I call it "Lair Brew." The program makes heavy use of a library that gives the Pi Arduinio like I/O functions: | ||
+ | |||
+ | <code c> | ||
+ | /* | ||
+ | Lair_Brew ~ Caffeine ~ | ||
+ | Version 0.2 | ||
+ | */ | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | void loopback(void) | ||
+ | { | ||
+ | char ch; | ||
+ | ch = getchar(); | ||
+ | while( ch != ' | ||
+ | } | ||
+ | |||
+ | int main (void) | ||
+ | { | ||
+ | int pin = 7; | ||
+ | char ch; | ||
+ | int exit_flag = 0; | ||
+ | printf(" | ||
+ | |||
+ | if (wiringPiSetup() == -1) return(1); | ||
+ | |||
+ | pinMode(pin, | ||
+ | printf(" | ||
+ | digitalWrite(pin, | ||
+ | |||
+ | | ||
+ | printf(" | ||
+ | scanf(" | ||
+ | ch = toupper( ch ); | ||
+ | |||
+ | | ||
+ | printf(" | ||
+ | digitalWrite(pin, | ||
+ | delay(250); | ||
+ | printf(" | ||
+ | } | ||
+ | else { | ||
+ | printf(" | ||
+ | loopback(); | ||
+ | } | ||
+ | if( ch == ' | ||
+ | } | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Playing with simple structs: | ||
+ | <code c> | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | typedef struct { | ||
+ | int a; | ||
+ | int b; | ||
+ | int c; | ||
+ | char abc[]; | ||
+ | }node; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | node n; | ||
+ | n.a = 1; | ||
+ | n.b = 2; | ||
+ | n.c = 3; | ||
+ | strcpy(n.abc, | ||
+ | printf(" | ||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | ====Projects assigned by Matt==== | ||
+ | The following projects were assigned by Matt in chronological order. | ||
+ | ====Data type project==== | ||
+ | |||
+ | This project went over different data types and the storage capabilities of those types. | ||
+ | |||
+ | The following code demonstrates the storage range of a signed and unsigned char: | ||
+ | <code c> | ||
+ | /* | ||
+ | * range.c - A program to display information for signed and unsigned data types | ||
+ | * | ||
+ | * | ||
+ | * Compile with: gcc -o range range.c -lm | ||
+ | * Execute with: ./range | ||
+ | */ | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | // Variables | ||
+ | unsigned long long int quantity = 0; | ||
+ | unsigned char uc = 0; | ||
+ | signed char sc = 0; | ||
+ | |||
+ | // Display information for unsigned char data type | ||
+ | printf(" | ||
+ | printf(" | ||
+ | quantity = (unsigned char)(uc-1) + 1; // What does this line do? | ||
+ | printf(" | ||
+ | |||
+ | // Display information for signed char data type | ||
+ | printf(" | ||
+ | quantity = (unsigned long long int)pow(2, (sizeof(sc)*8)); | ||
+ | printf(" | ||
+ | printf(" | ||
+ | |||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | ====Cipher project==== | ||
+ | The cipher project entails taking a string value that is either entered or within a file and shifting it to another value. the shift uses ascii as its base. | ||
+ | |||
+ | encipher.c | ||
+ | <code c> | ||
+ | /* | ||
+ | * encipher.c - program that encodes a message according to a key | ||
+ | * | ||
+ | * | ||
+ | * Compile with: gcc -o encipher encipher.c | ||
+ | * | ||
+ | * Place key value in: key.txt | ||
+ | * Place message to encipher in: plain.txt | ||
+ | * Enciphered message placed in: cipher.txt | ||
+ | * | ||
+ | * Execute with: ./encipher KEYVAL | ||
+ | * | ||
+ | */ | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int main(int argc, char **argv) | ||
+ | { | ||
+ | |||
+ | printf(" | ||
+ | FILE *in, *out; | ||
+ | int cipher; | ||
+ | int key; | ||
+ | |||
+ | in = fopen(" | ||
+ | out = fopen(" | ||
+ | |||
+ | if(argv[1] == NULL) //if program called without argument | ||
+ | { | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | char *plain; | ||
+ | plain = (char*) malloc (sizeof(char) * 127); //memory allocate for ascii | ||
+ | int count = 0; | ||
+ | |||
+ | |||
+ | //when text file is empty | ||
+ | if(in == NULL) | ||
+ | { | ||
+ | printf(" | ||
+ | fgets(plain, | ||
+ | while(plain[count] != ' | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | |||
+ | int cipher = atoi(argv[1]); | ||
+ | count = 0; //begining | ||
+ | |||
+ | while(plain[count] != ' | ||
+ | { | ||
+ | key = 1; | ||
+ | if(plain[count] >= 65 && plain[count] <= 90) //upper case letters | ||
+ | { | ||
+ | key = plain[count] + cipher; | ||
+ | while(key > 90) | ||
+ | { | ||
+ | key = key - 26; //cipher shift | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | { | ||
+ | | ||
+ | while(key > 122) | ||
+ | { | ||
+ | key = key - 26; //cipher shift | ||
+ | } | ||
+ | } | ||
+ | if(key == 1) | ||
+ | { | ||
+ | key = 32; | ||
+ | } | ||
+ | fprintf(out, | ||
+ | count = count+1; | ||
+ | } | ||
+ | printf(" | ||
+ | } | ||
+ | else //if file is present | ||
+ | { | ||
+ | fgets(plain, | ||
+ | printf(" | ||
+ | while(plain[count] != ' | ||
+ | { | ||
+ | printf(" | ||
+ | count = count+1; | ||
+ | } | ||
+ | int cipher = atoi(argv[1]); | ||
+ | count = 0; //begining | ||
+ | printf(" | ||
+ | while(plain[count] != ' | ||
+ | { | ||
+ | key = 1; | ||
+ | if(plain[count] >= 65 && plain[count] <= 90) //uppercase characters | ||
+ | { | ||
+ | key = plain[count] + cipher; | ||
+ | while(key > 90) | ||
+ | { | ||
+ | key = key - 26; //cipher shift | ||
+ | } | ||
+ | } | ||
+ | if(plain[count] >= 97 && plain[count] <= 122) //lowercase characters | ||
+ | { | ||
+ | | ||
+ | | ||
+ | { | ||
+ | key = key - 26; //cipher shift | ||
+ | } | ||
+ | } | ||
+ | if(key == 1) | ||
+ | { | ||
+ | key = 32; | ||
+ | } | ||
+ | printf(" | ||
+ | fprintf(out, | ||
+ | count = count+1; | ||
+ | } | ||
+ | printf(" | ||
+ | fclose(in); | ||
+ | fclose(out); | ||
+ | } | ||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | decipher.c | ||
+ | <code c> | ||
+ | /* | ||
+ | * decipher.c - program that encodes a message according to a key | ||
+ | * | ||
+ | * | ||
+ | * Compile with: gcc -o encipher encipher.c | ||
+ | * | ||
+ | * Place key value in: key.txt | ||
+ | * Place message to encipher in: plain.txt | ||
+ | * Enciphered message placed in: cipher.txt | ||
+ | * | ||
+ | * Execute with: ./decipher KEYVAL | ||
+ | * | ||
+ | */ | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int main(int argc, char **argv) | ||
+ | { | ||
+ | |||
+ | FILE *in, *out; | ||
+ | int key; | ||
+ | in = fopen(" | ||
+ | out = fopen(" | ||
+ | if(argv[1] == NULL) //if program called without arguments | ||
+ | { | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | char *enciphered; | ||
+ | enciphered = (char*)malloc(sizeof(char) * 127); //memory allocate for ascii | ||
+ | int count = 0; | ||
+ | fgets(enciphered, | ||
+ | printf(" | ||
+ | while(enciphered[count] != ' | ||
+ | { | ||
+ | printf(" | ||
+ | count = count+1; | ||
+ | } | ||
+ | |||
+ | int cipher = atoi(argv[1]); | ||
+ | count = 0; | ||
+ | printf(" | ||
+ | while(enciphered[count] != ' | ||
+ | { | ||
+ | key = 1; | ||
+ | if(enciphered[count] >= 65 && enciphered[count] <= 90) //for upper case letters | ||
+ | { | ||
+ | key = enciphered[count] - cipher; | ||
+ | while(key < 65) | ||
+ | { | ||
+ | key = key + 26; //cipher shift | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | { | ||
+ | key = enciphered[count] - cipher; | ||
+ | while(key < 97) | ||
+ | { | ||
+ | key = key + 26; //cipher shift | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | { | ||
+ | key = 32; | ||
+ | } | ||
+ | printf(" | ||
+ | fprintf(out, | ||
+ | count = count+1; | ||
+ | } | ||
+ | printf(" | ||
+ | fclose(in); | ||
+ | fclose(out); | ||
+ | return (0); | ||
+ | } | ||
+ | </ | ||
+ | ====Array calculator project==== | ||
+ | Bignum is an array based calculator program. The the two values are stored as character strings and compared against an integer length array. Each aggregate function is placed within a header file and called in an if/else if loop that's nested in a while loop. The struct really serves no purpose and was an experiment at the end. | ||
+ | |||
+ | bignum.c | ||
+ | <code c> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include " | ||
+ | #include " | ||
+ | #include " | ||
+ | #include " | ||
+ | |||
+ | struct { | ||
+ | char *first; | ||
+ | char *second; | ||
+ | int length; | ||
+ | int count; | ||
+ | int div; // | ||
+ | }n; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | |||
+ | int select=1; | ||
+ | printf(" | ||
+ | printf(" | ||
+ | scanf(" | ||
+ | n.length = n.length-1; | ||
+ | n.first = (char*)malloc(sizeof(char)*n.length); | ||
+ | |||
+ | printf(" | ||
+ | |||
+ | fgetc(stdin); | ||
+ | for(n.count = n.length; n.count >= 0; -- n.count) //get first number, compare it to length | ||
+ | { | ||
+ | n.first[n.count] = fgetc(stdin); | ||
+ | n.first[n.count] = n.first[n.count] - 48; | ||
+ | } | ||
+ | |||
+ | |||
+ | | ||
+ | |||
+ | printf(" | ||
+ | |||
+ | fgetc(stdin); | ||
+ | for(n.count = n.length; n.count >= 0; -- n.count) //get second number, compare it to length | ||
+ | { | ||
+ | n.second[n.count] = fgetc(stdin); | ||
+ | n.second[n.count] = n.second[n.count] - 48; | ||
+ | } | ||
+ | |||
+ | while(select != 0 ){ // | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | scanf(" | ||
+ | if (select == 1){ | ||
+ | printf(" | ||
+ | n.first = add(n.first, | ||
+ | for (n.length; n.length> | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | printf(" | ||
+ | break;} | ||
+ | else if (select == 2){ | ||
+ | printf(" | ||
+ | n.first = sub(n.first, | ||
+ | for (n.length; n.length> | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | printf(" | ||
+ | break;} | ||
+ | else if (select == 3){ | ||
+ | printf(" | ||
+ | multiply(n.first, | ||
+ | for (n.length; n.length> | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | printf(" | ||
+ | break;} | ||
+ | else if (select == 4){ | ||
+ | printf(" | ||
+ | divide(n.first, | ||
+ | printf(" | ||
+ | printf(" | ||
+ | break;} | ||
+ | else if (select == 5){ | ||
+ | printf(" | ||
+ | break;} | ||
+ | else{ | ||
+ | printf(" | ||
+ | } | ||
+ | } | ||
+ | printf(" | ||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | add.h | ||
+ | <code c> | ||
+ | #ifndef _ADDITION_H | ||
+ | #define _ADDITION_H | ||
+ | |||
+ | char * add(char * first, char * second, int length) //function prototype | ||
+ | { | ||
+ | int count = 0; | ||
+ | char carry = 0; | ||
+ | |||
+ | for(count; count <= length; ++count) //loop until no more remaining numbers, end of length | ||
+ | { | ||
+ | int holder = (first[count] + second[count])+carry; | ||
+ | if (holder > 9) // | ||
+ | { // | ||
+ | first[count] = holder-10; | ||
+ | carry=1; | ||
+ | } // | ||
+ | else // | ||
+ | { // | ||
+ | first[count] = holder; | ||
+ | carry=0; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | return first; //return to main for printing | ||
+ | } | ||
+ | |||
+ | #endif | ||
+ | </ | ||
+ | |||
+ | sub.h | ||
+ | <code c> | ||
+ | #ifndef _SUB_H | ||
+ | #define _SUB_H | ||
+ | |||
+ | char * sub(char *first, char *second, int length) //function prototype | ||
+ | { | ||
+ | |||
+ | char count = 0; | ||
+ | |||
+ | for(count; count <= length; ++count) //loop until no more remaining numbers, end of length | ||
+ | { | ||
+ | if (first[count] < second[count]) //if the second number is greater than the first | ||
+ | { | ||
+ | first[count+1] = first[count+1] - 1; // | ||
+ | first[count] = first[count] + 10; //carry for subtraction | ||
+ | } | ||
+ | |||
+ | first[count] = first[count] - second[count]; | ||
+ | } | ||
+ | |||
+ | return first; //return to main for printing | ||
+ | } | ||
+ | #endif | ||
+ | </ | ||
+ | |||
+ | multiply.h | ||
+ | <code c> | ||
+ | #ifndef _MULTIPLY_H | ||
+ | #define _MULTIPLY_H | ||
+ | #include < | ||
+ | #include " | ||
+ | |||
+ | void multiply(char * first, char * second, int length) //function prototype | ||
+ | { | ||
+ | int count = 0; | ||
+ | int holder = 0; | ||
+ | char *third; | ||
+ | third = (char*)malloc(sizeof(char)*length); | ||
+ | |||
+ | for (count; count <= length; ++count) //loop until no more numbers | ||
+ | { | ||
+ | third[count] = first[count]; | ||
+ | } | ||
+ | for(count=0; | ||
+ | { | ||
+ | holder = (second[count]*pow(10, | ||
+ | for(holder; holder > 0; --holder) //loop until no more in holder | ||
+ | { | ||
+ | first = add(first, third, length); //add function | ||
+ | } | ||
+ | } | ||
+ | |||
+ | first = sub(first, third, length); //return to main for printing | ||
+ | |||
+ | } | ||
+ | |||
+ | #endif | ||
+ | </ | ||
+ | |||
+ | divide.h | ||
+ | <code c> | ||
+ | #ifndef _DIVIDE_H | ||
+ | #define _DIVIDE_H | ||
+ | |||
+ | void divide(char * first, char * second, int length, int *div) //function prototype | ||
+ | { | ||
+ | int count = 0; | ||
+ | char *fourth; | ||
+ | |||
+ | fourth = (char*)malloc(sizeof(char)*length); | ||
+ | |||
+ | for (count; count <= length; ++count) //loop until count matches length | ||
+ | { | ||
+ | fourth[count] = first[count]; | ||
+ | } | ||
+ | |||
+ | for(count=length; | ||
+ | { | ||
+ | while(first[count] > 0) | ||
+ | { | ||
+ | ++*div; | ||
+ | first = sub(first, second, length); //return to main for printing | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | #endif | ||
+ | </ | ||
+ | ====Thoughts and impressions==== | ||
+ | |||
+ | Overall I've been pleased with what I've explored over this semester. I'm excited to further explore concepts of C and C++ in systems programming next semester. |