Tuesday, May 29, 2012

Area of Focus: C# and HTML5

It seems as if the best move for me would be to focus on a more modern language, such as C#, instead of the fundamental language of C at a low level.

I'm feeling confident about my level of pure C knowledge at this point anyway. There probably aren't too many jobs these days that are going to require me to dynamically allocate memory for strings on a ('sizeof' * length) level.

I've used C# before, when using XNA, so luckily I have some experience with it. Hopefully it turns out to be everything that a modern language should be. It will be nice to have garbage collection for once as a backup. Thank God for that...

Also, HTML5 seems to be even better than I thought it would so I am going to try to spend an equal amount of time on it as well.

Visual Studio Express, why u take so long to install?? =(

Coming Next: C# practices, standards, and classes. Oh My!

Wednesday, May 23, 2012

Lanlost.com has risen from the dead!

I just wanted to take this moment to thank my friend Ryan N. for deciding to magically ressurect my old domain for me. I thought it would be lost forever as the last time I let it lapse someone else registered it for 10 years and did nothing with it.

Since he is also awesome, he has provided me with hosting on his server which means I will now be able to accomplish a lot of the HTML5 and other code experiments I've wanted to do.

I never asked him. He's just awesome like that and took the initiative and made this happen for me.

[todo: Create and Insert Good Guy SA picture].

Update:

(Not an actual picture of Ryan N.)

Areas of Focus: Mobile Development or HTML5?

Title says it all. Which to focus on?

On one hand, mobile development is something I have been interested in for years. This goes back all the way to the pre-smartphone era for me. Something about seeing my program running on a hand held device was something that attracted me and still does. The situation has MUCH improved since then luckily.

With HTML5 the possibilities are endless. With mobile phones and tablets becoming more and more powerful this might eventually be the end all solution to cross platform problems. Are there any browsers that allow for HTML5 yet? Pixel perfect accuracy and drawing on a website? Check. Javascript instead of Java? Check.

While I ponder this maybe I'll take a stab a both in a simple way. If I'm going the mobile development route then it looks like it's time for me to get back into Linux full time. I always loved the Linux environment more even though it primarily consists of Eclipse and ADB.

I realize no one reads this yet but this would be a great time for someone to weigh in on both options. They are both going somewhere so either one isn't really a bad choice. .. Or is it? Please tell me something I don't know!






Well, at least it looks like I can integrate HTML5 with blogger in sort of a hackish way.

String Manipulation in C - 01

Here is another one...

Problem:
Escape all % characters in a string; % is the escape character.
Example: “I’d like 2% milk” becomes “I’d like 2%% milk”.

I decided to make it work for any problematic character and to be replaced with any escape character that you should choose. Maybe I will create an amendment later that allows you pass an array of characters that each should be escaped.

I took on this problem because they specifically talked about how easy it would be in C++ or Java where string manipulation is MUCH more advanced and defined. In C you can't simply insert the escaped character because it isn't a string but a character array of a fixed size. This means that you are going to have to allocate the space for it.

Here is what I came up with:
#include <stdio.h>
#include <string.h>

// for malloc, in memory. *
#include <stdlib.h>

char *escapeString (char *origString, char badChar, char escapeChar)
{
    int newStringLoc = 0;
    int toEscape = 0; 

    // Determine number of characters we will need to allocate.
    for (int curChar = 0; origString[curChar] != '\0'; curChar++)
        if (origString[curChar] == badChar)
            toEscape++;
   
    char *newString = malloc (strlen(origString) + toEscape);

    // This really shouldn't happen in 2012...
    if (!newString)
        return NULL;
 
    for (int curChar = 0; origString[curChar] != '\0'; curChar++)
    {
        if (origString[curChar] != badChar)
            newString[newStringLoc] = origString[curChar];
        else
        {
            newString[newStringLoc] =  escapeChar;
            newString[++newStringLoc] = origString[curChar];
        }
 
        newStringLoc++; 
   }

   return newString;
}

int main(void)
{
    char *origString = "This is a problematic string! 2%, 3%, 4%%!";
    char *newString = escapeString (origString, '%', '\\');

    // Check to see if it worked..
    printf ("origString: %s\n newString: %s\n", origString, newString);
 
    free (newString);
    return 0;
}

To make it much easier to see what is going on here, I have replaced the original escape character ('%') with ('\'). The output looks like this:


c:\CProgs\Str1-1>lcc strings1.c
c:\CProgs\Str1-1>lcclnk strings1.obj
c:\CProgs\Str1-1>strings1
origString: This is a problematic string! 2%, 3%, 4%%!
 newString: This is a problematic string! 2\%, 3\%, 4\%\%!


for (int curChar = 0; origString[curChar] != '\0'; toEscape += (origString[curChar++] == badChar));

Tuesday, May 22, 2012

Interesting Article and My Solution

So, I found an article on Coding Horror about how most programmers can actually not write a simple program. I figured their example question that people have trouble writing would be a good test to see how efficient I could make it.

The problem is as follows:

Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".

I started at (4:33:35 AM) and ended at (4:36:45 AM) for a total of around 3 minutes and 10 seconds.

Here is what I came up with:

#include <stdio.h>

int main()
{
     /* Write a program that prints the numbers from 1 to 100. 
        But for multiples of three print "Fizz" instead of the number
        and for the multiples of five print "Buzz". For numbers which are
        multiples of both three and five print "FizzBuzz". */

     for (int num = 1; num <= 100; num++)
     {
          if (num % 3 == 0)
             printf ("Fizz");
          if (num % 5 == 0)
             printf ("Buzz");
  
          if ((num % 3 != 0) && (num % 5 != 0))
             printf ("%d", num);
  
          printf ("\n");
     }

     return 0;
}

The short article is a fantastic read and only takes a second. Both solutions that appear early in the comments fail for various reasons. It is not necessary to have a statement to print both Fizz and Buzz when you can simply leave out the newline until the end and solve both in one go.

Even though it's simple problem and may seem like a step back in complexity from the previous post I'm happy with my solution. Moving to something a bit more complex tomorrow. For now, I need sleep.

Monday, May 21, 2012

Array Problem - 01

Problem:
Given an unsorted array of size n containing the integers 1 through n in random order with one element randomly replaced by 0, determine the missing element most efficiently.

Here was the solution I came up with:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
     
void InitArray (int *TheArray, int n)
{
     // Fill the array with values 1..n
     for (int i = 0; i < n; i++)
         TheArray[i] = i+1;
                   
     // Remove a value at random.
          TheArray[rand() % n] = 0;      
}
     
// From: http://codesam.blogspot.com/2011/04/how-to-shuffle-array-or-fisher-yates.html
// With reasoning from CodingHorror: http://www.codinghorror.com/blog/2007/12/the-dangerof-naivete.html
// Original Theory: http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
void KnuthShuffle(int *orgArray, int arraySize)
{
     if (arraySize == 0 || arraySize == 1)
          return;
     
     int i;
     int index, temp;
     for (i = arraySize - 1; i > 0; i--)
     {
          index = rand() % (i+1);
          temp = orgArray[index];
          orgArray[index] = orgArray[i];
          orgArray[i] = temp;
     }
}
     
void PrintArray (int *TheArray, int n)
{
     printf ("TheArray[%d]: {%d", n, TheArray[0]);
           
     for (int i = 1; i < n; i++)
          printf (", %d", TheArray[i]);
           
     printf ("}\n");
}
     
int main(int argc, char *argv[])
{
     // Sum of integers between 1 and n = n(n+1)/2
          
     int n = 9;         
     int TheArray[n];   
     int TotalValue = 0;
           
     // Check for arguement passed to the program to define n.
     if (argc == 2)
          n = atoi(argv[1]);
                   
     // Seed the randomizer with the current time.
     srand(time(NULL));
                   
     InitArray (TheArray, n);
     KnuthShuffle (TheArray, n);
     PrintArray (TheArray, n);
                   
     // And we will avoid a function here..
     for (int x = 0; x < n; x++)
          TotalValue += TheArray[x];
                   
     printf ("The missing number is: %d!\n", ((n*(n+1))/2) - TotalValue);
           
     return 0;
}

 This code accepts input from the command line to specify n. If I see a way to optimize it more I will do so at a later time.

EDIT:
Ack, I just realized that I defined TheArray with the default value BEFORE checking the command line arguments. It wasn't too long before I was grabbing data from outside the array's boundaries. This is what I get for adding the command line functionality at the last second.

A simple fix would be to replace the beginning of the main function with:

#include <stdio.h>
int main(int argc, char *argv[])
{
     // Sum of integers between 1 and n = n(n+1)/2
          
     // Check for arguement passed to the program to define n.
     int n;

     if (argc == 2)
          n = atoi(argv[1]);
     else
          n = 9;
        
     int TheArray[n];   
     int TotalValue = 0;
 . . .

That works much better.

Oracle Challenge - 01

For archival purposes these are the notes I left myself for the Oracle Challenge that I won at my last job. You may find something of interest here. Proceed at your own risk. You have been warned, I have an awesome style. ;-)
Please note that I have censored data that could possibly be relevant still.

----------------------
-- Oracle Challenge --
--    Move Notes    --
----------------------

-> Downloaded NaviCat 
-> Transferred Files via SFTP to PC: /media/LandoServ/CHALLENGE
-> Transferred Files to shared hosting at IX: /sitetomove.com

! Noticed index.php has the broken comment. Change \** to /**..

! Noticed database is set to connect at localhost (127.0.0.1), change to
  the same IP I got the site from. This likely means the database was running
  on the same server and WAS connecting from localhost. IP: 98.xxx.xxx.227

! Could not use NaviCat to connect to MySQL database on old host. It is
  claiming that my host is blocked for some reason or another.
  -> Used phpMyAdmin on their cPanel to connect to the database. ARCHIVED.
   || OR || || OR || || OR || || OR ||
   || OR || || OR || || OR || || OR ||
  -> Customer claimed there was no cPanel. I see a directory for it though 
     and tried it. If this is a mistake and there IS supposed to be no cPanel
     the datbase dump could still be accomplished in the following way:
 -> Log in with SSH and run:
 mysqldump -u webmove_dbusr -p******** webmove_db1 > webmove_db.sql
  ftp lanlost.com
  -> c293793
  -> *************
  -> cd lanlost.com
  -> binary
  -> send webmove_db.sql
  -> exit
 Voila! Go to lanlost.com/webmove_db.sql and grab it. Could also use SFTP but since
 I am going to need to upload it to this location anyway I figured this would work.

! ... \\Find me - No wonder they are leaving. (wp_settings.php)
  -> And in /wp-includes/http.php, line 193

! Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate
 122880 bytes) in /hsphere/local/home/c293793/sitetomove.com/blog/wp-content/plugins
 /wp-pagenavi/core.php on line 107 
  -> Increased memory size to 64mb

-> Moved site to /blog

+ One hour and two minutes later - DONE, almost.

-> Added default wordpress .htaccess file
-> Changed RewriteBase / to RewriteBase /blog/

*** Fixed. ***

! Addendum, XML Feed is borked.
  -> auto_prepend_file = /hsphere/local/home/c293793/sitetomove.com
     wejnswpwhitespacefix.php - (In php.ini, didn't Work)
  -> include_once("wejnswpwhitespacefix.php"); INTO /index.php + comment.
   + Original implementation calls for include not include_once but
        then you get errors about 'redefinition.'
        (http://wejn.org/stuff/wejnswpwhitespacefix.php.html)
  -> LINKS IN XML FEED DON'T WORK ... the permalink structure should be
     /%postname% not /%post_name%. This will fix it.

! WP Super Cache is also broken.
  -> Edited advanced-cache.php, changed (OLD USERNAME) to (NEW USERNAME)
  -> Added WP Super Cache rules to .htaccess 

*** Truly fixed! ***

I won not only by being the first person to turn in a complete solution but for the "Addendum" fixes which were not noticed and were thus seen as extra credit.

A Fresh Start.

This blog is going to be soon filled with a much needed reintroduction to programming -- the fun way. This may mean a lot of code examples, sorting algorithms and their uses, etc. If time permits I will try to spice it up with a bit of jQuery.