Tuesday, February 24, 2009

Introduction to Programming :: Using Functions

A function is a group of statements that is executed when it is called from some point of the program. The structure of a function call is as follows.


type name ( parameter1, parameter2, ...) { statements }
  • type :is the data type specifier of the data returned by the function.

  • name :is the identifier by which it will be possible to call the function.

  • parameters :Each parameter consists of a data type specifier followed by an identifier, like any regular variable declaration and which acts within the function as a regular local variable. They allow to pass arguments to the function when it is called. The different parameters are separated by commas.

  • statements is the function's body. It is a block of statements surrounded by braces { }.

In this small program the function subtraction() is being called by the main(). A serious of couts are being used to show different uses of the function call.




If a function does not return any value then the type would be void.


Monday, February 23, 2009

Introduction to Programming :: Switch Statements

The switch statement is a very useful structure in that it will allow for user input to control the flow of the program. In a switch statement there can be multiple cases. Each case can do a particular function. This is beneficial because a program may only need to do a certain operation every other run time. This example code below is from a binary tree program. This example shows a do-while loop controlling the menu and how long the switch statement will last. While the input from user is not 6, the loop will continue to print the menu to the screen and allow for another case to be selected. Much of the following code was left intact in order to show an example of a type of program all computer science majors should expect to see. The focal points of the program are the do-while loop and the switch statement within that loop.

Thursday, February 19, 2009

Jailbreaking your iPhone

I recently came across an article online that discussed the legality of jail breaking your iPhone and whether or not Apple should have the right to protect its property. I look at this as a case of Apple being overzealous in regards to protecting their product. However, as far as consumers are concerned they bought a piece of hardware. It should be the consumer’s property and their right to do what they like with it. If similar applications are available for download for free from an open source, then why should the consumer pay more money for a program that is sub-par that Apple created? If Apple were to slap a label on their product saying, "pay more get less" then fine fair warning was given, but when you have to pay a ridiculous amount of money for a product that has shown to break within months, owners should have access to applications at a fair rate.
The bottom line is that if a customer buys any machine they should be allowed to modify it without being told otherwise. If the operating system for the iPhone isn't preferred or any phone for that matter, then why shouldn't owners be allowed to install an open source operating system. Modifying the type of operating system is not costing Apple anything, unless consumers are downloading pirated applications. As long as no illegal activity is being performed the consumer should have every right to change their property.

Friday, February 13, 2009

Introduction to Programming :: Even More Loops


Do-While Loop


Shown above is a simple do-while loop. The basic concept of a do-while is to do { a set of instructions } until a condition has been met in the while (). In the above example the user is prompted to enter a number. The do-while loop will continue until the user inputs the value of 0. This is a good form of controlling the flow of your program because it can be used to execute a set of instructions until the end of an input file for example.

For Loop


This block of code is a utilizing an import and very commonly used loop for beginner programmers. The for loop is commonly used to increment a set of instructions a specific amount of times. In the above code it states (int n=10; n>0; n--) what this is doing is declaring n=10 and then decrementing until n is no longer greater than zero. An easy way to look at a for loop is for(initialization, condition, increment) statement. Each time through the loop the value of n is show on the screen. At the end of the loop the word "FIRE!" is show on screen. Incrementing a counter is essential for most programs.

Monday, February 9, 2009

Introduction to Programming :: Conditional Statements and Loops

Conditional Statements are used to control the direction of you program. There are many strategies for controlling you program. Most commonly used are if statements. An if statement pseudo code would be; if (a){instructions}. The (a) is the condition that has to be met in order for the {instructions} to proceed. Often there are times when you need to designate what will happen if the conditions are not met by using else.


Another form of controlling the direction of a program would be loops. Loops are used to repeat a statement a specific number of times or until the condition is met. The next example shows a very simple while loop. The loop begins by prompting the user to input a starting number. The loop then compares the value of x with the conditional statement (x>0). While (x>0) the loop will continue and decrement the value of x each iteration.


The for loop and the do-while loop will be discussed in the next post.

Thursday, February 5, 2009

Introduction to Programming :: Declaring Data Types

The syntax to declare a new variable is first write the desired data type and then the variable name.

Variable Names

-char
-short int
-int
-long int
-bool
-float
-double
-long double

The syntax to declare a new variable is first write the desired data type and then the variable name.
ex: int a;
float number;
string LastName
In cases of multiple variables of the same type its possible to declare them in one statement.
ex: int a, b, c;



Another aspect of declaring variables needs to be taken into account. The variables char, short, long and int can be either signed or unsigned, depending on the values that need to be represented.


Global variables can be referred from anywhere in the code, even inside functions, whenever it is after its declaration.

The scope of local variables is limited to the block enclosed in braces ({}) where they are declared

Tuesday, February 3, 2009

Introduction to Programming :: C++

Generally the first thing students will write in any programming language will be "Hello World." The way to begin a C++ program is to first set up the structure of the program by writing the #include statements. An example would be iostream. This is the basic input output library in C++. Following the include statements add using namespace std;. Every element of the standard C++ library is declared within a namespace. Next write the beginning of the executed code. int main() is recognized to be the beginning of the executable code. Now that everything is set up coding can begin on the "Hello World" program. This following code uses the items mentioned above and cout which will be explained more in depth in future blogs but for the time being it is a simple way to output to the screen.

#include
using namespace std;

int main()
{
cout<<"Hello World"<<endl;
return 0;
}

The program should look similar to this. The only thing that should be displayed on the screen is "Hello World." A good rule to follow when writing programs is always write comments using // " comment here " while working on the code. This will help keep the program organized and easy to understand for the user.