GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Nov-2007, 01:04
cloud cloud is offline
New Member
 
Join Date: Nov 2007
Posts: 2
cloud is on a distinguished road

Dividing arrays


spec:
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:
int divide(string a[], int n, string divider)
{
string temp;
if(n<0)
	return -1;
for(int i =0; i < n; i++)
if(a[i] < divider)
{
	temp = a[i];
	for(int j=i; j > 0  ; j--)
		a[j] = a[j-1];
	a[0] = temp;
}

for(int k =0; k < n ; k++)
{
	if(a[k] >= divider)
		return k;
}
	return n;
}


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  
Old 06-Nov-2007, 08:03
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,305
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: dividing arrays - need help urgently


Quote:
Originally Posted by cloud
...where the divider is the last element in the array and is the smallest in value...

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  
Old 06-Nov-2007, 11:38
cloud cloud is offline
New Member
 
Join Date: Nov 2007
Posts: 2
cloud is on a distinguished road

Re: dividing arrays - need help urgently


sorry 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  
Old 06-Nov-2007, 12:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,305
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: dividing arrays - need help urgently


Quote:
Originally Posted by cloud
sorry 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"

I hate to repeat myself but
Quote:
Originally Posted by davekw7x
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 1 and 2. After all, it's your program, not ours.

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  
Old 06-Nov-2007, 13:17
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 761
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: dividing arrays - need help urgently


The 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 GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 22:07.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.