failing like never before

10Feb/091

Weekly Biking

Distance Traveled: 8.38 Miles

Max Speed: 29.2 MPH

Average Speed: 14.4 MPH

Time Traveled: thirty five minutes

Tagged as: , , 1 Comment
9Feb/090

A Simple Weird Sorter

This is a little toy program written for one of my CS classes. The program takes a positive integer N as an argument and reads from stdin until it gets an EOF. The program then sorts the input using qsort, while assuming that each element to be supported is N bytes long.

Fun facts: the output from this program looks kinda cool if you do "./program_name 1 < index.html" where index.html is some large webpage.

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#define MEM_SIZE 100

// for the sake of ease, token length and input array are global
unsigned long global_token;
char * in_ptr;

// comparator for qsort,
// returns 1 if a is bigger, -1 is smaller, 0 if equal
int comparator(const void  * a, const void * b)
{
  unsigned int i;
  int r_value = 0;
  for(i = 0; i < global_token; ++i)
  {
    if( *(i + (char*)a) < *(i + (char*)b))
      return -1;
    else if ( *(i + (char*)a) > *(i + (char*)b))
      return 1;
  }
  return 0;
}

// get input from stdin and save it to dynmaically allocated memory
unsigned long get_input()
{
  char* temp_in_ptr;
  unsigned long ar_size, n_ar_size;
  unsigned long max_size = MEM_SIZE;
  for(ar_size = 0; !feof(stdin); ++ar_size) // loop until EOF
  {
    if(max_size == (1 + ar_size)) // check if more memory is needed
    {
      temp_in_ptr =(char *)realloc(in_ptr,
                  (size_t)(MEM_SIZE + max_size));
      if(!temp_in_ptr) // returns error and exits if realloc fails
      {
        fprintf(stderr, "Error in allocating memory for input.");
        fprintf(stderr, "Input data may be too large.\n");
        free(in_ptr); // free dynamically allocated memory
        exit(1);
      }
      in_ptr = temp_in_ptr;
      max_size += MEM_SIZE;
    }
    *(in_ptr + ar_size) = fgetc(stdin); // get next char from input
    if(ferror(stdin)) // check for read error
    {
      fprintf(stderr, "Error encountered in reading input.\n");
      free(in_ptr); // free dynamically allocated memory
      exit(1);
    }
  }
  ar_size --; // get rid of EOF char
  // if string was not multiple of token length, need to make it
  // a multiple of token length and fill end with '\0'
  if(ar_size % global_token != 0)
  {
    n_ar_size =
       (global_token * (unsigned int)(ar_size / global_token + 1));
    if(n_ar_size > max_size) // check if more memory is needed
    {
      temp_in_ptr = (char *)realloc(in_ptr, (size_t)n_ar_size);
      if(!temp_in_ptr) // check for error in mem allocation
      {
          fprintf(stderr, "Error in allocating memory for input.");
          fprintf(stderr, "Input data may be too large.\n");
          free(in_ptr); // free dynamically allocated memory
          exit(1);
      }
      in_ptr = temp_in_ptr;
    }
    for(; ar_size != n_ar_size; ++ar_size) // fill with '\0'
    {
      *(ar_size + in_ptr) = '\0';
    }
  }
  return ar_size;
}  

int main(int argc, char** argv)
{
  unsigned long ar_size, i;
  in_ptr = (char *)malloc(MEM_SIZE * sizeof(char));

  if (argc < 2)
  {
    fprintf(stderr, "Must provide at least one argument.\n");
    free(in_ptr); // free dynamically allocated memory
    exit(1);
  }
  // use strtoul() to convert char to long int
  // and use errno to check for error in conversion
  errno = 0;
  global_token = strtoul(*(argv + 1), NULL, 0);
  if(!global_token || errno == ERANGE)
  {
    fprintf(stderr, "Argument must be a positive integer.\n");
    free(in_ptr); // free dynamically allocated memory
    exit(1);
  }    

  ar_size = get_input(); // get data from stdin
  // sort with qsort
  qsort(in_ptr, (size_t)(ar_size / global_token),
      (size_t)(sizeof(char) * global_token) , comparator);

  for(i = 0; i < ar_size; ++i) //print sorted data
  {
    printf("%c", *(in_ptr + i));
  }
  free(in_ptr); // free dynamically allocated memory
  return 0;
}
Tagged as: , , , No Comments
3Feb/090

Weekly Biking

More biking states for the past week. Not exactly my best numbers.

Distance Traveled: 15.427 Miles

Max Speed: 28.3 MPH

Average Speed: 12.6 MPH

Time Traveled: 1 hour and thirteen minutes

2Feb/092

One Reason to Buy a Mac

When people ask me why I won't buy a Mac, I generally give three reasons:

  • Too expensive -I know a lot of people will argue that the 13.3 inch Macbooks actually have a list price that is similar to a Windows Laptop, but they forget that Macbooks are never available in the bargain bin on Black Friday
  • Right click - although I have gotten a little more used to the way Mac's double click, I still don't like it that much.
  • Delete key - this is decidedly annoying, and I just don't like having to press Function+Backspace when I want to delete.

Mind you, this is just me and I won't try to push these ideas on other people, some people don't mind throwing down a few hundred extra dollars, or losing the delete key (which most people never use). But lately, I have discovered one very good reason to buy a Mac.

The day before Thanksgiving, I tripped over the power cord to my HP dv2910us. Thankfully, the laptop was wedged between piles of junk and it didn't fall off the table, but the plug was bent out of shape and the little plastic insulation on the plug's tip cracked off. Since I needed a working laptop for school, I couldn't afford to order a new power cord from ebay and wait a week for it to arrive, so instead of paying ten dollars, my dad paid fifty dollars for a new cord and brick. I know its crazy, but it had to be done. Once I had the new cord, I figured the matter was resolved and that everything was going to be hunky-dory.

Just last week,  two months after I tripped over my power cable, I woke up, plugged my laptop in, and turned it on, just like most any morning. It immediately started making a funny, high-pitched buzzing, so I switched it off. It didn't take me long to notice that the power connection port was sparking, so like any sane man would do, I unplugged the power. If you look at the poorly-taken picture to the left, you can just barely notice that the white plastic ring surrounding the port is melted on the left-hand side. As far as I can guess, my tripping over the cord probably damaged the connection port just slightly, and two months of plugging and unplugging the power cable was enough to aggravate the port's damaged condition to the point that it started shorting.

26Jan/090

Weekly Biking

Distance Traveled: 6.475 Miles

Max Speed: 29.2 MPH

Average Speed: 12.7 MPH

Time Traveled: thirty minutes

I walked to class twice last week, and Monday was MLK day, so I didn't bike very much.

Tagged as: , No Comments
19Jan/090

Weekly Biking

Another week, another set of biking stats.

Distance Traveled:12.772 Miles

Max Speed: 28.8 MPH

Average Speed: 12.4 MPH

Time Traveled: 1 hour and two minutes

So I didn't bike nearly as much this week, as twice this past week I walked to class. My average speed for this week was just slightly higher then last week, which is nice to see.

The unfortunate thing about my campus, is that its all hills. The road leading out of campus has a very slight downgrade and so I can average about 26 MPH on it for about ten minutes without a problem, whereas coming back to campus I average only about 18 MPH. The short sprint down to class from my room is all downhill, and so I can coast most of the way down at around 25 MPH, once I hit the main part of campus I have to slow down substantially so that I don't hit people or cars.

Coming back to my room from class is always a trial for me however. When I'm carrying a few books and my laptop, I'm lucky to average eight miles an hour trudging uphill.

Tagged as: No Comments
15Jan/090

Seven-Segment Display in VHDL

I wrote my first VHDL project last night during lab, andgot to see my design come to life when I programmed it into a Spartan3 FPGA. It was a pretty basic program, that changed a seven-segment display (like those old LED screens on old calculators) based on which switches were flipped. The four switches used were a BCD (Binary Encoded Decimal) value, so if the first and fourth switches were on and the second and third switches were off, then the BCD value would be 9 (1001). The seven-segment display would then display the value "9" if the switches were in position 1001. Fairly simple to design and program, but quite fun to play with. My VHDL code is below.


entity seven_segment is

port(
    x3,x2,x1,x0: in std_logic;
    a,b,c,d,e,i,g: out std_logic
);

end seven_segment;

architecture Behavioral of seven_segment is

begin
    a <= NOT( x1 OR x3 OR (x2 AND x0) OR (NOT(x2) AND NOT(x0)) );
    b <= NOT( NOT(x2) OR x3 OR (x1 AND x0) OR (NOT(x1) AND NOT(x0)) );
    c <= NOT( x3 OR x2 OR x0 OR (NOT(x0) AND NOT(x1)) );
    d <= NOT( x3 OR (NOT(x0) AND NOT(x2)) OR (x1 AND NOT(x0)) OR
        (x1 AND NOT(x2)) OR (x2 AND x0 AND NOT(x1)) );
    e <= NOT( (x1 AND NOT(x0)) OR (NOT(x2) AND NOT(x0)) );
    i <= NOT( x3 OR (NOT(X0) AND NOT(x1)) OR (x2 AND NOT(X0) AND x1) OR
        (x2 AND NOT(x1)) );
    g <= NOT( x3 OR (NOT(x1) AND x2) OR (x1 AND NOT(x0)) OR
        (x1 and NOT(x2)) );

end Behavioral;

12Jan/090

Weekly Biking

I thought that since I now have a bike computer, I could post how much riding I'm doing weekly. I generally go down to campus at least twice a day and maybe two other short trips out to town every week. Unfortunately, I forgot my computer a few times this past week, so my numbers aren't quite accurate, but still surprisingly higher then I would have thought.

Distance Traveled: 18.805 miles

Maximum Speed: 29.0 MPH

Average Speed: 12.0

Time: 1 hour and 34 minutes

Another surprising fun fact: after only three weeks, I had to pump my tires up again.

Tagged as: , No Comments