Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    c
  » Arrays in C/C++
      by Victor (Rotkiv)
 Page 1 of 1 
   

(Login to remove green text ads)
Part 1
Part 1

Hi, my name is Victor (Rotkiv) and I'm going to try to instruct you in the ways of the uber coder ninja monkeys. I'm not an experienced programmer at all, but hopefully you don't know how to use an array yet so that this tutorial will be useful to you.

Arrays are very important in any programming language. They give a program the ability to access a set of variables without the programmer having to specify every call or action himself. Imagine a parent that has 20 kids, how's she going to remember all of their names? So she names them after herself and gives them shirts with their own number on it. That way she can call individual ones by the number on their shirt. This analogy is supposed to infer the idea that these 'children' could be accessed by loops, but that just wasn't working.

Now to the how:

Declaration: Arrays are declared exactly like a variable. The only thing different is that there's a post fix on the end of the variable name.

Code:
int a_variable; int an_array[];
But you have to specify a size. Size determines how many variables are in your array. so if you declare:

Code:
int array[10];
You essentially have 10 different variables, so far, each without a value.

Initialization: You probably know that you need to initialize a variable before it's used, if you didn't know that, you need to. You can give arrays values in any way you can give variables values.

Code:
int array[10] = {0,1,2,3,4,5,6,7,8,9};
Now all of your variables have a value 0 - 9. The curly braces are important.

A very important thing to know is when you declared an array of 10 variables, they were not named array[1] to array[10]; they were named array[0] to array[9].

Initializing an array like that is extremely boring and it's uses are very limited. So we use an arrays best friend, the loop, to make it more interesting.

Code:
int myarray[10]; for(int x=0; x<10; x++) { // myarray 0-9, therefore the < 10 myarray[x] = x * x; // x squared } // end the loop

So our value of myarray[0] - myarray[9] is respectively 0,1,4,9,16,25, etc. until we get to the square root of 9; but, therein lies another problem. There's no output of the value we gave it. We could print like this:

Code:
cout << myarray[0] << myarray[1];
No, as programmers we refuse to do things like that, there must be an easier way! And I know that your not stupid and know that there is an easier way. We just used it:

Code:
int myarray[10]; for(int x=0; x<10; x++) { myarray[x] = x * x; // x squared cout << myarray[x] << endl; // print x and a new line. } // end the loop


So I hope this tutorial has helped you understand how useful these things are.

Play around with them, get to know them a little, but most of all, wait for part 2.

Yes there's more; much, much more.




 
 Page 1 of 1 
   

Rate This Article
1 2 3 4 5 6 7 8 9 10





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle