Computer Forums

Search Tech-Forums.net

Member Login

Remember Me? Sign Up! | Forgot Password
 
 
Reply
Old 07-03-2006, 02:08 PM   #1 (permalink)
 
True Techie
Join Date: May 2006
Posts: 101
jonmon6691
Default Displaying arrays in c++

I am relativaly new to c++, i know most of the basics about programming but i dont know about any functions, and im wondering if there is a function to display the contents of an array and a function to return the length of an array
__________________
<small>
.:HP Pavillion dv6000:.
1.8 ghtz Intel Core 2 Duo
2GB RAM
120 GB HDD
nVidia Go graphics
Apache 2.2 (looking for a domain name)
</small>
jonmon6691 is offline   Reply With Quote
Old 07-05-2006, 01:15 PM   #2 (permalink)
Spartan's Avatar
 
Ultra Techie
Join Date: Jul 2005
Posts: 814
Spartan
Default

well, on the problem of displaying the contents of a array, you can just run a simple loop add 1 to a count variable, each time showing the contents of the array

Example:

void printarray (int number[], int length) <--calling this from the main program
{

for (int n=0; n<length; n++)
cout << number[n] << " ";
cout << "\n";

}

Sry if the code isn't 100% correct; the fundamentals are right.
__________________
Spartan is offline   Reply With Quote
Old 07-05-2006, 02:55 PM   #3 (permalink)
 
True Techie
Join Date: May 2006
Posts: 101
jonmon6691
Default

is there a way to return the length of an array so i dont have to keep track of a variable with it?
__________________
<small>
.:HP Pavillion dv6000:.
1.8 ghtz Intel Core 2 Duo
2GB RAM
120 GB HDD
nVidia Go graphics
Apache 2.2 (looking for a domain name)
</small>
jonmon6691 is offline   Reply With Quote
Old 07-06-2006, 09:59 PM   #4 (permalink)
 
Monster Techie
Join Date: May 2004
Location: /usr/root/mn/us
Posts: 1,121
bla!! is on a distinguished road
Default

There are a couple of ways to do this.

The first is to always make sure the end of your array has a termination character (such as null or an empty string "")

Then when you loop through the array have the termination criteria be i == NULL or i == ""

The other way is to keep track of the length of your array from the beginning of your program. For example, declare your array to be a specific length at the very start of your program or procedure.

There is no built in length() function for C/C++ arrays
__________________
<br>
Its a frigging Laptop, not a Labtop!!!!
bla!! is offline   Reply With Quote
Old 07-07-2006, 01:59 PM   #5 (permalink)
 
True Techie
Join Date: May 2006
Posts: 101
jonmon6691
Default

thanks
__________________
<small>
.:HP Pavillion dv6000:.
1.8 ghtz Intel Core 2 Duo
2GB RAM
120 GB HDD
nVidia Go graphics
Apache 2.2 (looking for a domain name)
</small>
jonmon6691 is offline   Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On