Saturday, October 31, 2009

Control Structures

My next assignment is to use control structures. I am going to go ahead and post the code then go back in dissect it for you.

/* Austin McGowan
 Find Largest/Smallest Number Code
 This program finds the largest number with a known amount of numbers
 and it also finds the lowest number with an unknown amount of numbers.
 this project is designed to test knowledge of control structures.*/

using System;


class ControlStructures
{
    static void Main(string[] args)
    {
        bool doExit = false; // Controls near-infinate loop.
        
        while (!doExit)
        {

            int currentNumber = 0 ; // Stores current number.
            int userInput = 0 ; // Store userInput
            int counter = 0 ; // counter for while loops
            int controler = 0 ; // controler for while loops
            string testString ; // For getting strings to int.

            Console.WriteLine("\nWelcome to my Week 2 Application.") ; // say helo.
            Console.WriteLine("[1] Find the largest number with a known quanity of numbers.") ; //Menu Options 1
            Console.WriteLine("[2] Find the smallest number with an unknown quantity of numbers.") ; // Menu options 2
            Console.WriteLine("[3] Quit") ; // menu options 3
            Console.Write("\nPlease enter your input: ") ; // prompt user for input

            testString = Console.ReadLine() ; //read user input to string.
            userInput = int.Parse(testString); //attempt to change string to Int
            Console.WriteLine("userInput = {0}", userInput); // Debug statment

            switch(userInput) //start switch on userinput
            {
                    
                case 1:// put largest number finder here
                    Console.Write("\nHow many numbers would you like to enter?: ") ; //as user how many numbers they have
                    testString = Console.ReadLine() ; //store in string
                    controler =  int.Parse(testString); // change to int

                    while (counter != controler) // start while loop based on the above input
                    {
                        Console.Write("\nInput integer for comparision: ") ; //ask for input
                        testString = Console.ReadLine(); // store in string
                        userInput = int.Parse(testString); // change to int
                        if (userInput > currentNumber) // check to see if user input is greater.
                        {
                            Console.WriteLine("\n{0} is greater than {1}. Setting stored varible to userInput", userInput, currentNumber); // output what is going on
                            currentNumber = userInput; //change current number since users is bigger.
                        }
                        else
                        {
                            Console.WriteLine("\n{0} is less than or equal to {1}. No changed made to stored varibles", userInput, currentNumber); // tell the user what is going
                        }
                        counter++ ; //increase counter so the while loop stops as some point
                    }
                    Console.WriteLine("\nOf the {0} numbers you entered the largest number was {1}.", controler, currentNumber); // out put results to user.
                    break ; // break out of Switch
                case 2: // put lowest number finder here.
                    Console.Write("\nInput integer for comparision (enter -999 to exit): "); // prompt the user for input
                    testString = Console.ReadLine();
                    userInput = int.Parse(testString);
                    currentNumber = userInput;

                    while(userInput != -999) // Make sure the user isn't trying to exit the program
                    {
                        if (userInput < currentNumber) // check if the user's number is lower then the current number.
                        {
                            Console.WriteLine("\n{0} is less than {1}. Setting stored varible to userInput.", userInput, currentNumber); // Tell the user what is going on.
                            currentNumber = userInput; // update the currentNumber variable
                        }
                        else
                        {
                            Console.WriteLine("\n{0} is greater than or equal to {1}. No change made to stored variables." ,userInput, currentNumber); // Tell the user what is going on.
                        }
                        counter++ ; // Increase counter for last output.
                        Console.Write("\nInput integer for comparision (enter -999 to exit): "); // Ask for input
                        testString = Console.ReadLine();
                        userInput = int.Parse(testString);
                    }
                    Console.WriteLine("\nOf the {0} numbers you entered the smallest was {1}.", counter, currentNumber); // Tell the user thier results.
                    break ; //break
                case 3:
                    doExit = true ; // Set to exit the maste while loop
                    Console.WriteLine("Exiting.. \nThanks for coming."); // thank the user.
                    break ;
                default: // invalid input.
                    Console.WriteLine("Invalid Input..") ;
                    break ;
            
            }
        }
    }
}
While I use comments, a lot, that might not be enough for you so I am going to go ahead and break it down some more.

class ControlStructures
{
    static void Main(string[] args)
    {
This is our basic main statment. We are calling the class ControlStructures since that is what we are showing.

bool doExit = false; // Controls near-infinate loop.
        
        while (!doExit)
        {
Here we declare a boolean variable and assign it the value of false. Boolean values can only be true or false. It is important we put this here because it is what gets us into our while loop. In this second we also start into our while loop. This loop will repeat over and over until doExit (our boolean variable) equals true.

int currentNumber = 0 ; // Stores current number.
            int userInput = 0 ; // Store userInput
            int counter = 0 ; // counter for while loops
            int controler = 0 ; // controler for while loops
            string testString ; // For getting strings to int.

These are all out variables. It is good practice to declare variables together and at the top so they are easily located when the time comes to change or update the program. The first four variables are integers and set to zero as thier default value. This is important as everytime the loop runs these variables need to reset to zero. If they aren't it might cause errors in the code. currentNumber will be used to store ethier the highest or lowest number depending on which menu option we are working at that time. userInput will be used to store the integer version of the user's typed number. counter will be used to keep track of loop runs. controler will be used to exit the known value loop. testString is a storage variable to deal with storing userinput. You will see what I mean in a second.

Console.WriteLine("\nWelcome to my Week 2 Application.") ; // say helo.
            Console.WriteLine("[1] Find the largest number with a known quanity of numbers.") ; //Menu Options 1
            Console.WriteLine("[2] Find the smallest number with an unknown quantity of numbers.") ; // Menu options 2
            Console.WriteLine("[3] Quit") ; // menu options 3
            Console.Write("\nPlease enter your input: ") ; // prompt user for input

            testString = Console.ReadLine() ; //read user input to string.
            userInput = int.Parse(testString); //attempt to change string to Int
            Console.WriteLine("userInput = {0}", userInput); // Debug statment
Here is the Console.WriteLine thing we learned last week. This simply displays the menu for the user then askes them for thier input. Once it gets the input it takes it and turns it into a int. This prevents people from inputing inproper numbers and crashing the system. It is the best way to go about it until we learn about exception handleing.

switch(userInput) //start switch on userinput
            {
This starts our switch statment. A switch statement takes a value (in this case userInput) and compares it to multiple cases. If the value matches any of the cases then that case is executed. If the value doesn't match any of the cases then the "defualt" case is executed.

case 1:// put largest number finder here
                    Console.Write("\nHow many numbers would you like to enter?: ") ; //as user how many numbers they have
                    testString = Console.ReadLine() ; //store in string
                    controler =  int.Parse(testString); // change to int

                    while (counter != controler) // start while loop based on the above input
                    {
This is case 1. This will be executed if the user selects 1 from the menu. The second line asks the user for the number of numbers we will be using. It then takes the user input and puts it into controler after converting it to int. After that a while loop is started. This while loop will keep going as long as counter does not equal the new, userinputted value, controler.

Console.Write("\nInput integer for comparision: ") ; //ask for input
                        testString = Console.ReadLine(); // store in string
                        userInput = int.Parse(testString); // change to int
                        if (userInput > currentNumber) // check to see if user input is greater.
                        {
                            Console.WriteLine("\n{0} is greater than {1}. Setting stored varible to userInput", userInput, currentNumber); // output what is going on
                            currentNumber = userInput; //change current number since users is bigger.
                        }
                        else
                        {
                            Console.WriteLine("\n{0} is less than or equal to {1}. No changed made to stored varibles", userInput, currentNumber); // tell the user what is going
                        }
                        counter++ ; //increase counter so the while loop stops as some point
                    }
This is kind of a large amount of code but it all goes back. The first three lines are something you've seen before. We ask for input the take it and change it to an integer and store it in userInput. Now we move into our first if statement. If statements are one of the easist control structures to understand. The above code could be written in english as: If userInput is greater than the currentNumber then tell the user it is great and set currentNumber to equal userInput. If it isn't then tell the user it isn't and do nothing. Line 12 then adds one to the counter variable. counter++ is the same as writing counter = counter + 1. We then close the while loop with }.

Console.WriteLine("\nOf the {0} numbers you entered the largest number was {1}.", controler, currentNumber); // out put results to user.
                    break ; // break out of Switch
These lines will only execute after the user has input all the numbers he wanted to thanks to our while loop. The first line outputs to the user thier largest number and the second line breaks out of the switch statment. "break;" should be included at the end of every case unless you have a special reason for it not to. We we talk about that at a later date.

case 2: // put lowest number finder here.
                    Console.Write("\nInput integer for comparision (enter -999 to exit): "); // prompt the user for input
                    testString = Console.ReadLine();
                    userInput = int.Parse(testString);
                    currentNumber = userInput;
This starts our second case. Since we don't how many numbers they are going to enter we need to provide them with a sentinal value. A sentinal value is a hardcoded value that when reached in some way will exit a loop. In this case we are going to use negative nine hundred ninty-nine. We then take the input and store it in userInput like we have before.

while(userInput != -999) // Make sure the user isn't trying to exit the program
                    {
This line starts our while loop. As you can see we have hardcoded our sentinal value. The user will be prompted to enter number after number until they type -999 which will signal to the program that they are done.

if (userInput < currentNumber) // check if the user's number is lower then the current number.
                        {
                            Console.WriteLine("\n{0} is less than {1}. Setting stored varible to userInput.", userInput, currentNumber); // Tell the user what is going on.
                            currentNumber = userInput; // update the currentNumber variable
                        }
                        else
                        {
                            Console.WriteLine("\n{0} is greater than or equal to {1}. No change made to stored variables." ,userInput, currentNumber); // Tell the user what is going on.
                        }
                        counter++ ; // Increase counter for last output.
                        Console.Write("\nInput integer for comparision (enter -999 to exit): "); // Ask for input
                        testString = Console.ReadLine();
                        userInput = int.Parse(testString);
                    }
Now we start our if statment again. This if statment in english would read as such: If userInput is less than the currentNumber then tell the user and a set currentNumber to the userInput. If it isn't then tell the user and do nothing. Once the if statment completes we increase the counter again. We are going to use this to tell the user how many numbers they put in. Then we ask the user for input again and store it correctly so the while loop can start over again. After that we close the while loop with } .

Console.WriteLine("\nOf the {0} numbers you entered the smallest was {1}.", counter, currentNumber); // Tell the user thier results.
                    break ; //break
These lines just print the results to the user and then break out of the case statment.

case 3:
                    doExit = true ; // Set to exit the maste while loop
                    Console.WriteLine("Exiting.. \nThanks for coming."); // thank the user.
                    break ;
                default: // invalid input.
                    Console.WriteLine("Invalid Input..") ;
                    break ;
            
            } //switch
        } //while
    } // main
} // class
This is the third case. Since the third case exits the program all we have to do is set doExit to true. Remember that from the very begining? This will cause our loop to stop repeating which causes the program to exit. After that we tell the user goodbye and break out of the switch. The next line is the default statment. This will only execute if they didn't enter a valid option when prompted with the menu so we just tell them they did something wrong. The last four lines are us just closing all our statments in order: switch, while, main, class. I hope this makes some sense to some people.

- A. R. McGowan.

break ;

Tuesday, October 20, 2009

Yes, The Bible.


I found the greatest site ever today. I know this site is suppose to be about programming and coding and what not but I just had to share this. Did you know there is a wikipedia for the conservative? It seems truth is differnt if you are conservative. That is niffty! I wish I was conservative so I could have differnt truths then the rest of the world.  This is really the most helpful tool I could of found on the internet. Good bye forever WikiPedia. A new wiki has won my heart.

The best thing they are doing is rewriting the bible. Don't worry the only thing changing is the names of the holy trinity, the inclusion of the fact that Jesus had slaves and the entirity of the Lord's Prayer. Nothing important really.

If you want to help out on the project I am sure they would enjoy a helping hand. I would love to help but I fear the amount of anger I have towards them coupled with my wit is just a loose stray ready to break a camels back. Plus I'd rather strike out in other ways.

- A. R. McGowan.

this text is conservative.

Monday, October 19, 2009

Surprise!

Have you ever learned something that you never thought you wouldn't know? That seems a little hard to follow.


I have worked on computers since I was a child. My mother had a Tandy complete with giant floppy disk drive and turbo button. Remember "turbo" buttons? Those were the days. I have, thanks to my supportive family, been blessed with quite a large amount of computer knowledge. I've understood how to use Office and what a mouse was since I was a child. I have built and repaired quite a few computers all by my lonesome. I understand that basics of computer repair such how to access BIOS. I have done it a hundred times from accessing it to flashing it an update to forcing a CMOS switch reset. While studying for one of my classes I learned something I thought I knew.

I learned what BIOS meant. Of course I always assumed it was an abbreviation by its all caps nature. When I learned that it meant Basic Input/Output System I was amazed that I hadn't ever had a clue what it meant. Amazed and shocked. Not sure how often this is going to happen but it did dawn on my while typing this that I have no clue what CMOS means either.

- A. R. McGowan.


it is scary realizing you know nothing

Saturday, October 17, 2009

C# Hello, World! Console App.

Pretty much every programmer learns "Hello, World!" first. Because C# is so much stronger and surprisingly different from C++ we are starting at the basics in my Advanced Programming class. Here is how you do a Hello, World! console application in C#.

First go into Visual Studio 2008/Express and goto File -> New -> Project. From the template list select "C# Console Application". In the name box go ahead and call the program "HelloWorld". Visual Studio will take it upon itself to generate code that looks something like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

Now this is a kinda overkill amount of code so we are going to just delete most of it. Remove most of the use lines as well as the namespace. Make sure you delete both the opening and closing curly braces for the name space. If you've done it right it should look like this.

using System;

class Program
{
    static void Main(string[] args)
    {

    }
}

Now we want to rename Program.cs in our file viewer. We want it called the same as our class which in this case is called "HelloWorld". Right Click on Program.cs and select Rename. Type in HelloWorld.cs . Don't forget the .cs which is obviously rather important! When you rename the file it will ask if you want to update references to the file within your code. Click YES.

Now all we have to do is add a console print command to the body of main. To do this type Console.WriteLine("Hello, World") ; into main. The code should now look like this.

using System;

class HelloWorld
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello, World!");
    }
}


With that your program should compile and run. If everything was done right then running it should look like this:


Congrats! Our program works!

- A. R. McGowan.
goodbye, world!

Thursday, October 15, 2009

Pick Two

I learned a new phrase:

You can have it Fast, Good or Cheap so pick two.



It seems this is an accepted statement in the software engineering world. Because most firms are working on multiple projects and because good quality software takes a very long time clients have to be realistic on time, quality and price.

Good + Fast = Amazingly Convenient:
Good and fast means that the company is going to stay up all night, every night getting your stuff done. As such it is going to cost you a butt load. If you can afford it this should be your first choice. Sadly people can't always afford this option.


Good + Cheap = Amazingly Slow:
Good and cheap means the company is going to make your product right but they aren't going to spend all their man power on it. This option is the second best and sometimes the best if there is a large time window. I would predict that most video games are made in this category or the one mentioned above.

Fast + Cheap = Amazingly Inferior: 
Fast and cheap means the company isn't going to focus so much on quality. While your needs may be met to their fullest there is no guarantee that it is going to be done well. This option will most likely get you a working product that you can attempt to use. This category will most likely result in buggy programs. Choosing this two options is like telling the company that you want it now but you don't respect them enough to pay full price. You aren't going to like the result. 

Some people might read the above and think it is a little harsh but its true. It is important for a company to realize that coding isn't something that takes two hours. The engineering of software is a tricky and fickle mistress.

- A. R. McGowan.


there isn't enough time or money in the world...

Wednesday, October 14, 2009

Classes started today!

Today was the first day of classes! So far I am loving my Advanced Programming class and we haven't really done much! My writing teacher seems very nice and down to earth and might even be reading this right now! If they are then "Hello!" to you professor. If I am using a lot of exclamation points then that is because I am exited to be back in classes. I have been very bored.

Advance programming is what I thought it would be. That is to say that is the next level of programming based of the building blocks I have already learned. Simple enough.

College Writing I is College Writing I. I hope dearly it isn't all business writing. I love to write but I hate being constrained by it. Writing is an art form and it should be expressed as such.

Life Cycles of Software is an interesting class. So far I have learned to call myself a Software Engineer and not a Programmer. Got that? I engineer software because I am a software engineer. I will never program software again!

Operating Systems I am still very vague on. I get the IDEA of operation systems but I don't get what I will learn. Hopefully after taking this class I will be able to rewrite hangman so it runs on most computers instead of just on Windows Vista Ultimate 32 Bit.

I really need to rebuild my computer before I start doing full scale video games. I don't think I am ready to render worlds never mind plat-formers on this thing. Here is hoping I can rebuilt this thing for Christmas.

I really want Windows 7. I had the beta installed on my laptop and I loved every moment of it! Sadly it will be awhile before I get around to getting it. I need to make sure Visual Studio and VMachine work on 7 before I can commit my Desktop to it. Wouldn't it be ironic if the software that creates computers couldn't run on a computer? The irony would kill me.

Is there anybody out there? Feel free to comment as it feels a little lonely out here on the vast internets.
- A. R. McGowan.


just another brick in the wall.

Monday, October 12, 2009

I am sad faced...

How depressing. As you are aware I was really excited to start classes today. I have been incredibly bored for the last few days (read: week). The problem is my day usually consist of studying, listening to music, and reading stuff on the internet about my school work (pseudostudying). If you remove school from the equation then suddenly my day becomes listening to music. Hulu fills in the gaps when there are things to watch on Hulu. Sadly there has recently been nothing I am interested in on Hulu. Is there a secret gem you love on Hulu? Tell me about it. The list of things I watch is long and daunting yet none of it is being update right now.

Oh, back on point. I am writing this at 5:04 in the morning on Monday. I attempted to stay up all night so I could get a jump start on my classes. I am a huge nerd. When I went to log in I found a sad message waiting for me:

This is not an active course.
The semester has either not begun yet or has already ended.
Please return to your Course Listing and select another course.
If you were wondering when my class does start it turns out the answer to that question is Tuesday. Why Tuesday? I haven't the slightest damn clue. I am serious about that Hulu Question, here is a list of all the stuff I watch on Hulu on a regular basis.


Currently Watching:
  • The Dresden Files
Caught up On:

  • 30 Rock
  • Bones
  • Chuck
  • Community
  • Defying Gravity
  • Dollhouse
  • Dorm Life
  • Dr. Horrible's Sing-Along Blog
  • Fringe
  • Glee
  • Greek
  • Heroes
  • Kings
  • Legend of the Seeker
  • Lie to Me
  • Mental
  • The Office
  • Psych
  • Royal Pains
  • Spaced
  • SG-U
  • Terminator: The Sarah Connor Chronicles
  • Trauma
  • Warehouse 13
As you can see I watch a lot of different things. Feel free to suggest anything I just ask that I can watch it from the start of the series as I hate coming in late. Guess I will post again tomorrow once I learn more about my classes.

- A. R. McGowan.

tuesday is not today