Saturday, September 11, 2010

C/C++ Continued

If you haven't read it yet, my first C tutorial should be read first by beginners. Last time, I talked about some basics of C. This time, I will talk about some more slightly advanced topics.

Lets say, you wanted to store one hundred account numbers in your program. To do this with normal means would mean typing out:
int Acct1, Acct2, Acct3, Acct4; //And so on ...
// Yes, you can define more than one variable at a time.
Needless to say, this would take a long time and you would have to reference them all separately. This is the reason we have arrays. An array simply is a simple way to store a lot of data within only one variable. To create an array holding 100 int variables, you would simply do this:
int Accounts[100];
Then to call a position in the array you would do this:
Accounts[0] = 2884756;
Arrays will always start at 0 and end one position less than the number you typed originally. In this case I typed 100 so the array indexes are from 0 to 99.

Since you can call a position on an array with a number, you can simply insert a variable holding a whole number inside of the brackets " [ ] ". (char, int, short, or long) This allows for much more versatility than having to specify each time which variable you wanted in your code. For instance, Lets say I wanted each number in the array to be set to 0. You could do the following:
int Accounts[100], x;
for(int x=0; x<100; ++x) // I'll talk about this in a minute.
{
    Accounts[x] = 0;
}
Or to make it simpler, when you create an array you can set a default value by using the curly brackets "{ } " around a value. Like this:
int Accounts[100] = {0};
You can also set all values by separating with a comma like this:
int Abc[3] = {1,2,3};

Now, in the last example I used a new command called for. for is one of the loop types available in C (as well as many other languages). for takes 3 parameters broken down into 3 steps: Initialization, Condition, and Increment.

Initialization is processed only once before the loop starts. (In the example, creating a integer variable "x".)
Condition is checked every time the end of the loop is reached. (In the example if x is less than 100.) If Condition is true, the loop repeats. If it is false then it doesn't (or breaks).
And, finally, Increment is processed every time the loop repeats. (In the example "++" causes the variable x to increase by 1. It is the same as typing "x=x+1")

Other types of loops are:
do while - 1 parameter; Condition. The do while loop always processes the first time regardless of the Condition statement. Use:
do {
   //Stuff goes here.
}while(x<100)

while - 1 parameter; Condition. Use:
while(x<100) {
   //Stuff goes here.
}

In all of these, the condition statement must return either true or false. This, in C as well as other languages is called a "Boolean" statement. To get this you need one of the Boolean operators:
== (equal)
!= (not equal to)
< (less than)
> (greater than)
<= (less than or equal to)
>= (greater than or equal to)

3 comments:

  1. this is perfect for my algorithms class, we're working on greedy algorithsm atm

    ReplyDelete
  2. I will add a great article on computer programming !

    Nice article !Supporting and please support knowledge,thanks:

    http://tetru.blogspot.com

    ReplyDelete
  3. Still quite similar to java. I'm picking this up quite quickly.

    ReplyDelete