User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 426,198 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,878 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 881 | Replies: 6
Reply
Join Date: Mar 2007
Posts: 8
Reputation: JadedTortoise is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
JadedTortoise JadedTortoise is offline Offline
Newbie Poster

sending part of array

  #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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2007
Location: Bangalore, India
Posts: 535
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Rep Power: 4
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: sending part of array

  #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  
Join Date: Aug 2005
Posts: 4,782
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 319
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: sending part of array

  #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.
I'm not a programmer. My attitude starts with ignorance, holds steady at conversation, and ends with a trip to the hospital. Get used to it.
Reply With Quote  
Join Date: Feb 2007
Location: Bangalore, India
Posts: 535
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Rep Power: 4
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: sending part of array

  #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 7:27 am.
Reply With Quote  
Join Date: Feb 2007
Location: Bangalore, India
Posts: 535
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Rep Power: 4
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: sending part of array

  #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  
Join Date: Aug 2005
Posts: 4,782
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 319
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: sending part of array

  #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 7:40 am.
I'm not a programmer. My attitude starts with ignorance, holds steady at conversation, and ends with a trip to the hospital. Get used to it.
Reply With Quote  
Join Date: May 2006
Posts: 2,723
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 222
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: sending part of array

  #7  
Mar 24th, 2007
Originally Posted by thekashyap View Post
Just a warning...
    printf("Beware the null char is not at teh end of supplied length: %s", equetion ) ;
}
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.
Age is unimportant -- except in cheese
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 6:59 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC