Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

Lesson 2



Arrays
Arrays are multiple variables of the same data type. Eg: You could have an array of integers which could hold 10 different values. Arrays are used lots, they save space and are very useful within loops. To define an array you put [number] after the variable, the number being how many variables you want of that data type.
A basic example is shown below of an array of 10 integers.



#include <conio.h>
#include <iostream.h>

void main()
{
	int Number,Index,My_array[10];
	cout << "Enter a number between 0 and 9: ";
	cin >> Index;
	cout << "Enter an integer (any number): ";
	cin >> Number;	
	My_array[Index]=Number;
	cout << "Your number was: " << My_array[Index];	
	getch();
}
Firstly I defined Index which was the index of "My_array[10]", this "Index" would be entered by the user and could have a value between 0 and 9, because arrays start at 0, the 10th element is a null character, this is not important for us, but you must get used to arrays starting from 0 instead of 1.
Next to put a value into 1 of the array variables, you write Array[index] = something. Althrough in my example i got the user to directly type in the variable "Number" for the array, then i put the number into the array index, this is just an alternate method.
An example of using a direct reference to a piece of the array is shown below.
Example 2:

#include <conio.h>
#include <iostream.h>

void main()
{
	int Number, My_array[10];
	cout << "Enter a number: ";
	cin >> Number;
	My_array[3] = Number;
	cout << "Your number was: " << My_array[3];
}
In this one, the user types in a number, which will be assigned to the 4th element in the array. Yes it looks like the 3rd element as there is a 3 for the array index, but remember it starts from 0, therefore 0,1,2,3,....... the 3 is the 4rd element in the array.
It is also possible to define and give a value to an array like this int My_array[10] = {5,10,76,35,98,23,62,87,45,58}
The physical representation of this is shown here:

int My_array[10] = {5,10,76,35,98,23,62,87,45,58}
Index 0 1 2 3 4 5 6 7 8 9 10
Values 5 10 76 35 98 23 62 87 45 58 Null

Click the values in the above table for an explanation.

Strings
You can have any data type for the array, for example you could have an array of floats, or boolean. But now its onto strings.
A string is a word, like a name or address. They are made up of an array of characters, so to define one we will use this example.
Example:

#include <conio.h>
#include <string.h> 
#include <iostream.h>

void main()
{
	char Name[30];
	cout << "Enter your name: ";
	cin >> Name;
	cout << "Your name is: " << Name;
	getch();
}
First there is 1 new #include file - string.h this is the libary for handling yes you guessed it strings.
The string Name[10] is simply a character array of 10 elements, which means that your name could be up to 10 chars in length. The array element 0 in this case would be your first initial. Say i was to put Name[0] instead of just Name eg: cout << Name[0]; this would output your first initial as it was the first element (character in this case) of the array Name.
If the user typed in JAMES, then the array would look like this:


char Name[10]="James";
Index 0 1 2 3 4 5 6 7 8 9 10
Values J A M E S - - - - - Null

Click the values in the above table for an explanation.





String functions
DefinitionExample
stricmp(compare_this, to_this);Me=stricmp(Name,"BILL"); - this is not case sensitive, Me will equal 0 if true,
strcmp(compare_this, to_this);strcmp(Name,"Bill"); - this is case sensitive, note there is no i in strcmp
strlen(string);Name_length=strlen(Name);
strcpy(copy_to, From);strcpy(Name, "Harry"); - copies "Harry" to Name, you can also copy variable to variable


Loops
Loops are a huge time saver in programming, they do repeatitive tasks and save alot of coding.
Two kinds of loops the for(start_value;condition;update) and also the while(statement true)
The for loop is normally a definate condition like index<5, and the while is normally something like while(Exit==false)
Example:

#include <conio.h> 
#include <iostream.h>
void main()
{
	int i;
	for(i=0;i<10;i=i+1)
	{
		cout << "Hello" << endl;
	}
	getch();
}
This will print out 10 hello's on the screen. Like any loop, it has an opening bracket { and a closing one }
The integer variable "i" was first set as 0, the condition was it must be less than 10 "< 10" for the loop to work, and everytime the loop was executed i was updated up 1. Next is the same example but with a while loop.

#include <conio.h>
#include <iostream.h>

void main()
{
	int i=0;
	while(i<10)
	{
		cout << "Hello" << endl;
		i=i+1;
	}
	getch();
}
Now ill show you why loops and arrays go well together. Say you had an array of 20 integers and wanted to set them all to 25, you could write out
numbers[0]=25;numbers[1]=25;.............numbers[19]=25; but that would take forever, so we use a loop.
Example:

#include <conio.h> 
#include <iostream.h>

void main()
{
	int i, numbers[20];
	for(i=0;i<20;i++)
	{
		numbers[ i ]=25;
	}
}


Everytime the loop runs, whatever the value i is, the array element i will be set to 25. So when i is 0, the variable numbers[0] will be set to 25 and so on.
You can do this with any array of any type. Next i will go over some shortcuts for C++ programming.

A "++" after a variable will increase its value by 1 everytime it is read, likewise a "--" after it will decrease its value by 1.
Say i wanted to increase the variable i by 1, like in the loop example above, i would write i++. Simalaly if i wanted to decrease its value by 1, i would put i--. This is just a shorter way of writting i=i+1 or i=i-1 . If you want to increase or decrease its value by more than 1 each time its read, you can put i+=3; or i - =3; this will increase or decrease the value of i by 3 everytime its run, its also short for writting i=i+3; or i=i-3;



Operators
SymbolDefinitionExample
< Less Thanif(Number<5)...
<= Less Than or Equal toif(Number<=5)....
> Greater thanif(Number>5)....
>= Greater than or Equal toif(Number>=7)...
= = Compare (Equal to)if(Number==3)...
! Notif(number!=3)...
%The remainderNumber=15%3 , Number will now be 0
/DivideNumber=15/3 , Number will now be 5.
*MultiplyNumber=10*3 , Number will now be 30.
+PlusNumber=10+5 , Number will now be 15.
-MinusNumber=10-5 , Number will now be 5.
&&Andif((Number==3)&&(Number!=5))...
||ORif((Number==3)||(Number==4))...


Random numbers
Random numbers are any number at any time, like throwing a dice. A simple dice example - dice = rand() % 6 this will give the variable dice a random value between 0 and 6, also you must put randomize before you use rand and also include time.h.

Example of rand() function - DICE

#include <conio.h>
#include <iostream.h>
#include <time.h>			//New include file - time.h

void main()
{
	int dice;
	randomize();
	dice=rand()%6;			//Get random dice value
	while(dice<=0)			//A dice has no 0 value, so get another value
	{
		dice=rand()%6;
	}
	cout << "The random number is: " << dice;
	getch();
}
This will give number a random value between 0 and 30, then output it to the screen.

If statements
Finally to end this lesson, i will teach you if's, already you saw them above, they are a condition, if it is true the statement between the 2 brackets will be executed.
Example:

#include <conio.h>
#include <iostream.h>

void main()
{
	int Number;
	cout << "Please enter a number: ";
	cin >> Number;
	if(Number>3)
	{
		cout << "Number is greater than 3";
	}
	else
	{
		cout << "Number is Less than or equal to 3";
	}
	getch();
}
You can mix operators also to make more complex statements.
Example 2

#include <conio.h>
#include <iostream.h> 

void main()
{
	int Number;
	cout << "Please enter a number: ";
	cin >> Number;
	if((Number!=6)&&(Number>=4)&&(Number<9))
	{
		cout << "Number is not 6, and is between 4 and 8";
	}
	else
	{
		cout << "Number is either 6, or less than 4, or greater than 8";
	}
	getch();
}
Well if you made it this far and still understand, congratulations, else go back and email me.
If your feeling confident, try the next Quiz - Click here


Back Home Next