cse

A Program by any other Name…

by ScottGeek 28. June 2009 13:48

So you find yourself trying to write a computer program. You’ve have decided on a program language (yes, regardless of where you go and what you do…writing a computer program evolves some kind of “Language”) Well, there are some things you should know, or at least think about before you go off the deep end and get yourself frustrated at trying to write a program that either doesn’t work or at best doesn’t do what you want it to do.

Is it hard to write programs? Mostly that, like so many other things, depends on the person. I’ve been around long enough to have seen folks that program with ease, while others get promoted to their proper level of never having to write programs (code) again. There are those that hate it and there are those that think they know everything… and then there are those like me (ScottGeek) that just see programming as a means to an end (don’t get me wrong, there is a certain level of…ummm…wonderment about writing programs that do interesting things…especially if your code solves a problem that someone has, and if you get paid for your efforts, all the better).

Ok, now that I’ve got all of the ‘wet noodle’ talking done… programming… where do you start? I would suggest before you start that you become ‘knowledgeable’ about some things that are common in all computer programming… YES, I wrote ALL and I mean “ALL”!

First: there is this thing called a “variable” or if you want exact phrase- Data Structures!

I’m not sorry… you have to start here (yeah sorta like that board game that has a “start here” spot) The “start here” spot in programming is D.A.T.A. S.T.R.U.C.T.U.R.E.S! Now don’t go away yet…this is really not the boring part of programming.

In every program (be it Halo, Word, or Twitter) there is the thing called “information”. Information is “data” that has some meaning assigned to it. Your Halo kills or your Halo profile- is in the depths of the Halo program- DATA. When you design your favorite Halo profile, that data becomes Information because you interacting with the Halo program. Ok, the wet noodle (your brain) doesn’t get it. Ok, something on the simple side… Twitter, a web page that allows you to type 140 characters that others can read, see, and sometimes understand… to the set of programs that make Twitter do what it does… your 140 characters is data. To the person reading your latest twit, what you are saying is information. Get it? The simplest example is the English words (data) and English sentences (information). The words have meaning, true, but the words (data) don’t say anything until you put them in a sentence (information).

Now, data- the raw little pieces that you put together to mean something… guess what a computer program spends most of it’s time doing? If you guessed that programs just really manipulate data in various ways, then you have begun to understand something very important about computer programs.

Ok, enough of the Bah Bah… Variables are just how to build Data Structures needed in the coding (or programming) process… yeap, a program does something with data and that data is kept in what is known as a Variable.

No, it’s a complicated idea, but it’s really easy to understand once you see a little code or syntax.

Regardless of what some people will tell you (especially when it comes to folks who only know one programming language)… all programming languages have variables and all programming languages have the same type of variables. Now, yes you can split hairs about how some languages represent different types of data, but in the end it’s still just data.

With that in mind… lets talk data…before we talk about syntax…

Data, there are TWO (count them) TWO types of data. Ok? Yeah, I will say it again… THERE ARE TWO TYPES OF DATA… Just two… not three… not one… not infinite…TWO! You need to fundamentally understand that…here and now. There are those who will tell you different…there are those who will make lots of noise about what they think they know… but in the end there are two types of data!

Type one: Numbers

This type of data is, as you would expect, that which contains the digits 0 through 9. There are lots of different kinds of numbers. Numbers that have plus or minus, numbers that have decimals, there are very large numbers, and very small ones. In all programming languages, every type of number can generally be defined (or represented)…one exception maybe something like imaginary numbers, but since imaginary numbers are not real it’s not a big deal (and yes, there are programs or simulators that you can do imaginary numbers with).

  The one thing about numbers that you should remember is that with numbers you do math, and computers are very good at doing math. Now, in programming languages, we define numbers into different types (here’s where some people like to say that there are more than two types of data…but numbers are still numbers regardless of how they look).

So, numbers are ‘typed’- Typed here means categorized or how something is defined. In programming it’s easier to look at numbers from a ‘typed’ point of view rather than just listing what kind of numbers and how to define them. With that I will explain numbers by grouping them into categories (or types). These types and generally available in most programming languages (yes there are exceptions).

Numbers are categories by size, sign, and decimal…which is the simplest way to look at it.

  Size: How big can a number be? Basically this tells you the range of a number. Now this changes depending on the computer you are working with.

  Sign: Can the number be positive or negative? Yes, not every number can have a minus value.

  Decimal: Can the number have a factional part? Some folks just call these real numbers.

Now one might ask…why not just one number type to do all things? Well, the best answer that I have for that involves you understanding how computers evolved. In the time beyond, when computers were the size of buildings – that’s how I would start. So to cut to the chase, the different types of numbers evolved mainly because the people that designed computers and computer languages were mostly mathematicians who had a very limited amount of things like memory. As a result there had to be different types of numbers that could be represented in various ways to save on memory space and be efficient for doing math. When you have lots of numbers, and not a very big computer, then you have to have many ways to define what a number is.

The first category of Numbers is things we call Integers. The only special thing about Integers that you need to remember, is that they do not have decimals. Beyond that, most languages give you a variety of ways to define the size of integers and whether or not you number has a sign. So, the Integers that have a small range and do not allow for a sign, these would be numbers that take the smallest amount of space. Using smaller Integers is one way to increase your program’s speed. It may or may not be important, but it’s there for you to use. Now of course you have to be careful. If you use a small Integer and your data gets too big to fit, you will get a program error or even a program crash (something called an Integer Overflow is when you put a number in small integer that will not fit- Underflow is when the number is too small to fit).

So, what kind of small integers are they?

  Looking at C#, the size of Integer is defined by how many bytes (or space it takes to represent the maximum and minimum numbers). So in C# land we have:

  Signed and Unsigned, 8,16,32, and 64 bit integers. Unsigned numbers actually use the same amount of space for the most part. They allow for larger values by removing the sign part.

Some examples types:

  byte and sbyte: A byte is a number in the range of 0 to 255. A sbyte is a number in the range –127 to 127. As you can see the unsigned byte does not have negative numbers, but the actual number of ‘numbers’ that can fit into a byte and a sbyte are the same. sbyte has 256 possible numbers (counting the 0) so does the byte. So, if they use the same amount of space, how come they can have a different range. Well, that’s done in how the number is stored in the computer. A byte is generally 8-bits long (i.e. there are 8 little pieces of memory that it takes to represent a byte). When you need a sign ( plus or minus) then you have to use one of those 8 little bits of memory to indicate the sign. So that leaves you with 7 bits of memory (doing the math that’s 1+2+4+8+16+32+64 = 127). So 7 bits for the number and 1 bit for the sign gives you the range –127 to 127. Magic? No…Math. Slot 7 represents 2^6 when it is set to a 1 or a value of 64
Slot 6 represents 2^5 value 32

etc..

Now you can always find the ranges of numbers for the language you are using just Google something like ‘types of integers in C#’… you’ll the various types of numbers. In general you can define most numbers as just plain integers. In C# this would look like:

int myInteger;

This is a simple variable ‘myInteger’ that is a integer. C# is nice in that it will allow you to set the initial value of the variable by using the syntax:

int myInteger = 0;

A special kind of number known as a Boolean…here’s where I know I will get into to trouble with the ‘purist’. Say what they may… a Boolean is still a number. In fact it is the smallest is space usage of anything that a computer can represent… a Boolean is a 1-bit number. It has the value or 1 or 0. The business of a Boolean is to be either true or false (which is the purist definition of a Boolean), but this is just how a Boolean is interpreted within a language (some languages will go as far as to allow you to set a Boolean to either 1 or 0; true or false). 

A Boolean variable in C# looks like:

Bool tstBool = false;

or

Boolean tstBool2 = true;

Yeah, Bool and Boolean the same.

Ok, enough on Numbers…

 

Type Two: Not Numbers

   Yeah, I’m being a smartarse… The second type is basically data that you really can’t (or shouldn’t) do math with. Yes, it can be data that looks like numbers, but it’s not. The key with non-Numeric data (as we call it) is to understand that you can’t do math with it or more important you can’t use this data as it was a number unless you do something to it (like convert it).

Ok, what is this stuff? letters, symbols, non-printing characters. The most general type of this type of data is something that we call strings. Now strings is just a collection of characters…and that’s the key to this. Characters! You know? ,.?/">ABCDEFG…!@#$%^&*()_+{}:”<>,.?/~`”… bah bah and the really fun part… don’t forget about the number characters. Like the characters “1234567890” (notice I put a “” around them to say that these are CHARACTERS not numbers).

Mostly, strings (which is a bunch of characters put together) is always surrounded by something like a “ to indicate that it is a string… here’s some examples to show the point:

 

  A number:

  int MyIntValue;

  string MyStringValue;

MyIntValue = 3;

MyStringValue = “3”;

Now, look at that…blink…look again. The integer 3 in MyIntValue is not the same as the character “3” in MyStringValue. No Way…No How are these the same. But, in the land of computing you can convert the character “3” to the number 3:

 

int MyNewIntValue;

MyNewIntValue = Convert.ToInt16(MyStringValue);

Yes, that “3” becomes a 3. Now what would happen if MyStringValue was a “ABC”? Yes, you get an error in C#. So can you test that the string is a good string of number characters before you try to convert it…of course.

Unlike VB, in C# there is nice function like IsNumeric, but you can write one:

 

bool IsNumeric(string input)
{
   bool result = true;
   for (int i = 0; i < input.Length; i++)
    {
      if (!Char.IsNumber(input[i]))
      {
       result = false;
       break;
     }
   }
  return result;
}

So to use that and test for Numeric characters you would do something like:

if ( IsNumeric(MyStringValue))
{

   MyIntValue = Convert.ToInt16(MyStringValue);

}

else

{

Console.WriteLine(“Error you need to give me a number!”);

}

 

So can you convert a number to a string…of course.

MyStringValue = MyIntValue.ToString();

 

Anyway, Data Structures… yes, you need to understand them. As you get into more and more complex programming the Data may get more complex, but at very lowest level… it still all boils down to these two types. Now at this point the purist will make all kinds of noise about how “objects” are not this or that… but what they will fail to tell you is that what they are doing is just creating a structure. To be exact, they are creating a data structure and if you drill into such a data structure deep enough, guess what you will find? It’s either a number or a non-number. The analogy works like this:

  In the Universe there are three fundamental particles known as Electrons, Neutrons, and Protons (and while there are sub-particles that exist within these fundamental particles), it remains true that everything in the known Universe is comprised of these three fundamental particles. You can put these particles together and get everything from an Elephant to Cold Virus…from a piece of Wood to a Skyscraper… they all have these three particles!

It is the same with data structures in modern computer. You can build complex abstract objects that can hold any kind of data or information, but to the computer data is either a number or a non-number.

~cse 

Tags:

C#

Comments

11/3/2009 1:47:34 AM #

Payday Loans

This my friends is old news - try to update this.

Payday Loans United States

11/3/2009 12:43:37 PM #

online personal loans

Interesting post

online personal loans United States

11/6/2009 7:26:07 PM #

pay day loans

I like what I see. keep it going

pay day loans United States

Powered by BlogEngine.NET - Modified by cse 1.5.0.7
Theme by Extensive SEO Modified by cse
Content on these pages are my thoughts and ideas and are by statement under US Copyright(C) CSE. If you borrow my words, Give Credit! Under no condition whatsoever do my Words, Thoughts, Opinions, Comments, or Statements reflect those of any Person, Company, Nation, or Living Creature upon this Planet or any other Planet in the known Universe or any Parallel Universe whether currently in existent or not. As such, I do not make warrenty as to anything written in the content of these pages. Use at your own risk!

Head Liner

Welcome to the new Blog upgrade... brought to a local Internet by me ScottGeek.