sending part of array

Reply

Join Date: Mar 2007
Posts: 8
Reputation: JadedTortoise is an unknown quantity at this point 
Solved Threads: 1
JadedTortoise JadedTortoise is offline Offline
Newbie Poster

sending part of array

 
0
  #1
Mar 24th, 2007
Question:I ran into a point where it would be very helpful to send a function a portion of an array, instead of the entire thing. Now i can think of a couple work arounds like sending the whole array and a begin number, and range number, then computing off of that, or making a second array with only the portion needed and sending it.

I am hoping however that there is a simpler way.

int myfunc(array[]) {}
x = myfunc(array[from a to b]);

is essentially the idea though I am certain its not in syntax.


Reason: In my programming a calculator I am ending up with some what is fairly convoluted code to essentially make it take any equation entered, compute it and return its output. And I would like to save 8 lines of code and hopefully a few steps for the processor.

as in:
string equation = 3+(42*-3.46);
answer = value_of(equation);
where value_of() is an absurdly complex function hehe
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: sending part of array

 
0
  #2
Mar 24th, 2007
Given you're using std::string you have many easy alternatives:
1. Use
basic_string substr(size_type pos = 0, size_type n = npos) const;
Returns a string composed of copies of the lesser of n and size() characters in this string starting at index pos.
Throws an out_of_range exception if pos > size().

2. Use this constructor which creates new string object using a substring of an existing strnig object: basic_string (const basic_string& str, size_type pos, size_type n= npos, const allocator& a=allocator());

If you're really concerned abt teh performance, you might wann use char* instead of std::string. In this case if you want to pass a sub string, the begin part can be taken care of by pointer arithmatic. The end part is tricky and it would be best (in terms of performance) if you pass another integer to indicate the end.
E.g.

  1. void comlex_func( char* equetion, const int length ) ;
  2.  
  3. int main()
  4. {
  5. char equation[] = "1+2+3*4" ;
  6. //say we want to pass "2+3" part to function.
  7. comlex_func( equation + 3, 3 ) ;
  8.  
  9. return 0;
  10. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: sending part of array

 
0
  #3
Mar 24th, 2007
I think you need to re-assess how to do this very carefully. At the moment it will fail somewhere down the line.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: sending part of array

 
0
  #4
Mar 24th, 2007
Originally Posted by thekashyap View Post
  1. void comlex_func( char* equetion, const int length ) ;
  2.  
  3. int main()
  4. {
  5. char equation[] = "1+2+3*4" ;
  6. //say we want to pass "2+3" part to function.
  7. comlex_func( equation + 3, 3 ) ;
  8.  
  9. return 0;
  10. }
Just a warning...

  1. void comlex_func( char* equetion, const int length ) ;
  2.  
  3. int main()
  4. {
  5. char equation[] = "1+2+3*4" ;
  6. //say we want to pass "2+3" part to function.
  7. comlex_func( equation + 3, 3 ) ;
  8.  
  9. return 0;
  10. }
  11.  
  12. void comlex_func( char* equetion, const int length )
  13. {
  14. printf("What I got is: ") ;
  15. for( int i = 0; i < length; i++ )
  16. printf( "%c", equetion[i] ) ;
  17.  
  18. printf("Beware the null char is not at teh end of supplied length: %s", equetion ) ;
  19. }
Last edited by thekashyap; Mar 24th, 2007 at 8:27 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: sending part of array

 
0
  #5
Mar 24th, 2007
Originally Posted by iamthwee View Post
I think you need to re-assess how to do this very carefully. At the moment it will fail somewhere down the line.
Just wondering what problem do you see.. ?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: sending part of array

 
0
  #6
Mar 24th, 2007
>Just wondering what problem do you see.. ?

I don't see a problem thus far. You've gotta split the array into tokens. I was just saying without careful planning for every possible outcome it is likely to fail. His/her design at the mo looks very simplistic, almost as if she/he's doing as she goes.
Last edited by iamthwee; Mar 24th, 2007 at 8:40 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: sending part of array

 
0
  #7
Mar 24th, 2007
Originally Posted by thekashyap View Post
Just a warning...
  1. printf("Beware the null char is not at teh end of supplied length: %s", equetion ) ;
  2. }
So? We aren't dealing with a string at this point. It's an array of characters. An array of character values does not need a null at the end. Only an array of characters representing a string.


Originally Posted by JadedTortoise View Post
Reason: In my programming a calculator I am ending up with some what is fairly convoluted code to essentially make it take any equation entered, compute it and return its output. And I would like to save 8 lines of code and hopefully a few steps for the processor.
This is a very bad reason to do anything. Assess what something easier means. Does it make the code harder to read? Do you have to resort to tricks to accomplish your goal? Will these tricks compromise maintainability (making it harder to change the code later)? Just trying to save a couple nano-seconds of processor time is not a good thing. Readability should not be compromised.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC