Length of an int array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2009
Posts: 66
Reputation: djextreme5 is on a distinguished road 
Solved Threads: 4
djextreme5's Avatar
djextreme5 djextreme5 is offline Offline
Junior Poster in Training

Length of an int array

 
0
  #1
Mar 7th, 2009
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..

  1.  
  2. int main()
  3. {
  4.  
  5. int iarray[10000];
  6.  
  7. // Now I have a lot of code here.
  8.  
  9. cout << "the length of the array is " << endl;
  10.  
  11. return 0;
  12. }

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.
Last edited by djextreme5; Mar 7th, 2009 at 6:52 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 12
Reputation: hansel13 is an unknown quantity at this point 
Solved Threads: 0
hansel13 hansel13 is offline Offline
Newbie Poster

Re: Length of an int array

 
0
  #2
Mar 7th, 2009
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;
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 68
Reputation: Rhohitman is an unknown quantity at this point 
Solved Threads: 4
Rhohitman's Avatar
Rhohitman Rhohitman is offline Offline
Junior Poster in Training

Re: Length of an int array

 
0
  #3
Mar 7th, 2009
Ya only two choices ....
1. Keep track of insertion and deletion as said above.
2. OR Count all over again
Chazing Dreams ;'P
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Length of an int array

 
0
  #4
Mar 7th, 2009
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.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 57
Reputation: BevoX is on a distinguished road 
Solved Threads: 12
BevoX's Avatar
BevoX BevoX is offline Offline
Junior Poster in Training

Re: Length of an int array

 
0
  #5
Mar 7th, 2009
Have you tried to use a vector?

You can only get the capacity of the array by this:
  1. sizeof( iarray ) / sizeof( int )
"Just because I'm losing, doesn't mean I'm lost. Doesn't mean I'll stop."
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 66
Reputation: djextreme5 is on a distinguished road 
Solved Threads: 4
djextreme5's Avatar
djextreme5 djextreme5 is offline Offline
Junior Poster in Training

Re: Length of an int array

 
0
  #6
Mar 7th, 2009
Here is the problem statement for which I need to make this program. I hope this clarifies stuff

  1. A plus B (2)
  2.  
  3. Given two positive or negative integers A and B, find their exact sum.
  4.  
  5. This time there are no limitations on A and B (of course, they will fit in memory).
  6.  
  7. A and B are less than 99999 digits.
  8. You have 3 seconds of time to answer each test case.
  9. There are ten test cases. Marked out of 100 making each test case worth 10 points. Good luck!
Last edited by djextreme5; Mar 7th, 2009 at 8:55 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Length of an int array

 
0
  #7
Mar 8th, 2009
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.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 794
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Length of an int array

 
0
  #8
Mar 8th, 2009
You should perhaps read those two value in two strings variables. And then write your own add function which would find there sum.
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 53
Reputation: arshad115 is an unknown quantity at this point 
Solved Threads: 2
arshad115's Avatar
arshad115 arshad115 is offline Offline
Junior Poster in Training

Re: Length of an int array

 
0
  #9
Mar 8th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 980 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC