Displaying arrays in c++

Status
Not open for further replies.

jonmon6691

In Runtime
Messages
106
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
 
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.
 
is there a way to return the length of an array so i dont have to keep track of a variable with it?
 
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
 
Status
Not open for further replies.
Back
Top Bottom