HELP! Arrays ~hw problem~

Feb 9, 2014 at 7:51pm
I had a stomach virus and missed class for a week and a half. I have emailed my teacher informing him and asking for the topics taught but he didn't message me back. I have no contacts in that class to help me out so I resort to this website. Please help me understand how to solve this homework problem!!

http://venus.cs.qc.cuny.edu/~waxman/211/rearrange%20'em.pdf

the last we did was the shift equivalent of arrays.

I dont understand how to use the rand() function and my efforts of researching it just confused me more all it does is produce a random number? how do i set the limits?
1
2
3
4
5
6
#include <iostream>
using namespace std;
void p(int k[],int n)
{
	int m= (k[n/2]+k[(n/2)-1])/2
	


what i have so far...
Last edited on Feb 9, 2014 at 9:08pm
Feb 9, 2014 at 8:32pm
Please make the small effort of putting the homework problem quoted in your post, explaining exactly what is it you don't understand, and possibly showing the code that you wrote so far.
Feb 9, 2014 at 8:41pm
@incognitocpp ;)
Feb 9, 2014 at 9:09pm
@catfish666 sorry :P I should've done that earlier. I wasn't thinking straight.

@chriscpp loll :D
Feb 9, 2014 at 9:13pm
@incognitocpp you know the rules ;) lol do you?
Feb 9, 2014 at 9:17pm
Look up what a function is. You can limit the maximum random number using modulo.

If you don't understand any of this, look it up.
Feb 9, 2014 at 9:20pm
@chriscpp what rules?

@iwishiknew i dont understand how to place the elements to the left and right of m.
Feb 9, 2014 at 9:35pm
I dont understand how to use the rand() function and my efforts of researching it just confused me more all it does is produce a random number? how do i set the limits?

Before using rand() you must seed the random number generator.
You seed the RNG by using srand(). Call it just once, in main().

1
2
3
4
5
6
7
8
#include <cstddef>
#include <cstdlib>

int main()
{
    std::srand(std::time(NULL));
    // we seed the RNG with a "random" number: the current time
}


Now when you use rand() it will give you a big integer. You trim it down by using the modulo operation.

1
2
3
4
5
6
7
// ...

std::rand() % 10;   // could be anything from 0 to 9
std::rand() % 3;    // could be anything from 0 to 2
std::rand() % 210;  // could be anything from 0 to 209

// ... 


So now the final step is, how to also set a minimum value, so that it's not always 0?
In fact I'll just give you the formula, and you reason why it is correct (if it is).

std::rand() % (max + 1 - min) + min; // could be anything from min to max
Last edited on Feb 9, 2014 at 9:36pm
Feb 9, 2014 at 10:31pm
I am more focused on finishing the function than doing the task. Is this the correct way to create the function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;
void p(int k[],int n)
{
	int m= (k[n/2]+k[(n/2)-1])/2
	for(int i=0; i<n;i++)
	{
		if(k[i] < m)
			k[m++]=k[i];	
			
		if(k[i] > m)
			k[m--]=k[i];
	}
}
Feb 9, 2014 at 11:40pm
The middle is given by int middle = k[n/2]

Where n is the size of the array. Just watch your code because you might go out of the bounds.
Feb 10, 2014 at 2:56am
Thanks guys! I figured out how to do it with all your help :D
Topic archived. No new replies allowed.