Well we spent a little time going over the concept of queues and talked a little bit about stacks again. Other than that I showed up an hour early due to forgetting what time I was supposed to be there... oh well haha. Anyway, I spent some of my time working on one of the experiments for my opus and got some more work done on that one. Other than that it was a pretty laid back day and we did work.
====October 14, 2011====
We went over queues again and we were all like, they're sideways staxs, yo. They follow the FIFO ruling, which is first in, first out. And a nice example he gave us was planting a seed in the ground and watching it grow.
A little demonstration:
Enqueue - put stuff on it
+- - - -+
|1|2|3|4|
+- - - -+
Dequeue - get stuff off it
And we also discussed a real life scenario for LIFO (Last in, first out) and it would be a pezz dispenser
And now for a little starter code
Node **dequeue(list *);
void enqueue(list *, Node *);
====October 21, 2011====
Today we went over File i/o.
Files: Theory and Practice //Also makes a good data structure topic
Philosophy/Quote: "Everything is a file"
3 Types of files
Regular - Pretty much most things that we interact with on a daily basis
Directory - A file that holds/has information of other files, it doesn't actually have any data itself
Special - hardware such as harddrive and pipes and sockets on the software side. Also anything else that is not of the other 2
Fields and records
"Column and Rows"
File Access/Operations
Open
Create
Close
read
Write (>)
Append (>>) - Moves the file pointer to the very end of the file
Some extra goodies:
---------------------------------------------------------------
r //Open text file for reading. The stream is positioned at the beginning of the file.
+r //Open for reading and writing. The stream is positioned at the beginning of the file.
w //Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.
+w //Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.
a //Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.
+a //Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.
---------------------------------------------------------------
//Example Program
#include
int main()
{
FILE *fPointer; //Declare file pointer
//fPointer = fopen("filename", "r"); //Opens the file, if there is a porblem it returns NULL, Check if the
pointer is NULL, if so, DONT PROCEED
fPointer = fopen("/etc/motd","r");
char value;
while((value = fgetc(fPointer)) != EOF)
{
printf("%c", value);
}
fclose(fPointer);
return(0);
}
====October 28, 2011====
I wasn't able to attend this class on this date due to some rather unforseen circumstances... There was a sudden death in the family and I was needed by my family.
But upon emailing the Great One, if there were to be a lecture today it would have been about Trees! And i thought I was only in one bio class this semester haha but anyway, here is what I know...
In a programming paradigm, unlike lists which use next and previous the common place formatting is to use right and left. The "bark" of a tree is to sort, preferably numbers. The lowest number on the left with the highest on the right as far down as it can go.
A little pseudo code example:
----------------------
if(value > current -> value)
current = current -> right;
else
current = current -> left;
=====Terminology=====
====arrays, pointer arithmetic====
The C language allows us to perform integer addition or subtraction operations on pointers. If pointer1 points to an integer, pointer1 + 1 is the address of the next integer in memory after pointer. pointer - 1 is the address of the previous integer before pointer.
#include
#include
int main()
{
int value = 10;
int *pointer = &value;
printf("Pointer: %d\n", pointer);
printf("Pointer + 1: %d\n", pointer + 1);
printf("Pointer + 2: %d\n", pointer + 2);
printf("Pointer - 1: %d\n", pointer - 1);
}
====Void pointers====
A void pointer is known as generic pointer, which can refer to variables of any data type.
#include
void use_int(void *);
void use_float(void *);
void greeting(void (*)(void *), void *);
int main(void) {
char ans;
int i_age = 22;
float f_age = 22.0;
void *p;
printf("Use int (i) or float (f)? ");
scanf("%c", &ans);
if (ans == 'i') {
p = &i_age;
greeting(use_int, p);
}
else {
p = &f_age;
greeting(use_float, p);
}
return 0;
}
void greeting(void (*fp)(void *), void *q) {
fp(q);
}
void use_int(void *r) {
int a;
a = * (int *) r;
printf("As an integer, you are %d years old.\n", a);
}
void use_float(void *s) {
float *b;
b = (float *) s;
printf("As a float, you are %f years old.\n", *b);
}
//Found online and thought it was a really good example
Output:
-----------------
Use int (i) or float (f)? i
As an integer, you are 22 years old.
Use int (i) or float (f)? f
As a float, you are 22.000000 years old.
====Function pointers====
A function pointer is a variable that stores the address of a function that can later be called through that function pointer. This is useful because functions encapsulates code so you dont have to keep re-writing it.
simply put, a pointer that points to a function.
#include
void myFunction(int x)
{
printf("%d\n", x);
}
int main()
{
void (*test)(int);
test = &myFunction;
test(2); //Or type (*test)(2) that will work also
return 0;
}
Output:
-----------------
2
====Null pointers====
These little guys point to a void in the space time continuum
#include
#include
//I think there might be an include that defines null... im not sure and if not then i guess you could make one?
//The reason is because the output is a seg fault
int main()
{
char *string = NULL;
strcpy(string, "Test");
printf("%s", string);
return 0;
}
Output:
---------------
Segmentation fault
====Memory De-allocation (free(), delete)====
Free deletes the space of memory
//Taken from my doubly linked list star wars themed program
if(force == yoda)
{
chewbacca = (sith_lord*)malloc(sizeof(sith_lord));
force -> anakin = chewbacca;
chewbacca -> anakin = NULL;
chewbacca -> darth_vader = force;
chewbacca -> lightsaber = jedi_rank;
yoda = chewbacca;
free(chewbacca);
}
====Pushing====
"First in last out" Puts something on top of the stack, also a singly linked list
void push(Stack *pointer, int value)
{
Some stuff goes here
}
====Top [not finished]====
Top of the stack
void push(stuff)
{
struct stack_test *temp;
temp = (struct stack_test *)malloc(sizeof(struct stack_test));
temp -> data = x;
temp -> next = top;
top = temp;
}
====LIFO or FIFO [not finished]====
Last in First Out - This is how stacks work. FIFO (beinf First in First out) I believe is a queue
====Overflow condition (where applicable?) [not finished]====
====Underflow condition [not finished]====
====Enqueuing====
Adds elements to a queue
myQueue.enqueue("I am adding stuff to my queue")
====Dequeuing===
Removes an element from a queue
myQueue.dequeue(some_stuff)
=====data Objective=====
====Star Wars Themed Doubley Linked List Goodness====
I will be taking the Singly Linked List and making it a Doubly Linked List... but wait... there's moar! It will be star wars themed! Oh, yeah!
===Method===
Did the amount of brain pain equal out to the feeling of accomplishment... I tell you I had to take some advil with the massive headaches the ensued...
===Measurement===
Writing it from scratch and constantly having to look back at things it took me about 5 hours in all to write this code... very time consuming and rather difficult... But I do feel like I did very well and learned quite a few things. I can look at the code and interact with it but I still need to look back at past examples of codes in order to write it out. But thats not a bad thing persay.
===Analysis===
Reflect upon your results of the measurement to ascertain your achievement of the particular course objective.
* How did you do?
* I did give myself a small round of applause and then promptly lapsed into a brain hemorrhaging coma...
* Room for improvement?
* Yes! lots! for one I need to get to the point where I can just write it wihout looking at my past work for references.
* Less procedural
* It is just themed... some of the things could make moar sense
* Could the measurement process be enhanced to be more effective?
* If it didn't take so long to write the code... I mean the guy could have made it easier -_-#
* Do you think this enhancement would be efficient to employ?
* It would make writing code especially when it seems to be rather high level stuff much easier and easier to learn. Like How I wrote it in Java as well. The code (class) does not show all the things happening in the background, but there is where a teacher can hop in and explain all of that. That as long as you understand the concept and how things are actually working then I do believe further simplifing things would make it easier.
* Could the course objective be altered to be more applicable? How would you alter it?
* I wouldn't alter the objective at all. It is accomplishable and it does teach you a lot. It will just take some time is all.
===Code===
#include
#include
struct sith_lord
{
int lightsaber;
struct sith_lord *darth_vader; //Pointer to the next Node in the list
struct sith_lord *anakin; //Pointer to the previous Node in the list
};
typedef struct sith_lord sith_lord; //just say jedi_council instead od struct jedi_council
int main()
{
sith_lord *yoda, *force, *chewbacca; //start, current, temp //declaring three pointers for me to use of this struct type
int jedi_rank;
int i = 0;
yoda = force = chewbacca = NULL; //making them all equal to NUL
printf("Enter your rank (-1 to exit): ");
scanf("%d", &jedi_rank);
while(jedi_rank != -1)
{
if(jedi_rank == -1)
break;
if(yoda == NULL)
{
yoda = (sith_lord*)malloc(sizeof(sith_lord));
yoda -> darth_vader = NULL;
yoda -> anakin = NULL;
yoda -> lightsaber = jedi_rank;
force = yoda;
}
else
{
force -> darth_vader = (sith_lord*)malloc(sizeof(sith_lord));
force -> darth_vader -> anakin = force;
force = force -> darth_vader;
force -> lightsaber = jedi_rank;
force -> darth_vader = NULL;
}
printf("Enter your rank (-1 to exit): ");
scanf("%d", &jedi_rank);
}
force = yoda;
printf("Going Forwards\n");
while(force != NULL)
{
printf("[%d] rank = %d\n", i, force -> lightsaber);
if ( force -> darth_vader != NULL )
force = force -> darth_vader;
else
break;
i++;
}
printf("Going backwards\n");
while(force != NULL)
{
printf("[%d] rank = %d\n", i, force -> lightsaber);
force = force -> anakin;
i--;
}
//inserting a node into the struct
printf("Enter a level to insert before: ");
scanf("%d", &jedi_rank);
force = yoda;
for(i = 0; i < jedi_rank; i++)
{
force = force -> darth_vader;
}
printf("Enter a new rank: ");
scanf("%d", &jedi_rank);
if(force == yoda)
{
chewbacca = (sith_lord*)malloc(sizeof(sith_lord));
force -> anakin = chewbacca;
chewbacca -> anakin = NULL;
chewbacca -> darth_vader = force;
chewbacca -> lightsaber = jedi_rank;
yoda = chewbacca;
//free(chewbacca);
}
else
{
chewbacca = (sith_lord*)malloc(sizeof(sith_lord));
chewbacca -> darth_vader = force;
chewbacca -> anakin = force -> anakin;
force -> anakin -> darth_vader = chewbacca;
force -> anakin = chewbacca;
chewbacca -> lightsaber = jedi_rank;
/*
force -> anakin -> darth_vader = chewbacca;
chewbacca -> darth_vader = force;
chewbacca -> anakin = force -> anakin;
force -> anakin = chewbacca;
chewbacca -> lightsaber = jedi_rank;
*/
//free(chewbacca);
}
force = yoda;
i = 0;
printf("Going Forwards\n");
while(force != NULL)
{
printf("[%d] rank = %d\n", i, force -> lightsaber);
if ( force -> darth_vader != NULL )
force = force -> darth_vader;
else
break;
i++;
}
printf("Going backwards\n");
while(force != NULL)
{
printf("[%d] rank = %d\n", i, force -> lightsaber);
force = force -> anakin;
i--;
}
//Appending
printf("Enter the level to apend to: ");
scanf("%d", &jedi_rank);
force = yoda;
for(i = 0; i < jedi_rank; i++)
{
force = force -> darth_vader;
}
printf("Enter a new rank: ");
scanf("%d", &jedi_rank);
if(force -> darth_vader == NULL)
{
chewbacca = (sith_lord*)malloc(sizeof(sith_lord)); //give birth to chewbacca
force -> darth_vader = chewbacca;
chewbacca -> darth_vader = NULL;
chewbacca -> anakin = force;
chewbacca -> lightsaber = jedi_rank;
//free(chewbacca); //i want to free chewbacca... he deserves it... how can he escape?
}
else
{
chewbacca = (sith_lord*)malloc(sizeof(sith_lord));
chewbacca -> darth_vader = force -> darth_vader;
force -> darth_vader = chewbacca;
chewbacca -> darth_vader -> anakin = chewbacca;
chewbacca -> anakin = force;
chewbacca -> lightsaber = jedi_rank;
}
force = yoda;
i = 0;
printf("Going Forwards\n");
while(force != NULL)
{
printf("[%d] rank = %d\n", i, force -> lightsaber);
if ( force -> darth_vader != NULL )
force = force -> darth_vader;
else
break;
i++;
}
printf("Going backwards\n");
while(force != NULL)
{
printf("[%d] rank = %d\n", i, force -> lightsaber);
force = force -> anakin;
i--;
}
//Deleting
printf("What rank would you like to delete: ");
scanf("%d", &jedi_rank);
force = yoda;
for(i = 0; i < jedi_rank; i++)
{
force = force -> darth_vader;
}
if(force == yoda)
{
yoda = yoda -> darth_vader;
force -> darth_vader = NULL;
yoda -> anakin = NULL;
free(force);
}
else if ( force -> darth_vader == NULL )
{
force -> anakin -> darth_vader = NULL;
force -> anakin = NULL;
free(force);
}
else
{
force -> darth_vader -> anakin = force -> anakin;
force -> anakin -> darth_vader = force -> darth_vader;
free(force);
}
force = yoda;
i = 0;
printf("Going Forwards\n");
while(force != NULL)
{
printf("[%d] rank = %d\n", i, force -> lightsaber);
if ( force -> darth_vader != NULL )
force = force -> darth_vader;
else
break;
i++;
}
printf("Going backwards\n");
while(force != NULL)
{
printf("[%d] rank = %d\n", i, force -> lightsaber);
force = force -> anakin;
i--;
}
}
=====Experiments=====
====JQuery Random Circles====
===Question===
Using the JQuery library is it possible to create a random amount of shapes (circles in this case) with a random set of color fill and color edging?
===Resources===
JQuery API
===Hypothesis===
I want to do something cool and challenging with JQuery, I believe this to be possible by attempting and hopefully being able to implement a way to draw random shapes (circles, im restricting myself so I dont go too overboard and have my brains blow up). Using canvas and its methods I will be able to draw things... at a click of a button!
===Experiment===
Michael Short