![]() |
|
#1
|
|||
|
|||
Dividing arraysspec:
int divide(string a[], int n, string divider); Rearrange the elements of the array so that all the elements whose value is < divider come before all the other elements, and all the elements whose value is > divider come after all the other elements. Return the index of the first element that, after the rearrangement, is not < divider, or n if there are none. For example, string f[6] = { "kelly", "jim", "michael", "angela", "ryan", "dwight" }; int r = divide(f, 6, "kevin"); // returns 4 // f might now be // "kelly" "jim" "dwight" "angela" "ryan" "michael" // or "dwight" "angela" "jim" "kelly" "michael" "ryan" // or several other orderings. // The first 4 elements are < "kevin"; the last 2 aren't. string g[4] = { "michael", "ryan", "pam", "angela" }; int s = divide(g, 4, "michael"); // returns 1 // g must now be either // "angela" "michael" "ryan" "pam" // or "angela" "michael" "pam" "ryan" // All elements < "michael" (i.e., "angela") come before all others. // All elements > "michael" (i.e., "ryan" and "pam") come // after all others. My code: CPP / C++ / C Code:
It does not work in the case where the divider is the last element in the array and is the smallest in value. How do I fix this problem? thanks Last edited by LuciWiz : 06-Nov-2007 at 13:03.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|||
|
#2
|
|||
|
|||
Re: dividing arrays - need help urgentlyQuote:
I didn't test your program, however... If I am reading the somewhat sloppy problem statement correctly: For this case,all elements in the array have values that are greater than or equal to the divider. In other words, no matter how you arrange the elements, all elements of the array have values that are not less than the divider. Therefore, no matter how the elements are arranged, the index of the first element in the array that is not less than the divider is zero. Or am I missing something? What does your program say? In other words, instead of just saying that your program, "does not work," you should tell us: 1. What did you get from the program? 2. What did you expect to get from the program? 3. What do you not understand about the difference between 2 and 3. After all, it's your program, not ours. Regards, Dave |
|
#3
|
|||
|
|||
Re: dividing arrays - need help urgentlysorry english is not my first language but
this is one of the cases that don't work when the input array is: "jim", "michael", "pam", "angela" simply because my program moves elements behind the divider that are less than the divider to the first element. The above mentioned case becomes an exception. It works for the first test case given in the project spec. |
|
#4
|
|||
|
|||
Re: dividing arrays - need help urgentlyQuote:
I hate to repeat myself but Quote:
The point is for us not to have to guess what you see and what you expect to see. Show us and tell us. Regards, Dave |
|
#5
|
|||
|
|||
Re: dividing arrays - need help urgentlyThe best way to find the mistake is to step through the program and watch the variables making sure that the operations do what they are supposed to do. Try that.
The code looks pretty straight-forward. In the case you described, none of the strings will be less than the divider so when you go through the 'k' loop, you'll always return 0 for the first index. If that is not happening, you should step through it to find the error. |
Recent GIDBlog
Not selected for officer school by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Arrays as function arguments - return arrays from functions | pisuke | C Programming Language | 2 | 25-Jul-2007 11:03 |
| but... I thought I understood arrays! | stephano | Java Forum | 4 | 22-Feb-2007 13:59 |
| Dynamic vs Static Arrays | WaltP | Miscellaneous Programming Forum | 5 | 16-Feb-2006 15:54 |
| Noob question on c arrays and functions | brett | C Programming Language | 1 | 20-Apr-2005 03:59 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The