Chapter 4 - Arrays
Created By Dr4g
http://dynamichell.com

Numerial Arrays;

Numerical, Meaning the array having a data type of Int - Float - Short - Long.

What is an Array ?
An Array is like a variable, with many values. which have an index of consecutive elements,
starting from 0 to the end of the array.
 Why Would We Use An Array ?
We would use an array because instead of having lots of different variables,
Example:
int number0;
int number1;
int number2;

And So On...
We would have,
int number[5];
Meaning it would list from 0 to 4
we can refer to each of these variables seperately by doing,
number[0] = 5;
number[3] = 2;

Just like in the For Loop Example.
we can set the index to i, remember i? it was being incremented by 1 every time by using the ++ operator.
Well the following is an exammple of taking in values and storing them in the array.

Example

#include <stdio.h>

int i, number[5];

int main()
{
	printf("Please Enter The Numbers of Your Array One By One\n");
	scanf("%d", &number[0]);
	for (i=1 ; i < 5 ; i++)
	{
		printf("\nPlease Enter Next Number\n");
		scanf("%d",&number[i]);
		printf("The Value of i is %d \n", i);
	}
}

You can now see how we declare an array as an int data type and set the amount of elements in the array.
on the First scanf function we took in the very first element of the array, rememebr arrays start from 0 NOT 1

We Created it to loop 5 times so it can take in values 0 - 4 ( number[0] - number[4] ).
And because we had already took in the first value of the array [0], i is now 1 and NOT 0. Hence i=1

It will take in number[1] - Display the value of i NOT the value being inserted into the element of the array.
Then increment i by 1 so next time it loops i will be equal to two (i=2).
Then take in number[i], so it looks for i and finds out it is 2 and then takes in number[2], and 3 - And So On..

Notice how i have indented my code their, now you see how easy it is to get lost with all our braces..
Indentation helps this problem :o)

Character Arrays
One of the main uses for arrays, is to store words, (names..etc)
This arrays works almost the same to the numerical one above.
However, ofcourse, were dealing with letters, not numbers.

How To Do This:
char name[5];
or
char name[5] = { 'H','e','l','l','o' };

then from this we could do;
printf("First Letter %c -- Last Letter %c", name[0],name[4]);

The output of this would be:
First Letter H -- Last Letter o 

If we did not assign values to the array we could do
scanf("%c",name[4]);
This would insert the input into the last element of the array, number 4.
We could also use i as an index rather than a value like 4.
So we could do the same as the numerical example above.

for ( i=0 ; i < 5 ; i++ )
{
	scanf("%c", name[i]);
	printf("The Current Location of the Array is %d", i);
}

Aight This is My Introduction To C Chapter on Arrays Finished
As this is an introduction i will go in depth, in arrays later on in further chapters
I am going to cover multi-dimensional arrays.
Previous chapters that you do not think i have went into as much depth as i could.
Let me know, however, i am aware of this as i want this to be an intro, not a 'throw-you-in-at-the-deep-end-tutorial' :o)
Thanks For Reading this chaper of my tutorial :o)
Next Chapter :: File I/O