It appears you have not yet registered with our community. To register please click here.

Origin XT RPG Network Home







XT Project Introduction
Blah blah intro to this place. To be composed later.

 

C++ - Tutorial, Lesson 3, Loops and Arrays


Mar 8 2007, 07:00 PM (Post #1)
Well why can't we do the shuffle?!
* * * * * *
Posts: 1,679
Cash: 183,455 / 0
Group: Nobility
Joined: 7/12/03 08:49 PM

Lesson 3 - Arrays and Loops


Welcome to the third lesson! If you're interested in learning about loops and arrays, then you've come to the right place!

What is an Array?


An array is what it says: an array of data of the same datatype. It is declared with any datatype (int, float, double, char, string, etc...). So say you had 20 integers that needed to be held by the program. Instead of declaring something like int a, int b, int c, int d...etc. , you would declare int numbers[20]. The number inside of the brackets equals the amount the array can hold. Remember that the place holder in an array starts at 0. So if we want 20 numbers, then the place holders will go through 0-19. Let's see an array in action.

Example One


CODE

#include <iostream>
using namespace std;

const int NUMBERS = 10; //this is a global variable, we will discuss the difference
//between global variables and local variables when we
//discuss functions and classes

int main()
{
int holder[NUMBERS]; //here is our array, it has a place holder for 10 total
//numbers, because NUMBERS = 10. We can use
//NUMBERS here because it is a global variable

/*The section coming up will ask for numbers 10 times. There are better ways
to implement this, however we haven't gone over it yet, but we will later in
this lesson.*/

cout<<"Input a number."<<endl;
cin>>holder[0];
cin>>holder[1];
cin>>holder[2];
cin>>holder[3];
cin>>holder[4];
cin>>holder[5];
cin>>holder[6];
cin>>holder[7];
cin>>holder[8];
cin>>holder[9]; //we end here, because we have 10 numbers total (0-9)

cout<<"Your numbers were: "<<holder[0]<<" " <<holder[1]<<" "<<holder[2]<<" "<<holder[3]<<" "<<holder[4]<<" "<<holder[5]<<" "<<holder[6]<<" "<<holder[7]<<" "<<holder[8]<<" "<<holder[9]<<endl;

//This is rather tedious isn't it?

system("PAUSE");
return 0;
}

You can save yourself some time and spare yourself from frustration by copying and pasting this code. All you do is enter a number, hit enter, then keep doing the same process until it displays your numbers. Arrays are useful, however, doing this is rather tedious, but when we talk about loops we can make this process much easier.

Arrays can hold more than one block. What I mean by this is that instead of having int holder[0], you can have int holder[0][0]. This is called a two-dimensional array. You can also have int holder[0][0][0] or even int holder[0][0][0][0]! These are called multi-dimensional arrays, and are often used to mess around with pixels, which is more of an advanced thing. Now, to save myself some time and some headaches, I'm not going to use a code with two-dimensional arrays until we talk about loops.

What are Loops?


Loops are code used to repeat a certain piece of code over and over until a certain condition is met. There are a few different kinds of loops, and we'll discuss them all here.

The "For Loop"


Using a counter, for loops can be quite handy. I will discuss them first because they are by far the most useful to use with arrays. Let me show you an example of how they are used.

Example Two


CODE

#include <iostream>
using namespace std;
const int NUMBERS = 10;

int main()
{
int i; // this is used as a counter variable. It will make more sense to you
//when you see the loop

int holder[NUMBERS];

for(i=0; i<NUMBERS; i++)
{
cout<<"Give me a number."<<endl;
cin>>holder[i];
}

for(i=0; i<NUMBERS; i++)
{
cout<<"Position " <<i<<": "<<holder[i]<<endl;
}

system("PAUSE");
return 0;

}

Copy and paste this baby and see what it does. As you can see, it's much more efficient, shorter, and cleaner.
CODE
for(i=0; i<NUMBERS; i++)


"for" declares the for loop so the compiler knows that you're going to make a loop. Now we'll take the stuff in the parenthesis one by one. i = 0; is just declaring i to equal 0. This is important, because arrays start at 0, so we do not want to skip that place holder.

i<NUMBERS; tells the program when to break the loop. The loop will keep going until i is no longer less then NUMBERS. Since NUMBERS = 10, we can get 10 numbers this way because the loop will go through place holders 0-9, but will not reach place holder 10 because i would no longer be less than NUMBERS.

i++; adds 1 to i every time the loop runs. This is important so i will eventually not be less than NUMBERS, and the loop can break. It's also important as a counter, as you will see in the next few lines of code.

You know what the cout does, obviously, but it's the cin>>holder[i]; that might confuse you. This is one of the reasons why i++; is so important. When the loop runs through the first time, i = 0, so the number you input will be stored in holder[0]. The next time through, i = 1, so the number you input will be stored in holder[1]. This will keep happening until i = 10, 10 is not less than 10, so the loop will break. This allows us to store numbers in place holders 0-9, which is 10 total numbers, as we wanted.

The next loop does pretty much the same thing, now that I've explained to you how this loop works, I'm sure you can figure out how the next one works.

Ok, now that we've got that one down, you can do the same thing with two-dimensional arrays as well as multi-dimensional arrays. They can be rather useful. Now let's discuss some other loops.

The "While Loop"


This loop is pretty useful, although I don't think it's quite as powerful as a for loop because it doesn't support the same kind of conditions. It only will loop through something while a certain condition is true. Let's look at an example.

Example Three


CODE
#include <iostream>
using namespace std;
const int NUMBERS = 10;

int main()
{
int i = 0; //we have to specify it here, because we cant do it in a while loop
int holder[NUMBERS];

while(i != 10) //the != here means does not equal
{
cout<<"Give me a number."<<endl;
cin>>holder[i];
i++;
}

i = 0;

while(i != 10)
{
cout<<"Position "<<i<<": "<<holder[i]<<endl;
i++;
}

system("PAUSE");
return 0;

}

As you can see, this practically does the same thing, except extra code is needed for initialization of our counter variable and to increment it. I don't think that while loops are as flexible for these purposes as for loops, but they're still useful.

The "Do-While Loop"


I also want to mention do-while loops while we're on the subject; these do practically the same thing as while loops, but in reverse order. They do something while a certain condition is true. The code is a tad bit different so I'll show you how it looks.

Example Four


CODE

#include <iostream>
using namespace std;
const int NUMBERS = 10;

int main()
{
int i = 0;
int holder[NUMBERS];

do
{
cout<<"Give me a number."<<endl;
cin>>holder[i];
i++;
}

while(i != 10); //notice that the while part comes after the brackets close for the
//do part, I guess things are more organized this way.

i = 0;

do
{
cout<<"Position "<<i<<": "<<holder[i]<<endl;
i++;
}

while(i != 10);

system("PAUSE");
return 0;

}

I suppose it is up to you as to which loop you wish to use. Certain loops are better for certain situations than others, but it also has to do with your programming style. Try them out to determine your style.

Memory Allocation of Arrays


Before I end this lesson, I want to say one more thing about the memory allocation of arrays. If you declare an array like: int holder[10], even though it's an int variable, it will take up more than 4 bytes. This particular array will take up 40 bytes of memory. You find this out by taking how many bytes the variable type takes up, in this case it's 4, and multiply it by how many spaces the array can hold, in this case it's 10. So 4 x 10 = 40. So, you must be careful not to make arrays TOO big. With multi-dimensional arrays like this: int holder[10][10][10], you do practically the same thing to find out how much memory it takes. It's just 4 x 10 x 10 x 10 = 4000 bytes. In these days, it'd be pretty hard to overload your memory since most computers these days come with at least 512 MB of RAM, and with Windows Vista out alot of them come with 1024 MB of RAM, so unless you declare lots of arrays that are REALLY big, you shouldn't have too much of a problem.

Virus Alert


Wanna make someone's system fry? Just declare some REALLY big arrays. It's easier to use the double datatype for that sort of thing.

Afterword -


That's all for this lesson! See you next time!

Next Lesson:
Lesson 4 - Conditional Statements and Switches

~Nesticles~

This post has been edited by Jinghao: Jun 16 2007, 01:00 AM
Post Options