944,030 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2177
  • C RSS
Mar 24th, 2007
0

sending part of array

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
JadedTortoise is offline Offline
8 posts
since Mar 2007
Mar 24th, 2007
0

Re: sending part of array

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. }
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Mar 24th, 2007
0

Re: sending part of array

I think you need to re-assess how to do this very carefully. At the moment it will fail somewhere down the line.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 24th, 2007
0

Re: sending part of array

Click to Expand / Collapse  Quote originally posted by thekashyap ...
  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.
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Mar 24th, 2007
0

Re: sending part of array

Click to Expand / Collapse  Quote originally posted by iamthwee ...
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.. ?
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Mar 24th, 2007
0

Re: sending part of array

>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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 24th, 2007
0

Re: sending part of array

Click to Expand / Collapse  Quote originally posted by thekashyap ...
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.


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.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Returning An Array
Next Thread in C Forum Timeline: Set combinations function





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC