Computer ForumsComputers  

Go Back   Computer Forums > Programmers Lounge > Programming Discussions

Reply
 
LinkBack Thread Tools Display Modes
Old 07-03-2006, 02:08 PM   #1 (permalink)
True Techie
 
Join Date: May 2006
Posts: 101
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
__________________

.: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)
jonmon6691 is offline   Reply With Quote
Old 07-05-2006, 01:15 PM   #2 (permalink)
Resident Badass
 
Spartan's Avatar
 
Join Date: Jul 2005
Posts: 821
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
Default

is there a way to return the length of an array so i dont have to keep track of a variable with it?
__________________

.: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)
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,096
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
__________________


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
Default

thanks
__________________

.: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)
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



All times are GMT -5. The time now is 05:07 PM.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.1.0