Archived

This topic is now archived and is closed to further replies.

jcdenton999

Arrays, Variables, Constants...?

Recommended Posts

I''m having problems...can someone tell me what the difference is between an array, a variable, and a constant. Do they not all do the same thing? When would you use a array rather than a constant etc. Also, are functions absolutely necessary in a program? Can you not get round a problem without using a function? As you can probably see, I am totally new to programming, and I don''t get much time to learn what with going out with friends and playing football. So any help will be appreciated thanks

Share this post


Link to post
Share on other sites
Variables: They hold one instance of a data type, for example, one integer or one floating point number.

Constants: A pre-defined value. A constant can''t change during program execution.

Arrays: An array can hold multiple instances of the samy type, for example:

int MyInts[10];

...declares an array which can hold up to 10 integers. To access the integers stored in the array, simply do:

n = MyInts[4];

This will store the fifth (MyInts[0] is the first one) integer value in the array MyInts in the variable n.

You can''t program without functions. Functions help you structure your code, and without them it would quickly become an unreadable mass of "spaghetti code".


For crying out loud, she has fishes coming out of her head on either side. How can you find this hot?!

Share this post


Link to post
Share on other sites
A variable is a singular container. An array is a list of containers. A constant is a container that you cant open to change the contents.

Think about it as a box, you define a variable, you open one box, with no lid, so you can throw anything in as long as it fits. An array is a load of boxes all joined together, but they''re the same size, and you can throw stuff into any of the boxes as long as they fit. i.e. box[0] would be the first box, and box[6] would be the seventh box. A contstant is a box you can see into, but you cant, however hard you try, open it and change the contents. You can have constant arrays too.

Share this post


Link to post
Share on other sites
As for the functions problem, yes, you can technically get through a program writing only one function (the entry point function, i.e. int main(void) ) but in most projects you will encounter, its best to modularise your code into functions to save you having to write the same code over and over again. Its more of a logical thing than anything.

Share this post


Link to post
Share on other sites
I guess in a way they do sort of do the same thing, they all store data. The most basic form of data storage is a variable (like x + 2 = 5, x is the variable). A constant is a variable that cannot be changed in a program (the compiler won''t let you) so once it is initialized the data cannot be accidentally altered (quite handy at times). Lastly arrays. Arrays are slightly more complex. An array is a group of variables, stored in sequential order, that can be accessed using an index. For example in C++:

//create an array of 5 integers
int myArray[5];

//set the 3rd item in the array (0 is the first) to 7
myArray[2] = 7;

//set the second item to 0
myArray[1] = 0;

//print second and 3rd items
cout << myArray[1] << myArray[2];


These become handy when coupled with various loops. Say, for example you had to a list of 500 characters. This would be quite a pain to store and print without an array. Here we see it''s easily done (pretend the list is already filled):

for(int i = 0; i < 500; i++)
cout << myArray << " ";

That prints all 500 character with a space in between each one. It''s that easy!

Hope this helps.


--Sometimes the hardest thing to do is nothing

Share this post


Link to post
Share on other sites
Hi there jcdenton
Let's see if i can explain this correctly...
A variable is simply..hmm..well say like this

    

int i; // <- This is a definition of a variable, i is the actual variable ;)



An array, hmm, i guess you can call it a "list". A variable with "places" in it...aww, think i go for the source again


int i[10]; // Ok, this defines the variable "i" as an array with 10 places in it...



And about the functions...you don't HAVE to use them, but they sure as hell can make things easier. Say you want to print some lines of text to the screen. Either you copy-n'-paste, or you could use a function...


void function() // Ok, here is the actual function. We make it a void because we don't want to "return" a value from it ;)

{
cout << "Bla bla bla bla bla..." << endl; // text

cout << "More bla bla bla bla..." << endl; // more text...

}

void main() // Main

{
for (int i=0; i<10; i++) // For loop wich calls the function "function()" 10 times ;)

function();
}


Ok, not sure if this makes any sense to you, still rather new myself, but hey...I tried

Good Luck

EDIT: Uhh, fast people


[edited by - Android_s on May 2, 2003 1:15:48 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Ok lets get the basics down here.

A variable (singular) is an item you use in your programs to define some type of value. Basic variable types consist of ints, chars, and floats. Example: int Value;

Arrays are lists of variables that are all the same type. For instance: int ValueArray[5]; This created a list of 5 integer variables that you can access by index: ValueArray[0],ValueArray[1],ValueArray[2],ValueArray[3],ValueArray[5],ValueArray[5]. So in this case our array is 5 times the size of our regular Variable.

As for constants....

Constants are variables that can not be modified. const int Value = 5; Value can not be changed. You can not do Value = 4; if Value is a constant.

So none of the above are the "same" they are similar but have different aspects.

As for functions, no you can not make a C++/C program with out using at least one.

Share this post


Link to post
Share on other sites
quote:
Original post by jcdenton999
But if you cannot change a constant then what is the point of using them? Why not use variables all the time?


They are helpful for things like strings you dont want changed, c++''s answer to the old #define statement.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by jcdenton999
But if you cannot change a constant then what is the point of using them? Why not use variables all the time?


Well the objective of the const keyword is to help minimize bugs that can occur in your games by using normal variables.

Example:

char GameName[] = "Burnination";

GameName[5] = ''P'';

Now GameName would print out "BurnPnation". If you had done const char GameName[]= "Burnination"; the second line of code would have either not worked or caused a compiler error. Thus giving you the desired game name.

A more useful example would be something along these lines

int ArraySize = 5;
Enemy ArrayOfEnemies[5];

ArraySize++; // Increment ArraySize from 5 to 6.

for( int i=0;i< ArraySize; i++)
{
ArrayOfEnemies.state(Alive);
}


Now the problem with the above is that the array''s size is 5, so when it tries to go above 5 in the for loop your going to crash the program. A solution to the problem would be something along the lines of :

const int ArraySize = 5;
Enemy ArrayOfEnemies[5];

ArraySize++; // Increment ArraySize from 5 to 6.

for( int i=0;i< ArraySize; i++)
{
ArrayOfEnemies[i].state(Alive);
}


Const is used when you KNOW that you don''t want a value to accidentally change. The above examples are pretty basic but show off the concept well enough. What you also need to know is that these problems with normal variables can also be complicated by functions for instance....


void function( int &value )
{
value++;
}

now


int ArraySize = 5;
Enemy ArrayOfEnemies[5];

function(ArraySize); // Array size was just incremented from 5 to 6 again except this time it''s harder to find out how it happened.

for( int i=0;i< ArraySize; i++)
{
ArrayOfEnemies[i].state(Alive);
}


and again defining arraysize as a const int instead of a regular int would''ve stopped the problem from occuring.

Share this post


Link to post
Share on other sites
quote:
Original post by jcdenton999
But if you cannot change a constant then what is the point of using them? Why not use variables all the time?



So if there is a variable you don''t want changed, you can make it a constant so you don''t change it by accident(It''ll bring up an error). I''ve heard it can be handy when you use it in projects that you are woking on with other programmers.

Share this post


Link to post
Share on other sites