User Tools

Site Tools


opus:fall2011:mshort3:part2

Part 2

Entries

October 07, 2011

<html> <p>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.</p> </html>

October 14, 2011

<html> 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.

<pre> A little demonstration:

 Enqueue - put stuff on it
+- - - -+
|1|2|3|4|
      +- - - -+
             Dequeue - get stuff off it

</pre> </html> 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

<html> <p>Today we went over File i/o.</p>

<p>Files: Theory and Practice Also makes a good data structure topic</p> <p>Philosophy/Quote: “Everything is a file”</p> <p>3 Types of files</p> <ul style=“list-style-type: none;”> <li>Regular - Pretty much most things that we interact with on a daily basis</li> <li>Directory - A file that holds/has information of other files, it doesn't actually have any data itself</li> <li>Special - hardware such as harddrive and pipes and sockets on the software side. Also anything else that is not of the other 2</li> </ul> <p>Fields and records</p> <p>“Column and Rows”</p> <p>File Access/Operations</p> <ul style=“list-style-type: none;”> <li>Open</li> <li>Create</li> <li>Close</li> <ul style=“list-style-type: none;”> <li>read</li> <li>Write (>)</li> <li>Append (») - Moves the file pointer to the very end of the file</li> </ul> </ul> <pre> 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. ————————————————————— </pre> </html> <file c> Example Program

#include <stdio.h>
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);
}

</file>

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 <stdio.h>
#include <stdlib.h>
 
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);
}

<html> <pre> Output:


Pointer: -2077239964 Pointer + 1: -2077239960 Pointer + 2: -2077239956 Pointer - 1: -2077239968 </pre> </html>

Void pointers

A void pointer is known as generic pointer, which can refer to variables of any data type.

#include <stdio.h>
 
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

<html> <pre> 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. </pre> </html>

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 <stdio.h>
 
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;
}

<html> <pre> Output:


2 </pre> </html>

Null pointers

These little guys point to a void in the space time continuum

#include <string.h>
#include <stdio.h>
//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;
}

<html> <pre> Output:


Segmentation fault </pre> </html>

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 <stdio.h>
#include <stdlib.h>
        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

<html> <body>

<a href="http://jquery.com/">JQuery API</a>

</body> </html>

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

<head>
<title>Michael Short</title>
</head>
 
<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
<script type="text/javascript">
	$(document).ready(function()
	{
		$("#create").click(draw);
		$("#destroy").click(clear);
	});
 
	function draw()
	{
		var canvas = document.getElementById("canvas");
		var ctx = canvas.getContext("2d");
 
			for(i = 0; i <= 100; i++)
			{
				ctx.fillStyle = "rgb(" + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + ")";
 
        		ctx.strokeStyle = "rgb(" + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + ")";
 
        		ctx.lineWidth = "2";
 
        		var x = Math.floor(Math.random() * 500);
        		var y = Math.floor(Math.random() * 500);
        		var radius = 0.1 + Math.floor(Math.random() * 10);
 
        		ctx.beginPath();
        		ctx.arc(x, y, radius, 0, Math.PI * 2, true);
 
        		ctx.stroke();
        		ctx.fill();
    		}
	}
 
	function clear()
	{
		var ctx = canvas.getContext("2d");
		ctx.clearRect(0, 0, 500, 500);
	}
 
</script>
 
<body>
 
<canvas id="canvas" width="500" height="500" style="border: 3px solid red"></canvas>
 
<button id="create">Draw</button>
<button id="destroy">Clear</button>
 
</body>
</html>

Data

<html> <body>

<a href="http://lab46.corning-cc.edu/~mshort3/jCircles/Lab10.html" target="_blank">Click here for the funz</a>Also, it will not work in IE

</body> </html>

Analysis

Based on the data collected:

  • was your hypothesis correct?
    • It was, everything worked out just fine and amazingly i might add :D
  • is there more going on than you originally thought? (shortcomings in hypothesis)
    • There is, for one you can do 3D shapes… now that might be an experiment for later on. And you cant just do/add things to the canvas without first getting the content.
  • what shortcomings might there be in your experiment?
    • Besides failing and not getting anything to work, there was the whole thing with making circles… rectangles/squares were a little too easy… and also only drawing one at a time. Making them circles and adding the functionality of choosing how many drawn at once was adding a nice depth to this experiment.
  • what shortcomings might there be in your data?
    • It's not fun…

Conclusions

With canvas you can draw more than just circles, for example: rectangles, triangles, and smiley faces! And most likely many moar.

HTML 5 Drawing FUN

Question

Can I… nay… Am I ready to take on some of HTML 5's new features… I say… YES!!! One new thing I'd like to see if I can do is draw using their new SVG tags or (Scalable Vector Graphics).

Resources

<html> <p>Before you can go any further you will need either one of two things:</p> <ul style=“list-style-type: none;”>

<li>Most recent version of Firefox (which, why wouldn't you?)</li>
<li>Or you may have to go into the about:config page and enable HTML5 support (with the newest version you shouldn't have to do this though)</li>

</ul> <p>The Links!!! They be down below, arrrgghhh</p> <a href=“http://www.tutorialspoint.com/html5/html5_svg.htm” target=“_blank”>TutsPoints</a> </html>

Hypothesis

With the growing support of the new HTML standard more and more browsers are starting to have support for the new HTML5 KING!!! I would like to perform one of its new features and see how it works and display browser compatibility.

Experiment

<!DOCTYPE html>
<head>
<title>Some cool and new HTML5 stuff!!!</title>
</head>
<body>
 
<svg id="svg1" height="200" xmlns="http://www.w3.org/2000/svg">
	<rect id="redrect" width="300" height="100" fill="black" /> <!-- Draws the rectangle -->
	<circle id="redcircle" cx="50" cy="50" r="50" fill="red" /> <!-- Draws the circle -->
	<circle id="redcircle" cx="250" cy="50" r="50" fill="red" /> <!-- Draws the circle -->
	<text x="75" y="55" stroke="purple">Look, you can also put text in real easily!</text>
</svg>
 
</body>
</html>

Data

<html> <p>Follow the link below to behold the greatness!</p> <a href=“http://lab46.corning-cc.edu/~mshort3/html5/” target=“_blank”>Click Here</a>

<p>Browsers that support it… or at least have some support</p> <ul style=“list-style-type: none;”>

<li>Yes</li>
  <ul style="list-style-type: none;">
    <li>Firefox</li>
    <li>Chrome</li>
    <li>Internet Explorer 9</li>
  </ul>
<li>No</li>
  <ul style="list-style-type: none;">
    <li>Opera (Although a newer version perhaps does support it... I don't use it enough (does anyone?)</li>
  </ul>
  <li>And im not sure on Safari... I dont have a mac...</li>

</ul> </html>

Analysis

Based on the data collected:

  • was your hypothesis correct?
    • Yes it was my dear watson
  • is there more going on than you originally thought? (shortcomings in hypothesis)
    • I only performed a few of the basic ideas, as you will see with the link you can do so much more than I demonstrated. Looks like making logos and the like in photo shop/illustrator will become a thing of the past?
  • what shortcomings might there be in your experiment?
    • Browser support is a big downfall on all things web tech… I seriously hate how I have to write the code 50 different ways so old and 50 different browsers will know how to interact with the page properlly
  • what shortcomings might there be in your data?
    • Took me a little bit at first as to why it wasnt working in firefox… then i looked at my version and it was 3.6… super outdated. So kids, keep things up-to-date

Conclusions

There is plenty of room here to grow. Someone could make their own custom images. And with the promise of HTML5 to grow and expand over time I see the SVG and Canvas attributes becoming very popular.

opus/fall2011/mshort3/part2.txt · Last modified: 2011/10/30 15:48 by mshort3