Todd Edmisters fall2012 Opus
Hello there everyone. Nice to meet you all. You may, or may not know the likes of me. Either way, let me tell you a little about myself. I have been working at Corning Wegmans for the past two years of my life, nothing but love for the good ol' Wegs. Over this previous summer I went on an adventure of a lifetime. The adventure was called the Bonnaroo music and arts festival. If you love drugs, music or a good time, maybe a mixture of all three, then go down to the farm next summer and experience something that cannot be described in a few words. Then again the Tennesse summer heat might shy away a couple of you, but be not afraid it is nothing to be worried about, you won't think twice about it once you are there. Other than that my summer consisted of going to various music events and working. These events include an RHCP concert in Columbus, Vans Warped Tour and Uproar Music Festival both occuring in Syracuse. My passion for music lead me to playing bass guitar, first started off on regular guitar, about two years ago this winter. Oh how life has changed ever since started living on the low end. Anyways, enough about my life here. <3 Sincerely, the kid who wrote this introduction.
The creation of the program called size.c.
This is significant because you can determine your current character sizes, in bytes, based upon your current working console.
My code for the program below:#include<stdio.h> #include<stdlib.h> int main() { unsigned short int usi=0; unsigned int ui=0; unsigned long int uli=0; unsigned long long int ulli=0; unsigned char uc=0; // char is 8 bits (256 bits, 0-255) unsigned char carry=0; unsigned char quantity=0; quantity =uc-1; uc=quantity+1; if (uc<quantity) carry=1; else carry=0; // printing values of short int printf("unsigned short is %d bytes\n", sizeof(unsigned short int)); printf("usi low value is: %hx\n", usi); printf("usi high value is: %hx\n", (usi-1)); printf("unsigned short int unique values is: %hx%.2hx\n", carry, uc); printf(" \n"); // printing values of int printf("unsigned int is %d bytes\n", sizeof(unsigned int)); printf("ui low value is: %x\n", ui); printf("ui high value is: %x\n", (ui-1)); printf("unsigned char unique values is: %x%.2x\n", carry, uc); printf(" \n"); // printing values of long int printf("unsigned long int is %d bytes\n", sizeof(unsigned long int)); printf("uli low value is: %lx\n", uli); printf("uli high value is: %lx\n", (uli-1)); printf("unsigned char unique values is: %lxx%.2lxx\n", carry, uc); printf(" \n"); // printing values of long long int printf("unsigned long long int is %d bytes\n", sizeof(unsigned long long int)); printf("ulli low value is: %llx\n", ulli); printf("ulli high value is: %llx\n", (ulli-1)); printf("unsigned char unique values is: %llxx%.2llxx\n", carry, uc); printf(" \n"); // printing values of char printf("unsigned char is %d bytes\n", sizeof(unsigned char)); printf("uc low value is: %hhx\n", uc); printf("uc high value is: %hhx\n", (uc-1)); printf("unsigned char unique values is: %hhx%.2hhx\n", carry, uc); return(0); }
Concepts that do not make perfect sense currently are the %endings to each character.
Presenting the unique values for each character based on demand for it.
* What action or concept of significance, as related to the course, did you experience on this date?
Created a program called pointers.c
The code for this program is below:
#include<stdio.h> #include<stdlib.h> int main() { int a=12; int *b; b=&a; // changing values can lead to segmentation faults ex) b=2600 printf("a is located at 0x%X\n", &a); printf("b is located at 0x%X\n", &b); printf("b, when dereferenced, points to %d\n", *b); // dereference= a value printf("b contains 0x%X\n", b); return(0); }
* Why was this significant?
The significance of this program that is shows how the use of pointers work in use.
* What concepts are you dealing with that may not make perfect sense?
The "0x%", I don't fully understand what this is trying to accomplish.
* What challenges are you facing with respect to the course?
How exactly & work, and how to use them efficiently.
What action or concept of significance, as related to the course, did you experience on this date?
Created my own program that showed the relations to my logic choosing, based around the thought of converse implication.
My code for my logic program: (called ctruth.c)
#include <stdio.h> #include<stdlib.h> int main() { char logicor(char, char); int p = 0; int q = 0; int XNq(int p, int q); int result = 0; if ((p = 0) || (q = 0)); result = 0; if ((p = 0) || (q = 1)); result = 1; printf(" \n"); printf("Your Converse Implication Truth Table.\n"); printf(" \n"); printf("P | Q | XNq \n"); printf("_ _ _ _ _ \n"); q = 0; printf("%d | %d | %d \n", p, q, (p, q)); // all 0's q = 1; printf("%d | %d | %d \n", p, q, (q, p)); // when Q is false p = 0; printf("%d | %d | %d \n", q, p, (q, q)); // when Q is True p = 1; printf("%d | %d | %d \n", q, p, (p, 0)); // when P is False printf(" \n"); printf("Please enter the value for P: \n"); scanf("%d", &p); getchar(); printf("Please enter the value for Q: \n"); scanf("%d", &q); getchar(); putchar() printf(" \n"); if (p != q) { printf("Converse Implication Result.\n"); printf("F | T | F \n", p, q, (q, p)); printf(" \n"); } else (p + !q); { printf("Other Results.\n"); printf("T | T | T \n", p, q, (p, q)); printf("T | F | T \n", q, p, (p, q)); printf("F | F | T \n", q, p, (p, q)); } int XNq(int p, int q); return (0); }
Why was this significant?
This was significant because it was a project needed to be done for a grade.
What concepts are you dealing with that may not make perfect sense?
The concepts of printf syntax of formatting a line.
What challenges are you facing with respect to the course?
The understanding of pointers and how to use them effectively.
What action or concept of significance, as related to the course, did you experience on this date?
Learned about pre-processing directories, including the #include and #define, #ifdef, #endif.
Why was this significant?
These are used to make source programs easy to change and easy to compile in different execution environments. Directives in the source file tell the preprocessor to perform specific actions.
What concepts are you dealing with that may not make perfect sense?
Which directories to use to execute a certain function correctly.
What challenges are you facing with respect to the course?
Syntax involved with the typedef keyword.
Holds the address of another variable.
Void Pointers
The void pointer is a generic pointer type. A pointer to void can store an address to any non-function data type, and, in C is implicitly converted to any other pointer type on assignment, but it must be explicitly cast if dereferenced inline.
Dynamic Memory Allocation (Malloc/Free)
Dynamic memory allocation is the task of allocating a free chunk of memory specific to the size you predetermine in bytes, by using the malloc function. The chunk of memory is not always in the same location hence being “dynamic” instead of static. By using the “free” function, that will release the block of memory back to the system.
/* * Sample code block */ #include<stdio.h> #include<stdlib.h> struct node { char value; struct node *next; }; typedef struct node Node; int main() { Node *start, *tmp, *tmp2; // Node pointers char input, i = 0; start = tmp = tmp2 = NULL; printf("Enter a value (-1 to end): "); scanf("%hhd", &input); while (input != -1) // input does not equal -1 { if (start == NULL) { tmp = tmp2 = start = (Node *) malloc(sizeof(Node)); //allocating the size of the node start->value = input; start->next = NULL; // Starts next element is null } else { tmp->next = (Node *) malloc(sizeof(Node)); tmp->next->value = input; //tmp next value= new node(user input) tmp->next->next = NULL; //tmp next next =NULL tmp = tmp->next; } printf("Enter a value (-1 to quit): "); scanf("%hhd", &input); } tmp = start; i=0; while (tmp != NULL) //tmp does not equal NULL { printf("(%hhd)%hhd -> ",i, tmp->value); // print currrent value at node tmp = tmp->next; i = i + 1; } printf("NULL\n"); printf("Enter node # to delete: "); scanf("%hhd", &input); tmp = start; if (input != 0) { for (i = 0; i < input -1; i++) tmp = tmp->next; tmp2 = tmp -> next; tmp -> next = tmp -> next -> next; tmp2 -> next = NULL; free (tmp2); } else { start = start -> next; tmp -> next = NULL; free(tmp); } tmp=start; i=0; while (tmp != NULL) //tmp does not equal NULL { printf("(%hhd)%hhd -> ",i, tmp->value); // print currrent value at node tmp = tmp->next; i = i + 1; } printf("NULL\n"); return (0); }
Alternatively (or additionally), if you want to demonstrate something on the command-line, you can do so as follows:
lab46:~$ cd src/data/Linked_List lab46:~/src/data/Linked_List$ ls lab46:~/src/data/Linked_List$ append append.c insert insertion.c link linked.c linkedlist.h node node.c remove remove.c lab46:~/src/data/Linked_List$ ./remove Enter a value (-1 to end): 1 Enter a value (-1 to quit): 2 Enter a value (-1 to quit): 3 Enter a value (-1 to quit): 4 Enter a value (-1 to quit): 5 Enter a value (-1 to quit): -1 (0)1 -> (1)2 -> (2)3 -> (3)4 -> (4)5 -> NULL Enter node # to delete: 3 (0)1 -> (1)2 -> (2)3 -> (3)5 -> NULL lab46:~/src/data/Linked_List$
For any two propositions P and Q, if Q implies P, then P is the converse implication of Q.
It may take the following forms:
P | Q | XNq _ _ _ _ _ 0 | 0 | 0 0 | 1 | 0 1 | 0 | 1 1 | 1 | 0
List any sites, books, or sources utilized when researching information on this topic. (Remove any filler text).
Nonconjunction/Not Both … And.
The relation among the components of such a proposition, usually expressed by AND or & or ·
Left Projection
The left-shift operator causes the bit pattern in the first operand to be shifted to the left by the number of bits specified by the second operand. The value of a left-shift expression x « y is x * 2y
/* * Sample code block */ #include <stdio.h> int main() { unsigned int i; int j; int input; i = 1; printf("Enter a number to end your projection on: "); scanf("%d", &input); for(j = 0; j < input ; j++) { i = i << 1; printf("Left shift %d: %d(%X)\n", j, i); } return (0); }
Alternatively (or additionally), if you want to demonstrate something on the command-line, you can do so as follows:
lab46:~$ cd ./src/discrete lab46:~/src/discrete$ ls big bigl bigl.c bignum.c bit bitwise.c ctruth ctruth.c inc inc.c sets.c sort sort.c lab46:~/src/discrete$ ./bit Enter a number to end your projection on: // Choose any number here \\ 9 Left shift 0: 2(0) Left shift 1: 4(1) Left shift 2: 8(2) Left shift 3: 16(3) Left shift 4: 32(4) Left shift 5: 64(5) Left shift 6: 128(6) Left shift 7: 256(7) Left shift 8: 512(8)
Numbers in ( ) represent hexadecimal format, for when it becomes applicable at larger projection numbers.
What is the question you'd like to pose for experimentation? State it here.
Are there other escape sequences to meet a variance of needs, other than (\n) for a new line?
Collect information and resources (such as URLs of web resources), and comment on knowledge obtained that you think will provide useful background information to aid in performing the experiment.
Chapter 2: Types, Operators, and Expressions. The C Programming Language Book.
Based on what you've read with respect to your original posed question, what do you think will be the result of your experiment (ie an educated guess based on the facts known). This is done before actually performing the experiment.
I believe the result of my experiment go as I intend it to end, by that I mean that the proper escape sequence will do its function.
State your rationale.
My rationale for this would be that my trust in the program will run correctly, not segmentation faults or anything controversial to the situation.
How are you going to test your hypothesis? What is the structure of your experiment?
I am going to test my hypothesis by writing that program that will use some of the escape sequences. My data will show whether or not these escape sequences worked correctly or not. The structure of my program will be a line of printf statements using a certain escape sequence.
Perform your experiment, and collect/document the results here.
"The complete set of escape sequences is: " \a alert (bell) character \\ backlash \b backspace \? question mark \f formfeed \' single quote \n newline \" double quote \c carriage return \ooo octal number \t horizontal tab \xhh hexadecimal number \v vertical tab
Code for such program below:
#include <stdio.h> #include <stdlib.h> main () { int input; printf("Hello There\a -> alert (bell) character\n"); printf("Hello There\b -> backspace\n"); printf("Hello There\f -> formfeed\n"); printf("Hello There\n -> newline\n"); printf("Hello There\t -> horizontal tab\n"); printf("Hello There\v -> vertical tab\n"); printf("Hello There\\ -> backslash\n"); printf("Hello There\? -> question mark\n"); printf("Hello There\' -> single quote\n"); printf("Hello There\" -> double quote\n"); return(0); }
Results from running such code:
Hello There -> alert (bell) character Hello Ther -> backspace Hello There |-> formfeed Hello There |-> newline Hello There -> horizontal tab Hello There |-> vertical tab Hello There\ -> backslash Hello There? -> question mark Hello There' -> single quote Hello There" -> double quote
Was your hypothesis correct?
Yes, my hypothesis was correct.
Was your hypothesis not applicable?
My hypothesis was applicable, as shown above.
Is there more going on than you originally thought? (shortcomings in hypothesis)
The form-feed and the newline options produce the same result.
What shortcomings might there be in your experiment?
Not using all the escape sequences, for example \r (carriage return), this was not possible in my current experiment.
What shortcomings might there be in your data?
Not showing all the escape sequences shown in the table, as it was not possible in my current experiment.
What can you ascertain based on the experiment performed and data collected? Document your findings here; make a statement as to any discoveries you've made.
Based on the data collected I can say that escape sequences can be very useful, if used correctly, if not your statement will look awful. Discoveries made are that \f (form feed) and \n (newline) are relatively the same thing, they produce the same output. Form feed is now an outdated command, so no need to worry about its usage.
What action or concept of significance, as related to the course, did you experience on this date?
Learned about stacks and the importance of using such concept. A stack is an area of memory that holds all local variables and parameters used by any function, and remembers the order in which functions are called so that function returns occur correctly.
Why was this significant?
The significance of this is that I now know about how to use stacks, somewhat, but I still have some troubles with implementation.
What concepts are you dealing with that may not make perfect sense?
The implementation, via code, of writing a working stack program.
What challenges are you facing with respect to the course?
How to use stacks whenever needed in a situation.
* What action or concept of significance, as related to the course, did you experience on this date?
Learned about functions on this date. A function is art of source code within a larger computer program that performs a specific task and is relatively independent of the remaining code.
* Why was this significant?
This is significant because I now know how to implement the use of functions, making my code much cleaner looking.
* What concepts are you dealing with that may not make perfect sense?
The limitations that are held when using a function call.
* What challenges are you facing with respect to the course?
Passing by reference within a function.
What action or concept of significance, as related to the course, did you experience on this date?
On this day we learned about recursion. Recursion is a method where the solution to a problem depends on solutions to smaller instances of the same problem.
Why was this significant?
The significance of this is that it is another way of solving a problem and thinking about a program.
What concepts are you dealing with that may not make perfect sense?
The formatting a using a successful recursion sorting program.
What challenges are you facing with respect to the course?
Using recursion in certain situations over other ways of solving a problem.
#include<stdio.h> int itfact(int, int); // int factorial int rfact(int, int); // recursion factorial int main() { int (*fact)(int, int); int product = 1; int n = 4; fact = &rfact; // product = itfact(product, n); //INTEGER // fprintf(stdout, "%d! = %d\n", n, product); product = fact(product, n); //RECURSION fprintf(stdout, "%d! = %d\n", n, product); return (0); } int itfact(int p, int n) // Function Call { int i; for (i = n; i > 1; i--) p = p * i; return (p); } int rfact(int p, int n) // Function Call { if (n > 1) { p = p * n; p = rfact(p, (n - 1)); } return (p); }
What action or concept of significance, as related to the course, did you experience on this date?
Learned about binary trees on this day. a tree data structure in which each node has at most two child nodes, usually distinguished as “left” and “right”.
Why was this significant?
Binary trees can be used to classify and organize information. Easy way to sort information.
What concepts are you dealing with that may not make perfect sense?
How to implement a program using the idea of a binary tree.
What challenges are you facing with respect to the course?
Writing a program using the idea of recursion.
Queue Dequeuing Operation
Removes and returns the object at the beginning of the Queue (Represents a first-in, first-out collection of objects). Often referred to as a head-tail linked list.
A stack is a very important part in computer science, especially in the programming field. This could be helpful in many functions even like a simple yet probably not widely used function, reversing a string. Its simple but if we didn't use a stack, it would seem mildly difficult. A stack is like a deck of playing cards, you place one card down and continue stacking cards on top. Then when you are ready, you take the top card off and continue on down. Thats how a stack works, push stuff in and pop stuff off, LIFO (Last in, First out).
* NOT FINISHED, PARTIALLY WORKING CODE *
#include <stdio.h> #include <stdlib.h> int size(int a[]); int push(int a[]); int pop(int a[]); int a[999]; int main() { int input, num; printf("\nStack Operations\n"); printf("----------------------\n"); printf("0. Set Size\n"); printf("1. Push\n"); printf("2. Pop\n"); printf("9. Quit\n"); printf("Your Selection: "); scanf("%d", &input); while (input != 9) { if (input == 0) { size(a); } else if (input == 1) { push(a); } else if (input == 2) { pop(a); } else if (input == 9) { return (1); } printf("\nStack Operations\n"); printf("----------------------\n"); printf("0. Set Size\n"); printf("1. Push\n"); printf("2. Pop\n"); printf("9. Quit\n"); printf("Your Selection: "); scanf("%d", &input); } return (0); } int size(int a[]) { int i = 0; int size; printf("\nEnter Stack Size (0 for unlimited): "); // Sizing array scanf("%d", &size); if (size > 0) { printf("Stack Size Set to: %d\n", size); return (0); } else { printf("Please Enter Appropriate Stack Size\n"); return; } } int push(int a[]) { int i = 0, num; int size; if (i >= size) { printf("*STACK OVERFLOW*\n"); return; } else { if (i <= size) { printf("Enter Value To Push Onto Stack: "); scanf("%d", &num); a[i++] = num; printf("A %hhd Has Been Pushed Onto The Stack\n", num); } } } int pop(int a[]) { int i = 0, num; int size; if (i > 0) { printf("A %hhd Has Been Popped Off Of The Stack\n", a[i]); i--; } else { printf("*STACK UNDERFLOW*\n"); } }
Intersection
Two sets A and B is the set that contains all elements of A that also belong to B (or equivalently, all elements of B that also belong to A), but no other elements.
Example:
The intersection of the sets {1, 2, 3} and {2, 3, 4} is {2, 3}.
cartesian product
Cartesian Product is making one set that takes one from a set and pairs it with one value from another set. This is done for each of the values in each set. Example:
lab46:~$ ./cartesian Enter Array Size For Array A: 3 Enter Array A Elements: 1 2 4 Enter Array Size For Array B: 3 Enter Array B Elements: 5 7 9 Cartesian Product Of Array A & B: A = {1,2,4} & B = {5,7,9} A x B = {1,2} x {2,7} x {4,9} = {(1,5), (1,7), (1,9), (2,5), (2,7), (2,9), (4,5), (4,7), (4,9)} B x A = {4,9} x {2,7} x {1,2} = {(4,9), (4,7), (4,5), (2,9), (2,7), (2,5), (1,9), (1,7), (1,5)}
Is it possible to set person options for your own terminal?
What I hope will happen is that if there is such a way to edit person option within my terminal then I will be able to create personal changes to commands.
I will be testing my hypothesis by running the stty command in order to change some options within my terminal. The structure of my experiment is a command and hope it works.
stty [-a] [-g] [modes]
-a: Write to standard output all of the option set tings for the terminal.
-g: Report current settings in a form that can be used as an argument to another stty command. Emits termios-type output if the underlying driver sup ports it; otherwise, it emits termio-type output.
lab46:~$ stty -a speed 38400 baud; rows 24; columns 80; line = 0; intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^ werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0; -parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts -ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixo -iuclc -ixany -imaxbel -iutf8 opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke
* Now for the change * LOOK AT ERASE FUNCTION
lab46:~$ stty erase \^h lab46:~$ stty -a speed 38400 baud; rows 45; columns 76; line = 0; intr = ^C; quit = ^\; erase = ^H; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0; -parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts -ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8 opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0 isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke
Was your hypothesis correct?
My hypothesis was correct.
Was your hypothesis not applicable?
My hypothesis was applicable.
Is there more going on than you originally thought? (shortcomings in hypothesis)
No shortcomings in hypothesis, or more going on than I originally thought. Ran, and completed as I thought it would happen.
What shortcomings might there be in your experiment?
Only changed one instance in my options. Didn't want to mess up a whole lot and regret it.
What shortcomings might there be in your data?
Not enough changes happen for sufficient data.
This command can be very useful if you do not like the standard settings being used. You can change them to whatever feels more comfortable to you.
Learned about shared and static object libraries on this day. My Notes:
libraries: shared object && static object libraries shared: .so static: .a shared obj: MOST COMMONLY USED to update, no need to update every single file write code that uses functions that uses the library information. ldd shows that shared object file that ls relies upon just have what you wrote, with refrences to lib. much smaller in code, since copying lib pro: resulting file size = smaller cons: if lib dissappears the functions won't work. unpredictable behavior since you lost data points static obj: EASIER TO USE multiple functions in one library. need to change all files to update.
The significance of this knowledge is that knowing the difference of what library you are about to create will change the ease-ability you are about to experience.
How to implement which library to use.
BINARY TREES
Re-learned about gd drawings, how to implement them, compile, and show via web browser.
This knowledge will help me out with the eoce drawings that I will be needing to do.
Making the drawing to certain criteria.
Certain math needing to create a certain path in a line.
Downloaded the latest version of Perl, 5.16.2, to my lab46 account. P.S. it takes a very long time, I do not recommend it.
This is significant because it gives me the ability to use the latest version of Perl, and its latest libraries to use.
Certain implementation of libraries within the new Perl version.
Still making headway with the Perl language, aspects of the language are still unknown to me.
We talked about queues and such on this day, was a lovely subject to talk about.
This is significant because this not only helped with the binary tree project, but, as opposed to stacks it is another way for inputing values in an organized fashion.
Implementation of these sorting methods in various languages.
Recursion and iteration via stacks and or queues.
Function Pointer
Instead of referring to data values, a function pointer points to executable code within memory. When dereferenced, a function pointer can be used to invoke the function it points to and pass it arguments just like a normal function call.
Recursive tree traversal
A tree traversal is the process of examining each node exactly once in a systematic way. Different traversals are classified and named by the process in which they examine the tree. A recursive tree traversal examines the tree recursively which is the process of calling a function within itself. One example of recursion is two mirrors faced towards each other creating an infinite recursion.
Recursive tree transversal
A form of going through a Tree via recursion. Recusion is calling the function while still inside the function before finishing the original function.
lab46:~$ Please Enter Values For Tree Traversal (-1 to quit): Enter Value: 2 Enter Value: 5 Enter Value: 7 Enter Value: 1 Enter Value: 8 Enter Value: 4 Enter Value: 0 Enter Value: 6 Enter Value: -1 lab46:~$ Your Numbers: 2,5,7,1,8,4,0,6 2 / \ 1 5 / / \ 0 4 7 / \ 6 8
Permutation
A way, esp. one of several possible variations, in which a set or number of things can be ordered or arranged
De Morgan's Law
In propositional calculus form: where: *¬ is the negation operator (NOT) *∧ is the conjunction operator (AND) *∨ is the disjunction operator (OR) *⇔ means logically equivalent (if and only if).
Permutation
A way, esp. one of several possible variations, in which a set or number of things can be ordered or arranged
lab46:~$ Enter First Number For Permutation: 3 Enter Second Number For Permutation: 1 Permutation Results: 3! / 1! = 5 (3,1), (2,1), (1,1), (1,3), (1,2)
Since I had a gain of sudden interest of the Perl language, I was wondering to myself if I could re-write a program, primarily the stack program that was due, using this as a great learning experiment to introduce myself to the Perl language.
The result of my experiment will be a working stack program, just written in Perl. In doing so you can push & pop numbers from a list in any fashion you so choose. But you must set a certain size to the stack unless the program will not run as it is intended, plus you can display your stack at any point in time.
Compile and run the program to see if it works.
Structure:
Stack Program Version 1.0
$top = -1; do { print "\nStack Operations\n"; print "----------------\n"; print "0. Display Stack\n"; print "1. Set Size\n"; print "2. Push\n"; print "3. Pop\n"; print "9. Quit\n"; print "Your Selection: "; chomp($input = <STDIN>); if ($input eq "0") #Display Stack { if ($top == -1) { print "Stack Has Yet To Be Created\n"; } else { print "Your Stack: {"; print "@value "; print "\b\b} \n"; } } if ($input eq "1") #Set Size { print "Set Size To Stack: "; chomp($size = <STDIN>); } if ($input eq "2") #Push { if ($top == $size - 1) { print "STACK OVERFLOW\n"; } else { $top++; print "Enter Value To Push Onto Stack: "; $value = <STDIN>; chomp($value); push(@value, $value); print $value." Has Been Pushed Onto The Stack\n"; } } if ($input eq "3") #Pop { if ($top == -1) { print "STACK UNDERFLOW\n"; } else { $top--; $value = pop(@value); print $value." Has Been Popped Off Of The Stack\n"; } } } while ($input ne "9");
Stack Program Version 1.1 (Using Sub Function Calls):
$top = -1; sub display { print "Your Stack: {"; print "@value "; print "\b\b} \n"; } sub push { $top++; print "Enter Value To Push Onto Stack: "; $value = <STDIN>; chomp($value); push(@value, $value); print $value." Has Been Pushed Onto The Stack\n"; } sub pop { $top--; $value = pop(@value); print $value." Has Been Popped Off Of The Stack\n"; } do { print "\nStack Operations\n"; print "----------------\n"; print "0. Display Stack\n"; print "1. Set Size\n"; print "2. Push\n"; print "3. Pop\n"; print "9. Quit\n"; print "Your Selection: "; chomp($input = <STDIN>); if ($input eq "0") #Display Stack { if ($top == -1) { print "Stack Has Yet To Be Created\n"; } else { &display(); } } if ($input eq "1") #Set Size { print "Set Size To Stack: "; chomp($size = <STDIN>); } if ($input eq "2") #Push { if ($top == $size - 1) { print "STACK OVERFLOW\n"; } else else { &push(); } } if ($input eq "3") #Pop { if ($top == -1) { print "STACK UNDERFLOW\n"; } else { &pop(); } } } while ($input ne "9");
Possible Results:
lab46:~/perl/stack$ perl stacks.pl Stack Operations ---------------- 0. Display Stack 1. Set Size 2. Push 3. Pop 9. Quit Your Selection: 1 Set Size To Stack: 3 Stack Operations ---------------- 0. Display Stack 1. Set Size 2. Push 3. Pop 9. Quit Your Selection: 2 Enter Value To Push Onto Stack: 4 4 Has Been Pushed Onto The Stack Stack Operations ---------------- 0. Display Stack 1. Set Size 2. Push 3. Pop 9. Quit Your Selection: 2 Enter Value To Push Onto Stack: 8 8 Has Been Pushed Onto The Stack Stack Operations ---------------- 0. Display Stack 1. Set Size 2. Push 3. Pop 9. Quit Your Selection: 2 Enter Value To Push Onto Stack: 1 1 Has Been Pushed Onto The Stack Stack Operations ---------------- 0. Display Stack 1. Set Size 2. Push 3. Pop 9. Quit Your Selection: 2 STACK OVERFLOW Stack Operations ---------------- 0. Display Stack 1. Set Size 2. Push 3. Pop 9. Quit Your Selection: 0 Your Stack: {4 8 1} Stack Operations ---------------- 0. Display Stack 1. Set Size 2. Push 3. Pop 9. Quit Your Selection: 3 1 Has Been Popped Off Of The Stack Stack Operations ---------------- 0. Display Stack 1. Set Size 2. Push 3. Pop 9. Quit Your Selection: 3 8 Has Been Popped Off Of The Stack Stack Operations ---------------- 0. Display Stack 1. Set Size 2. Push 3. Pop 9. Quit Your Selection: 0 Your Stack: {4} Stack Operations ---------------- 0. Display Stack 1. Set Size 2. Push 3. Pop 9. Quit Your Selection: 3 4 Has Been Popped Off Of The Stack Stack Operations ---------------- 0. Display Stack 1. Set Size 2. Push 3. Pop 9. Quit Your Selection: 3 STACK UNDERFLOW Stack Operations ---------------- 0. Display Stack 1. Set Size 2. Push 3. Pop 9. Quit Your Selection:9
Based on the data collected:
Yes, my hypothesis was correct.
My hypothesis was applicable.
Since I am new to the language there is always something that I fully don't understand or something that could be done in a different manner.
Not enough commenting in the program for a person who has not seen this language before so they can understand what is going on and why.
The data size could be larger to prove that there will not be any problems when I go this program goes into larger data sets.
Based on this experiment, I have gained a lot of knowledge, based on before I started, to this language. This knowledge is going to be quite helpful if I ever move on and use language for various other projects and experiments at hand.