Archived

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

C++ Arrays

This topic is 5246 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im reading the book Practical C++ Programming by Steve Oualline and I am stuck on chapter 5. The Chapter is about Arrays but I read it like 10 times I still don''t get it. Can somebody explain what an Array is when Array''s are used and how to use it. I know how to declare it but Im stuck on what the hell this things do. Also does anyone know of any free C++ books online. I read Thinking in C++ but it requires knowldge in C and I have none of that.

Share this post


Link to post
Share on other sites
Im not the best person to describe array's but ill see what I can do.

Arrays are very useful in programming from holding integers to allocating memory from text files and maybe more. A little description, one is you cannot define an array with an index of 0 as in:
quote:

int myArray[0];


That will give you a compiler error stating that it cannot allocate an array of constant size 0. So please be sure to initialize your arrays with a number greater or equal to 1. Also int arrays are a little better to cope with than char arrays. Char arrays are where most people get stuck. Keeping it simple now, integer arrays can be used in this manner:
quote:

int myArray[3] = {1, 2, 3};


Where myArray[0] = 1, myArray[1] = 2, and myArray[2] = 3 and myArray[3] equals nothing so far. You can also define them outside of the initialization:
quote:

int myArray[2];
myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 3;


That just covers integer arrays. Also integer arrays can equal of int arrays:
quote:

int array1[2], array2[2];

array1[0] = 3;
array2[0] = array1[0];


That means array2[0] equals 3 since array1[0] equals 3. Im hoping this is starting to make sense. Ill make another post soon about char pointers since I dont want this post to long.

I hope this helps.

[edited by - BlueDev on August 5, 2003 7:09:34 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I kinda get it now thanks. O ya I have one more question is it possible to use arrays to make a program which takes hours and minutes and turns them into minutes. EX

1 hour and 30 minutes = 90 minutes

Or should I just use variables to do this.

Share this post


Link to post
Share on other sites
I kinda get it now thanks. O ya I have one more question is it possible to use arrays to make a program which takes hours and minutes and turns them into minutes. EX

1 hour and 30 minutes = 90 minutes

Or should I just use variables to do this.

Share this post


Link to post
Share on other sites
You should check out the C++ tutorials at
http://www.gametutorials.com/
There''s one on arrays (page 2).

To convert from hours and minutes to just total minutes, an array probably wouldn''t be used unless you had alot of values to store that needed converting.

So if you just had
int hours = 1;
int minutes = 30;

Then you could just do
int total_minutes = hours*60 + minutes;


Tadd
- WarbleWare

Share this post


Link to post
Share on other sites
i would think using variables would be easier(but im not always the best when it comes to how do make things work well) but unless your getting multiples input on the hours and minutes at a time it would be best to run it through a loop until their done, using the same virables for hours and mintues. Then at the end of the loop you can do the calculations and store them into an array...heres 2 examples

these are basic stuff thought...not really fully compatable with API

first example


int hour, min;
int result[10];

for(int a = 0; hour != -1; a++)
{
cout <<"enter Hour: ";
cin >>hour;
cout <<"enter mins: ";
cin >>min;

result[a] = (hour*60)+min;
}


now the variable result will hold 10 ints (0-9). THis loop will continue till the user enters -1 for the hour;

now if you wanted to do use arrays to do this it would look somewhat the same..(using parrell arrays)



int min[10], hour[10];
int result[10];
int blah = 0;

for(int a = 0; hour[a] != -1; a++)
{
cout <<"enter Hour: ";
cin >>hour[a];
cout <<"enter mins: ";
cin >>min[a];
}

while(hour[blah] != NULL)
{
result[blah] = (hour[blah]*60)+min[blah];
blah++;
}


now this stores all the input into seperate arrays and then

i might of made an error or 2(correct me if i did)

hope this helps

[edited by - Rydis on August 5, 2003 7:27:55 PM]

[edited by - Rydis on August 5, 2003 7:30:30 PM]

Share this post


Link to post
Share on other sites
Now onto Char Arrays, they have alot more ways of getting around than int arrays. Since characters have so many different characteristics lets start with the simple char array first. This char array can only hold one string of how ever many lines you define so lets say 255 lines max:
quote:

char myArray[255];


No with that you cannot do something as simple as 'myArray = "hello"' because you will get an error that it cannot convert from 'char [6]' to 'char [255]'. Now the way to succesfully do this is to copy the string which in this case we'll use strcpy().
quote:

strcpy(myArray, "hello");


That code works just fine when dealing with char arrays since the string is decided on how long the users string is and how long the initialized string max is. strcpy() deals with all that for you and you have no worries. If having problems with strcpy() please include 'string.h' or 'windows.h'

Now that that is taken care of lets move to char pointers. I like to bring this up since it also confuses some people when talking about char arrays. A char pointer looks something like this:
quote:

char* pChar;


No array number, no problems with a equal sign. This is what I call the automatic size finder, hehe. What it does and you dont have to do is you can actually do something like 'pChar = "hello"' and it will not give you an error. Though you might run into more problems when coding certain programs when using pointers, but they are still good in most cases I just prefer char arrays more.

Nest for char array holding for whole strings in each allocated array we will need to do something differently:
quote:

char* pChar[255];


Yes you are thinking correctly, you can very well make pChar[0] equal "hi" and pChar[1] equal "bye" but its not a simple proccess as 1 2 3. To break it down you'll need some experience or at least need to know how to use the 'new' and 'delete' operators. Now the way I operate my char arrays are shown in the following:
quote:

char data[255];
char* pChar[5];

strcpy(data, "hello");
pChar[0] = new char[strlen(data)+1];
strcpy(pChar[0], data);

strcpy(data, "bye");
pChar[1] = new char[strlen(data)+1];
strcpy(pChar[1], data);

// and so on and so forth


Please be sure to include 'string.h' when using strlen() and strcpy(). Also be sure to delete the memory allocated from the char arrays once you quit your program as the following:
quote:

for( int x = 0; x < 4; x++ ) {
delete [] pChar[x];
}



I hope this brings a whole new light to arrays and if you have any more questions feel free to ask.



   BlueDev Net

[edited by - BlueDev on August 5, 2003 8:33:29 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Rydis
snipped - Rydis' examples



Just a note, those examples allow the user to continue entering data beyond the size of memory allocated for the array (10 int's). This points out one of the things to be careful with for arrays. You need to be aware of the memory you have allocated and be careful when indexing into arrays. Standard Template Library (STL) vectors, lists, etc. are useful to help avoid those problems. That's probably more advanced, but just wanted to point it out.


Tadd
- WarbleWare


[edited by - reana1 on August 5, 2003 8:24:08 PM]

Share this post


Link to post
Share on other sites
Thanks for all the help everyone. Finally I understand what an array is and I can understand the chapter in the book. I made this 2 codes they both work but the last one has this werid thing if you guys can plz help me fix that thing.

Heres the first codeWorks Perfectly)

#include <iostream>
#include <stdlib.h>

int main()
{
float Numbers[5]; //Some random numbers we will be using

Numbers[0] = 21.3;
Numbers[1] = 21.4;
Numbers[2] = 50.0;
Numbers[3] = 34.1;
Numbers[4] = 2.3;

cout << "The total is: "
<< Numbers[0] + Numbers[1] + Numbers[2] + Numbers[3] + Numbers[4]
<< endl;

cout << "The average is: "
<< (Numbers[0] + Numbers[1] + Numbers[2] + Numbers[3] + Numbers[4]) / 5.0
<< endl;

system("PAUSE");
return 0;
}


Here''s the second codeIt works perfectly except when it display the the name there is a letter E at the begining. Why?It says your full name is EX: EBrian Johonson. It is supposed to be Brian Johonson but it says EBrian Johonson)


#include <iostream>
#include <stdlib.h>
#include <string>
int main()
{
char full_name[100],first_name[100],last_name[100];

cout << "Please enter your first name: ";
cin >> first_name;

cout << "Please enter your last name: ";
cin >> last_name;

strcat(full_name, first_name);
strcat(full_name, " ");
strcat(full_name, last_name);

cout << endl << "Your full name is: " << full_name << endl;

system("PAUSE");
return 0;
}

Share this post


Link to post
Share on other sites
A couple issues with those examples (such as the possibility of jumping beyond the bounds of your character arrays, etc), but the main problem (I believe) is that you are not initializing your strings (in particular, your full name string).

When you first create a string in C, it could contain anything...garbage, data previously occupying that memory, etc. The strcat function will append to strings, but even though YOU know full_name is a string, you haven''t set it to an empty string - and that ''E'' you see, is the garbage.

Try using strcpy(full_name, "") prior to your strcat statements. That should fix it (haven''t got VC++ installed just yet so I can''t test it).

Besides that, I highly suggest you make sure on your strcat functions that you are not going beyond the bounds of your arrays (either by checking the length with strlen or using strncat to only concat a certain number of characters.

Hopefully that helped somewhat.

Cheers

Share this post


Link to post
Share on other sites
Well I tried your program in dev-c++ and it works correctly even if you don''t initialize the char array, I''m not saying it''s the right way to do it but it works on that compiler. Just remember you should clear the string before using it or you''ll encounter problems like that...

Share this post


Link to post
Share on other sites