How do you find the length of an int array??

I have int array with some values and I need to count how many values are there?

I tried a lot of things like strlen(); sizeof(); etc..

int main()
{

int iarray[10000];

// Now I have a lot of code here.

cout << "the length of the array is " << endl;

return 0;
}

so how do I find the length of the array and no I am not reading in the values straight to the int array, I read it in char array then convert it to string then reverse it and then back to char array and then I convert that char array into an int array. I then need to run a for loop. Thats why I need to find the length of the array.

Thanks in advance.

Recommended Answers

All 8 Replies

You could just create a variable (for example: int size = 0).

Then everytime you insert an item into your array, increment the size variable:

size++;

then at the end of your code output the size variable:
cout << size << endl;

Ya only two choices ....
1. Keep track of insertion and deletion as said above.
2. OR Count all over again

There is no direct way to know how much content you have in an array, versus the size of the array. Except, of course, when your array is of type char and you are using it properly for strings.

Consider an array of integers. Just because some element has a value 0, does that make it an unused element? Zero might well be a valid data value.

Hansels's suggestion to keep track of data as it's entered, and pass that value around along with the array, is about the best general solution.

If I understand what you're doing, when the data is in a string might be a good time to capture the length of the data ( str.size() )

Sounds like an awful lot of work just to reverse the array. Why convert (copy?) from the char array to an int array? Char's are, after all, just small integers.

Have you tried to use a vector?

You can only get the capacity of the array by this:

sizeof( iarray ) / sizeof( int )

Here is the problem statement for which I need to make this program. I hope this clarifies stuff

A plus B (2)

Given two positive or negative integers A and B, find their exact sum.

This time there are no limitations on A and B (of course, they will fit in memory).

A and B are less than 99999 digits.
You have 3 seconds of time to answer each test case.
There are ten test cases. Marked out of 100 making each test case worth 10 points. Good luck!

How are these integers input? Do you read them from a file?
Since you are storing them to array of char, you can, to a small degree, initially treat them as strings, thus use the strlen( ) function to find how many digits were entered.

Don't forget you have to check for a positive/negative sign.

You should perhaps read those two value in two strings variables. And then write your own add function which would find there sum.

you can simply use a char array to store the integer array and then use strlen(array) function of string.h lib to get the length,
or you can also break the integer array in to units by using modulus operator and division.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.