Member Avatar for format_c

Im trying to write a code for a class that has 20 activities .. each activity has a start time and a finish time.

When i try to generate the times the start time is larger than the finish in some cases. can any one help me with my function error.. im trying to get all the start times random other than 0 and smaller than the finish and pass to my set and get funtions in main.

int randomStart(int x)
{
	int a;
	int q;
	x= 0+rand()%(69-0)+1;
	q = randomFinish(a);
	while(x>q)
		{
		x= rand()%69;
		q = randomFinish(a);
		//return x;
	}
		
	return x;
	
	
}

int randomFinish(int y)
{
	y = 1 + rand()%70;
	return y;
}

Your code is confusing. I'm assuming you assign a value to "a" in code you haven't shown, otherwise you're getting whatever junk is in "a" to begin with.

Why not just use rand() to generate a start time, then call it again to generate a random offset from the start time? Afterwards, add that offset to your start time to get the finish time.

Also please use [code] //code here [/code] code tags next time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.